Spring request mapping with Paypal - spring

For the return URL, it seem you have to define the whole URL like so:
String returnURL = "http://localhost:8080/appName/shopping/confirmorder";
Now, I have a problem with the request mapping:
#RequestMapping(value = "/shopping/confirmorder?token={token}&PayerID={payerID}", method = RequestMethod.GET)
public String doGet(#PathVariable("token") String token, #PathVariable("payerID") String payerID,
HttpServletRequest request) {
// do stuff
}
The controller is never called for some reason?
The final returnURL returned from Paypal is like this:
http://localhost:8080/appName/shopping/confirmorder?token=EC-4...G&PayerID=A...W
Note the Ids have been edited.

If you have two path variables named token and payerID, then the method signature should be
public void doGet(#PathVariable("token") String token,
#PathVariable("payerID") String token,
HttpServletRequest request,
HttpServletResponse response)
How did you expect Spring to put those two strings inside a single option parameter of type int?
See http://static.springsource.org/spring/docs/current/spring-framework-reference/html/mvc.html#mvc-ann-requestmapping-uri-templates
Moreover, PathVariable is used to bind portions of the request path to method arguments. In your case, you have request parameters. You should thus use #RequestParam:
#RequestMapping(value = "/shopping/confirmorder", method = RequestMethod.GET)
public void doGet(#RequestParam("token") String token,
#RequestParam("PayerID") String token,
HttpServletRequest request,
HttpServletResponse response)
See http://static.springsource.org/spring/docs/current/spring-framework-reference/html/mvc.html#mvc-ann-requestparam

Related

Two similar Spring endpoints, wrong one is being called

I have two endpoints in the same controller file
#PostMapping("{app}/{schema}")
public ResponseEntity createResource(
#PathVariable final String app,
#PathVariable final String schema,
#RequestBody final Object input,
final HttpServletRequest request) {
//do stuff
}
#PostMapping("{app}/bulk")
public ResponseEntity bulk(
#PathVariable final String app,
#RequestBody final Object input,
final HttpServletRequest request) {
//do stuff
}
The createResource gets called when I do POST app/bulk.
Your first endpoint
#PostMapping("{app}/{schema}")
is a wildcard endpoint due to two PathVariables. Therefor, any request to a path yourapp.com/xyz is caught by it and the second endpoint is always ignored.
To solve this, you should either create two distinct endpoints without using the {schema} PathVariable like you are doing in the second endpoint, or just use the first one as is and check the {schema} variable for the current path in //do stuff.
This is because when the URL gets resolve, your first endpoint will perfectly fit in.
#PostMapping("{app}/{schema}")
Here bulk will be resolve as {schema} string. You can avoid it by defining a unique name like this :
#PostMapping("{app}/schema/{schema}")
This should solve your problem.
You should merge two endpoint like this
#PostMapping("{app}/{schema}")
public ResponseEntity createResource(
#PathVariable final String app,
#PathVariable final String schema,
#RequestBody final Object input,
final HttpServletRequest request) {
if ("bulk".equals(schema)) {
// do bulk stuff
} else {
//do stuff
}
}

Error 400 when receiving data from URL parameters en Spring MVC

