RequestBody REST Applications

Iam bit new for SpringMVC REST concept. I need help from experts here to understand / solve the following problem, I developed the SpringMVC application, the following is part of the controller class code, and it works fine with the way it is, which means that it works fine with an object of type JSON.

@RequestMapping(method = RequestMethod.POST, value = "/user/register") public ModelAndView addUser( @RequestBody String payload) { try{ ObjectMapper mapper = new ObjectMapper(); CreateNewUserRequest request = mapper.readValue(payload, CreateNewUserRequest.class); UserBusiness userBusiness = UserBusinessImpl.getInstance(); CreateNewUserResponse response = userBusiness.createNewUser(request); return new ModelAndView(ControllerConstant.JASON_VIEW_RESOLVER, "RESPONSE", response); 

and this is my rest-servlet.xml looks like

 <bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" /> <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" /> <bean id="jsonViewResolver" class="org.springframework.web.servlet.view.json.MappingJacksonJsonView" /> <bean id="viewResolver" class="org.springframework.web.servlet.view.BeanNameViewResolver" /> <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"> <property name="messageConverters"> <list> <ref bean="jsonConverter" /> </list> </property> </bean> <bean id="jsonConverter" class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"> <property name="supportedMediaTypes" value="application/json" /> </bean> <bean name="UserController" class="com.tap.mvp.controller.UserController"/> 

My problem is how to make it work for a regular POST request. My controller should not accept an object of type JSON, instead, it should work for regular HTTP POST variables. How do I get values ​​from a query? What changes need to be made for this. I need to get rid of

 ObjectMapper mapper = new ObjectMapper(); CreateNewUserRequest request = mapper.readValue(payload, CreateNewUserRequest.class); 

and instead you need to add an instance creation method

CreateNewUserRequest

calling its constructor. To do this, I need to get the values ​​from the request. How can I do it? Can I handle the @RequestBody String payload as a map and get the values? or is there a specific way to get values ​​from an HTTP POST method request? the request will contain the following values:

firstName, lastName, email, password

+6
source share
2 answers

Here you mix two concepts. The REST service in Spring MVC is much more elegant as Spring handles JSON / XML for you:

 @RequestMapping( headers = {"content-type=application/json"}, method = RequestMethod.POST, value = "/user/register") @ResponseBody public CreateNewUserResponse addUser( @RequestBody CreateNewUserRequest request) { UserBusiness userBusiness = UserBusinessImpl.getInstance(); return userBusiness.createNewUser(request); } 

Check out the @ResponseBody annotation. You do not need any resolvers and manual JSON sorting. And you get XML (through JAXB) for free.

However, the data sent through the POST form is very different. I would suggest creating a second mapping to various types of media:

 @RequestMapping( headers = {"content-type=application/x-www-form-urlencoded"}, method = RequestMethod.POST, value = "/user/register") public ModelAndView addUser(@RequestParam("formParam1") String formParam1) { //... } 

With this configuration, REST calls with Content-type=application/json will be redirected to the first method and form POST requests to the second (at least, theoretically, have not tried). Note that there are easier ways to process form data in Spring than with the raw @RequestParam annotation; see Pass the request parameter in Spring MVC 3 .

+10
source

Another answer to the exact OP question is to set the content type of consumes to "text/plain" and then declare the input parameter @RequestBody String . This will pass the text of the POST data in the declared variable String ( postPayload in the following example).

Of course, this assumes that your POST payload is text data (as indicated in the OP).

Example:

  @RequestMapping(value = "/user/register", method = RequestMethod.POST, consumes = "text/plain") public ModelAndView addUser(@RequestBody String postPayload) { // ... } 
+1
source

Source: https://habr.com/ru/post/905645/


All Articles