Java 如何在Spring3.0.2中设置请求内容类型?

Java 如何在Spring3.0.2中设置请求内容类型?,java,json,spring,spring-mvc,soap,Java,Json,Spring,Spring Mvc,Soap,我的代码如下: @Controller @RequestMapping( value = "/walley/login", method = RequestMethod.POST) public void login(HttpServletRequest request, HttpServletResponse response, @RequestBody RequestDTO requestDTO) th

我的代码如下:

 @Controller
    @RequestMapping( value = "/walley/login", method = RequestMethod.POST)
    public void login(HttpServletRequest request,
            HttpServletResponse response,
            @RequestBody RequestDTO requestDTO)
            throws IOException, ServiceException {
              String userName = requestDTO.getUserName();
            String password = requestDTO.getPassword();
            System.out.println("userName " + userName +" :: password "+        password);}
RequestDTO.java文件

public class RequestDTO {
    public String userName;
    public String password;

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }
    }
在postman中按以下步骤点击post请求

  • 打开邮递员
  • 在URL栏中输入URL
  • 单击Headers按钮并在value中输入内容类型作为header和application/json
  • 从URL文本框旁边的下拉列表中选择POST
  • 从URL文本框下方的可用按钮中选择raw
  • 从下面的下拉列表中选择JSON。 在下面可用的文本区域中,发布您的请求对象: { “用户名”:“测试”, “密码”:“某人” }
  • 作为回应,我得到了一个错误:

    org.springframework.web.HttpMediaTypeNotSupportedException:Content type 'application/json' not supported
    
    我检查了一下,发现在Spring3.1.X或3.2.X中,我们可以在@RequestMapping中为请求设置内容类型“consumers and producers”,它可以工作,但在3.0.2中,它们不支持“consumers and producers”。那么,我们如何在Spring3.0.2版本中使用@RequestBody注释设置请求内容类型呢

    Spring 3.0.2版没有“消费者和生产者”,因此我们需要在root pom.xml和dispatch-servlet.xml中添加一些额外的条目,以使用@RequestBody注释

    pom.xml

      <!-- jackson -->
          <dependency>
               <groupId>org.codehaus.jackson</groupId>
               <artifactId>jackson-mapper-asl</artifactId>
           <version>1.9.13</version>
          </dependency>
          <dependency>
               <groupId>org.codehaus.jackson</groupId>
               <artifactId>jackson-core-asl</artifactId>
               <version>1.9.13</version>
          </dependency>
    

    您的方法没有阻止任何内容,因此每个请求都会转到该方法。确保类路径中有一个类似Jackson的JSON库。尝试将@RequestBody作为第一个参数i.s.o.最后一个参数。
    <bean id="jacksonMessageConverter"
        class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"></bean>
        <bean
            class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
            <property name="messageConverters">
                <list>
                    <ref bean="jacksonMessageConverter" />
                </list>
        </property>
        </bean>
    
    @Controller
        @RequestMapping( value = "/walley/login", method = RequestMethod.POST)
        public void login(@RequestBody RequestDTO requestDTO,
                HttpServletRequest request, HttpServletResponse response)
                throws IOException, ServiceException {
                  String userName = requestDTO.getUserName();
                String password = requestDTO.getPassword();
                System.out.println("userName " + userName +" :: password "+   password);}