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