Bind body and uri http parameters into Spring controller method parameters - spring

Hy,
I have the next controller method in Spring:
#RequestMapping(value="indicadores/{entUsuario}/{codUsuario}/{idCliente}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
#ResponseBody
public IndicadoresBean getIndicadoresPlanBasicoBean(
#RequestBody InvestmentProposalGroupingResponse investmentProposalGroupingBean,
IndicadoresPlanBasicoRequest request,
BindingResult validationResult,
HttpServletResponse response) {......... }
But when I send a request I get :
"Required request body content is missing: org.springframework.web.method.HandlerMethod$HandlerMethodParameter".
If I change the verb to POST it works but I need it works with GET
I need to fill in the request body json in the parameter investmentProposalGroupingBean and uri parameters in parameter request. Is possible?
Thanks

Related

Spring mvc 4 #PostMapping or #RequestMapping expose rest give 415 response

I am trying to expose rest webservice using spring mvc 4.
With these annotations in controller
#RequestMapping(value = "/services/empservice", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
public String testArgument(#RequestBody Employee employee){
System.out.println("employee variable--->"+employee.getEmployeeID());
return String.valueOf(20);
}
#PostMapping(value = "/services/empservices", produces = MediaType.APPLICATION_JSON_VALUE,headers = "content-type=application/x-www-form-urlencoded")
public String testArguments(#RequestBody Employee emploee){
System.out.println("employee variable--->"+employee.getEmployeeID());
return employee.getEmployeeID();
}
The response from WAS 8.5.5 gives 415 Media unsupported Status with
response header
Accept →application/octet-stream, text/plain, application/xml, text/xml, application/x-www-form-urlencoded, application/*+xml, multipart/form-data, /
Accept →application/x-www-form-urlencoded
How to get pass this error and how to set response header for getting application/json with json response
Your issue should be resolved with the next issue of the Content type 'application/x-www-form-urlencoded;charset=UTF-8' not supported for #RequestBody answer.
Where necessary to remove annotation #RequestBody

Call Spring endpoint without content-type setted

What is necessary to set in method signature in Spring for a call from Postman or others without Content-Type set?
The signature is:
#RequestMapping(value = "/{id}", method = RequestMethod.GET, produces = "application/json")
#ResponseBody
public ResponseEntity getHouseholdChanges (#PathVariable("id") Long id) { ... }
When I call in Postman without Content-Type I received 415 status.
I need from others call when I can't set content type but is same always for this method.
Try do delete from method signature the
produces = "application/json"
This should return a String that postman should display without throwing any error code.

How to pass json parameters with jax-rs?

if I want to post json parameter to a url with jax-rs, how to receive the parameters in the back end and convert it into an integer array?
parameters:{"sbfIdList":[14]}
backend service(in java): I tried below , but feel no luck, I just got an empty array.
#POST
#Consumes(MediaType.APPLICATION_JSON)
#Path("/delete")
public String deleteSbc(#QueryParam("sbfIdList") List<Long> sbfIds,#Context HttpServletRequest request, #Context HttpServletResponse response) {
return "";
}
I request the backend service using POST method.

Spring 3.2 forward request with new object

I'm trying to forward a request from one controller to another controller and set a object in request so that the forwarded controller can use it in #RequestBody.
Following is the exact scenario:
Twilio calls a controller method with data sent by client as following:
#RequestMapping(value = "/sms", method = RequestMethod.POST)
public String receiveSms(#RequestParam("Twiml") String twiml,
HttpServletRequest request,
HttpServletResponse response) {
//TODO - Create new instance of Activity and populate it with data sent from client
return "forward:/activity/new";
}
Now, I want to forward this request to ActivityController which already handles the request from web/rest clients.
ActivityController.java has a method with following signature:
#RequestMapping(value = "/activity/new", method = RequestMethod.POST)
public ResponseEntity<Activity> updateLocation(
#RequestBody Activity activity) {
}
Is it possible? If yes, how?
Thanks,
Create the object and add it to the request as an attribute in the first controller,
request.setAttribute("object",new OwnObject()),
return "forward:/activity/new";
In the updateLocation Method retrieve that object from the request
#RequestMapping(value = "/activity/new", method = RequestMethod.POST)
public ResponseEntity<Activity> updateLocation(
#RequestBody Activity activity, HttpServletRequest request) {
OwnObject o = (OwnObject) request.getAttribute("object");
}

Using both #RequestBody and #RequestParam together in spring mvc3

I am using spring-mvc 3.1.0.RELEASE and for some reason, mapping POST with query params and request body does not work.
Here is how my controller method looks:
#RequestMapping(method = POST, value = "/post-to-me/")
public void handlePost(
#RequestBody Content content,
#RequestParam("param1") String param1,
#RequestParam("param2") String param2
){
//do stuff
}
However, if I convert all the request params to path params, mapping works. Has anyone run into something similar?
Thanks!
EDIT:
"does not work" == 404 when I try doing, POST /post-to-me?param1=x&param2=y
First, your POST url doen't match the controller method url, your POST url must be "/post-to-me/?param1=x&param2=y" not "/post-to-me?param1=x&param2=y"
Second, where did Content class come from?? I used a String and works fine for me
#RequestMapping(method = RequestMethod.POST, value = "/post-to-me/")
public void handlePost(#RequestBody String content,
#RequestParam("param1") String param1,
#RequestParam("param2") String param2, HttpServletResponse response) {
System.out.println(content);
System.out.println(param1);
System.out.println(param2);
response.setStatus(HttpServletResponse.SC_OK);
}
Note that I used HttpServletResponse to return a HTTP 200 code, but I think there is a better solution for return Http codes, check this: Multiple response http status in Spring MVC
Trailing slash at the end of your request mapping value might be the problem.
Try:
#RequestMapping(method = RequestMethod.POST, value = "/post-to-me")
or send your POST request to POST /post-to-me/?param1=x&param2=y

Resources