我们如何在Spring 3.0.2中设置请求内容类型?

时间:2015-06-03 17:42:06

标签: java json spring spring-mvc soap

我的代码就像:

 @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中的以下步骤点击发布请求。

  1. Open Postman。
  2. 在网址栏http://localhost:8080/walley/login中输入网址。
  3. 单击Headers按钮并输入Content-Type作为标题和application / json in value。
  4. 从URL文本框旁边的下拉列表中选择POST。
  5. 从URL文本框下方的按钮中选择原始。
  6. 从以下下拉列表中选择JSON。 在下面提供的textarea中,发布您的请求对象: { “userName”:“test”, “密码”:“某人” }
  7. 作为回应我收到错误:

    org.springframework.web.HttpMediaTypeNotSupportedException:Content type 'application/json' not supported
    

    我检查过,我在Spring 3.1.X或3.2.X中找到了我们可以在@RequestMapping中设置内容类型“使用者和生产者”以获取请求并且它有效但在3.0.2中它们不支持“消费者和生产者”。那么如何使用@RequestBody注释在Spring 3.0.2版本中设置请求内容类型呢?

1 个答案:

答案 0 :(得分:2)

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

的pom.xml

$array = array('item1','item2','item3','item4','item5','item6','item7','item8','item9','item10');
$i = 1;
$j = 1;
echo $div = '<div class="group'.$j.'">';
foreach ($array as $value) {
    if($i==1){
        echo $value.'<br/>';
        echo '</div>';
    }elseif($i%4==2){
        $j++;
        echo '</div>

        <div class="group'.$j.'">';
        echo $value.'<br/>';
    }else{
        echo $value.'<br/>';
    }
    $i++;
}

调度-servlet.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>

应该喜欢控制器

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