#RequestMapping(value = "/connect{accountDeviceId}", method = RequestMethod.GET)
public #ResponseBody
String showCompleteAuthorizat(HttpServletRequest request,
HttpServletResponse response,
#RequestParam("accountDeviceId") int accountDeviceId) throws Exception {
I'm getting this error
org.springframework.web.bind.MissingServletRequestParameterException: Required int parameter 'accountDeviceId' is not present
and this is the url that calls the above api
http://localhost:8080/gch-restful/fitbit/connect29?oauth_token=b1e939450e745664ce4bbbc194b4ed47f&oauth_verifier=9dc1045654dc775d2347ae2963d5ae878c
I'm new to spring and basically learning it, please tell me what am I doing wrong here.
Regards
You should use #PathVariable(value="accountDeviceId") instead of #RequestParam("accountDeviceId") in order to get a variable from url.
Related
I'm running into a situation where my model attribute is losing values between pages.
I've got two controller methods that handle GET and POST requests respectively.
GET Method
#RequestMapping(value = "/checkout/billing", method = RequestMethod.GET)
public String getBillingPage(Model model, final HttpServletRequest request) throws CMSItemNotFoundException {
// other code
checkoutForm.setCustomFieldsForm(customFieldsForm);
model.addAttribute("checkoutForm", checkoutForm);
// other code
}
Debug View After GET Method completes
POST Method
#RequestMapping(value = "/checkout/billing", method = RequestMethod.POST)
public String submitPayment(
#Valid #ModelAttribute("checkoutForm") final CheckoutForm checkoutForm,
final BindingResult bindingResult,
Model model,
final HttpServletRequest request,
final HttpServletResponse response) throws CMSItemNotFoundException
{}
Debug View When POST Method is Invoked
The 1234 comes from the user entering that data in the form field. The other values should still be there and not null though.
What could be happening here?
Your model is not stored in the session. Each request creates a new model object. That's why it's empty.
You can add your model as a session attribute. Please find the documentation here https://docs.spring.io/spring/docs/3.2.x/spring-framework-reference/html/mvc.html#mvc-ann-sessionattrib
I have a question with Spring MVC RequestMapping annotation. need your help.
I have created one IPSLcontroller and i want that IPSLcontroller to handle all request url.i have created two method in this controller.
1)handleLogoutRequest :- this method should invoke on below url.
2)handleRequest :- this method should invoke on all request url otherthan logout.
http://localhost:9086/webapp/login
or
http://localhost:9086/webapp/add
or
http://localhost:9086/webapp/remove
here is my sample code. but it's not working as expected.
#Controller
public class IPSLController {
#RequestMapping(value={"/logout/*"},method = RequestMethod.POST)
protected void handleLogoutRequest(HttpServletRequest request,
HttpServletResponse response) throws Exception {
System.out
.println("........................IPSLController logout request.......................................");
}
#RequestMapping(method = RequestMethod.POST,value={"/*"})
protected void handleRequest(HttpServletRequest request,
HttpServletResponse response) throws Exception {
System.out
.println("........................IPSLController all request Post.......................................");
}
}
You should use a general Prefix for every controller you use, so you can differ between them better. Also you donĀ“t need any "/" for calls like this.
#Controller
#RequestMapping("ispl")
public class IPSLController {
#RequestMapping(value={"logout"},method = RequestMethod.POST)
protected void handleLogoutRequest(HttpServletRequest request,
HttpServletResponse response) throws Exception {
System.out
.println("........................IPSLController logout request.......................................");
}
#RequestMapping(method = RequestMethod.POST,value={"hello"})
protected void handleRequest(HttpServletRequest request,
HttpServletResponse response) throws Exception {
System.out
.println("........................IPSLController all request Post.......................................");
}
}
If you now want to call them over a ServletRequest or with a restService or something similar you should declare them like this
#GET
#Path("ispl/logout")
public void Method (HttpServletResponse ...)
Well it is working the way it should. You have a mapping for /* and for /logout/*. So when you post to /logout it invokes the method for /*. I suspect that if you post to /logout/something it would invoke your logout handler.
If you want it to work, you cannot have a wildcard mapping for the second method. At least use /something/* so that spring can make a correct decision on mappings.
i want to call REST API Controller from my REST API Controller.
"http://localhost:8080/function/1?filter={"id":1435263}"
Since we cannot send directly ({"id":1435263})JSON query String along with url because of spring mvc cannot read "{",i am sending query string(Search variables) in the form of map .
Object response = restTemplate.getForObject(crudControllerURL,
Object.class, map);
where map contains the values .
On Server side i am unable to retrieve this map. I tried #RequestParam Object obj but it did not work . I am clueless how can i get these values there?
Do i need to convert it into POST?
EDIT
when i try to use whole url with query String then i recieve
java.lang.IllegalArgumentException: Not enough variable values available to expand '"id"'
Adding Server side contoller code snippet(not whole) and please note i need to access map in Server REST API Controller .
Server side controller
#RequestMapping(value = "/{function}/{type}", method = RequestMethod.GET)
public List<Order> performFetchAll(#PathVariable String function,
HttpServletRequest request) throws JsonParseException,
JsonMappingException, IOException, InvalidAttributesException {
String requestQueryString = request.getQueryString();
if (requestQueryString == null
|| requestQueryString.equalsIgnoreCase(""))
return orderService.findAll();
Please provide your feedback. Thanks.
You should do this probably than complicating the request:
URL can be changed like this : http://localhost:8080/function/1?idFilter=1435263
#RequestMapping(value = "/{function}/{type}", method = RequestMethod.GET)
public List<Order> performFetchAll(#PathVariable String function, #RequestParam("idFilter") String id, HttpServletRequest request) throws JsonParseException,
JsonMappingException, IOException, InvalidAttributesException {
//DO something
}
If your filter request is going to be big and complex like a json, change your method to POST and take the JSON and do your logic.
I have the following property that I need mapped to a post parameter in Spring. Is there an attribute I can use? It accepts application/x-www-form-urlencoded for string-based payloads, multipart/form-data for binary payloads. Other properties are mapping fine without underscores.
String deliveryAttemptId;
mapped to the post parameter
DELIVERY-ATTEMPT-ID
Controller
#Controller
#RequestMapping("/notifications")
public class NotificationController {
#RequestMapping(method = RequestMethod.POST)
#ResponseBody
public void grade(EventNotificationRequest request, HttpServletResponse response) throws Exception {
}
Model
public class EventNotificationRequest {
String deliveryAttemptId;
I just made a work around for this Spring limitation. This also fixes case sensitivity issues with parameters. Sorry I am used to .NET and how easy binding is so it's frustrating to run into these Spring issues.
HttpServletRequest parameter lowecase
#RequestMapping(method = RequestMethod.POST, value = "/grade")
#ResponseBody
public void grade(HttpServletRequest request, HttpServletResponse response) throws Exception {
EventNotificationRequest notificationRequest = new LearningStudioEventNotificationRequest();
notificationRequest.setDeliveryAttemptId(getCaseInsensitiveParameter(request, "DELIVERY-ATTEMPT-ID"));
I am trying to convert controllers from the old inheritance framework to the new annotations.
Here's an existing controller:
public class SelectedTabController extends AbstractController {
private TabSelectionHelper tabSelectionHelper;
public ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response) throws Exception {
String param = request.getParameter("selectedTab");
if (param != null)
tabSelectionHelper.setSelectedTabTo(param);
return null;
}
public void setTabSelectionHelper(TabSelectionHelper tabSelectionHelper) {
this.tabSelectionHelper = tabSelectionHelper;
}
And after conversion I have this:
#Controller
public class SelectedTabController {
private TabSelectionHelper tabSelectionHelper;
#Autowired
public SelectedTabController(#Qualifier(value = "tabSelectionHelper") TabSelectionHelper tabSelectionHelper) {
this.tabSelectionHelper = tabSelectionHelper;
}
#RequestMapping("/selectedTab")
public void selectTab(String selectedTab, HttpServletResponse response) throws Exception {
//String param = request.getParameter("selectedTab");
if (selectedTab != null)
tabSelectionHelper.setSelectedTabTo(selectedTab);
}
}
This works but there is a (redundant) HttpServletResponse object in the selectTab paramter list. If I remove it, then the JQuery call says the server returns 500 and the call fails.
Any help?
The stacktrace shows:
javax.servlet.ServletException: Could not resolve view with name 'selectedTab' in servlet with name 'prodman'
So it is trying to find a view and failing. However, there is NO view to display as its a backend callby JQuery.
I guess by declaring the response object, Spring thinks I will write the response.
How can I prevent Spring from trying to resolve a view?
When you use void as your return type Spring will by default try to determine the view name from your method name, unless it thinks you're directly writing the response (which it does when you have a HttpServletResponse as a parameter). Have a look at section 15.3.2.3 of the Spring 3 docs.
You might want to try changing the return type to ModelAndView and return null and see what happens (I'm not certain you can get away with a null view with #RequestMapping as it's not something that I have ever tried)