RequestBody的REST应用程序 [英] RequestBody of a REST application

查看:91
本文介绍了RequestBody的REST应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对SpringMVC REST概念不熟悉。需要专家的帮助来理解/解决以下问题,
我开发了一个SpringMVC应用程序,以下是控制器类代码的一部分,它的工作方式完全正常,这意味着它可以正常工作JSON类型对象,

Iam bit new to SpringMVC REST concept. Need a help from experts here to understand/ resolve following issue, I have developed a SpringMVC application, following is a part of a controller class code, and it works perfectly ok with the way it is, meaning it works ok with the JSON type object,

@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);

这是我的rest-servlet.xml看起来像

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"/>

我的问题是如何让它适用于正常的POST请求,我的控制器不应该接受JSON类型而不是它应该适用于普通的HTTP POST变量。
如何从请求中获取值?我应该为此做些什么修改。我需要摆脱

My problem is how do i make it work for normal POST request, My controller should not accept JSON type object instead it should work for normal HTTP POST variables. How do i get values from the request?What are the modifications should i need to be done for that. I need to get rid of

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

而是需要添加方法来创建

and instead need to add way to create an instance of


CreateNewUserRequest

CreateNewUserRequest

class,通过调用其构造函数。为此我需要从请求中获取值。我怎么做?我可以将 @RequestBody字符串有效负载视为地图并获取值吗?或者是否有一种特定的方法从HTTP POST方法的请求中获取值?以下值将在请求中,

class, by invoking its constructor. For that i need to get values from request. How do i do that? Can i treat @RequestBody String payload as a map and get the values? or is there a specific way to get values from the request of HTTP POST method? following values will be in the request,


firstName,lastName,email,password

firstName, lastName, email,password


推荐答案

这里混合了两个概念。 Spring MVC中的REST服务更优雅,因为Spring为您处理JSON / XML编组:

You are mixing two concepts here. The REST service in Spring MVC is much more elegant as Spring handles JSON/XML marshalling 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);
}

注意 @ResponseBody 注释。您不需要任何视图解析器和手动JSON编组。你可以免费获得XML(通过JAXB)。

Notice the @ResponseBody annotation. You don't need any view resolvers and manual JSON marshalling. And you get XML (via JAXB) for free.

然而,通过表单POST发送的数据是非常不同的。我建议创建第二个映射处理不同的媒体类型:

However data sent via form POST is very different. I would suggest creating second mapping handling different media type:

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

使用此配置REST调用 Content-type = application / json 将被路由到第一个方法并向第二个方法形成POST请求(至少在理论上,没有尝试过)。请注意,与原始 @RequestParam 注释相比,Spring中处理表单数据的方法更简单,请参阅:在Spring MVC 3中传递请求参数

With this configuration REST calls with Content-type=application/json will be routed to first method and form POST requests to the second one (at least in theory, haven't tried it). Note that there are simpler ways to handle form data in Spring compared to raw @RequestParam annotation, see: Pass a request parameter in Spring MVC 3.

这篇关于RequestBody的REST应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