I am trying to receive data from an URL with two parameters like this one:
http://localhost:80000/xxx/xxx/tickets/search?codprovincia=28&municipio=110000
No matter the approach, I am always getting a 400 error, but if I access the URL without the two parameters, the controller returns the view correctly (without the parameters, naturally)
This is the code of my controller:
#Controller
#RequestMapping(value = "/xxx" )
public class BuscadorIncidenciasController extends BaseControllerWeb {
#RequestMapping("tickets")
public String tickets(Model model, #RequestParam ("codprovincia") String codprovincia, #RequestParam ("municipio") String municipio, HttpServletRequest request) throws NoAjaxException {
//...
return CONST.JSP_VIEW;
}
...}
Extra info: if I use this URL:
http://localhost:9081/xxx/xxx/tickets/search/28/790000
And this code:
#Controller
#RequestMapping(value = "/xxx" )
public class BuscadorIncidenciasController extends BaseControllerWeb {
#RequestMapping(value = "buscar/{codprovincia}/{municipio}", method = RequestMethod.GET)
public String buscar(#PathVariable Integer codprovincia, #PathVariable Integer municipio ,Model model, HttpServletRequest request) throws NoAjaxException {
//...
return CONST.JSP_VIEW;
}
...}
It gets the parameters correctly. The problem is that I have to use the first URL. I have reviewed similar questions about similar issues, and I have implemented the solutions to those issues, but I get the 400 error regardless what I try (add value="xxx=, required=false, and other suggestions.)
For RequestParam, you need to explicitly add 'name' attribute
#RequestParam(name = "codprovincia"), #RequestParam (name = "municipio")
No need to for HttpServletRequest, unless you have reason
Also, in your 'tickets' method, RequestMapping is not conforming to your URL path.
I think it should be
#RequestMapping("/xxx/tickets/search")
Cheers!

Spring - Query parameters without question mark

I'm having an issue parsing an URL with Spring.
My endpoint is
#RequestMapping(path = "/register", method = RequestMethod.GET)
public String userActivation(#RequestParam("token") String token, #RequestParam("code") String code, final Map<String, Object> model) {
...
}
So I am expecting a token and a code in the URL.
The problem I am facing is that the service redirecting to my page omits the question mark, something like:
http://myapp/register/&token=sdgddfs&code=fdasgas
Which Spring fails to match to my endpoint.
Is there any way to handle this?
You can re-write you method using #PathVariable instead of #RequestParam
So you'll have an URL like http://myapp/register/sdgddfs/fdasgas, and an annotation for method
#RequestMapping(path = "/register/{token}/{code}")
public String userActivation(#PathVariable("token") String token, #PathVariable("code") String code) { ... }

how to use #Requestparam #RequestBody together in spring restful

how to use #Requestparam #RequestBody together in spring restful
#RequestMapping(method = RequestMethod.POST, value = "/upate")
#ResponseBody
public ModelAndView availableCheck(
#RequestParam("key") String key, #RequestBody User user)
throws Exception {
//handle
//
}
I want to update user by unique key,so I need request key paramer and the new user json object.
Advance thanks!
There is some possible mistake: if you return a ModelAndView then it is highly unlikely that you want to be it the ResponseBody, therefore remove #ResponseBody.
The other problem is that RespondeBody is for strings. It mean put the Body string in this variable.
So it your user is the command object populated by some form, then just remove the #RequestBody annotation
#RequestMapping(method = RequestMethod.POST, value = "/upate")
public ModelAndView availableCheck(
#RequestParam("key") String key, User user)
throws Exception {
//handle
//
}

Need to set parameter value and datatypes taken from URI of resful web service into HashMap

#RequestMapping(value = "/{methodName}/{responseType}", method = RequestMethod.GET)
public ModelAndView execute(#PathVariable String methodName, #PathVariable String responseType,
HttpServletRequest request, HttpServletRequest response){
Map<String,String> yesMap = new HashMap<String,String>();
}
As shown in code above I need to get data types of parameters passed from {responseType} and set the same in yesMap against parameter values.
String param=request.getParameter("id");
is returning null values.
need to get data types of parameters passed from {responseType}
Its type will always be String as you are collecting it in responseType which is String.
get data types of parameters passed from {responseType} and set the same in yesMap against parameter values
I think yesMap.put("String",responseType); will do.
In the code you posted above, the follwign call
yourRootUrl/cheese/stilton
will result in these being true in your "execute" method:
methodName.equals("cheese");
responseType.equals("stilton");
I don't understand what you are trying to achieve. Your parameter names are confusing.
Its quite simple, you have declared two string paramters, which thanks to spring can be path variables.
This is a GET request, so getParamter won't work, try using String param=request.getAttribute("id"); which is completely unrelated to your path variables. Or the differetn annaotion RequestParam like so :
#RequestMapping(value = "/{methodName}/{responseType}", method = RequestMethod.GET)
public ModelAndView execute(#PathVariable String methodName, #PathVariable String responseType, #RequestParam int id)

Resources