【问题标题】:How can we set request content type in Spring 3.0.2?我们如何在 Spring 3.0.2 中设置请求内容类型?
【发布时间】:2015-08-18 01:49:27
【问题描述】:

我的代码是这样的:

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

在邮递员中使用以下步骤点击发布请求。

  1. 打开邮递员。
  2. 在网址栏中输入网址http://localhost:8080/walley/login
  3. 单击 Headers 按钮并输入 Content-Type 作为 header 并在 value 中输入 application/json。
  4. 从 URL 文本框旁边的下拉列表中选择 POST。
  5. 从 URL 文本框下方的可用按钮中选择 raw。
  6. 从以下下拉列表中选择 JSON。 在下面可用的文本区域中,发布您的请求对象: { “用户名”:“测试”, “密码”:“某人” }

作为回应,我收到了错误:

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

我检查并发现在 Spring 3.1.X 或 3.2.X 中我们可以在 @RequestMapping 中为请求设置内容类型“消费者和生产者”,它可以工作,但在 3.0.2 中它们不支持“消费者和生产者”。那么如何在 Spring 3.0.2 版本中使用 @RequestBody 注解设置请求内容类型呢?

【问题讨论】:

  • 您的方法没有阻塞任何东西,因此每个请求都转到该方法。确保你的类路径中有一个 JSON 库,比如 Jackson。
  • 尝试使用@RequestBody 作为第一个参数 i.s.o。最后。

标签: java json spring spring-mvc soap


【解决方案1】:

我们在 Spring 3.0.2 版本中没有“消费者和生产者”,所以我们需要在根 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>

dispatch-servlet.xml

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

【讨论】:

    猜你喜欢
    • 2018-05-26
    • 2011-07-24
    • 2012-04-07
    • 1970-01-01
    • 2017-07-17
    • 1970-01-01
    • 1970-01-01
    • 2021-05-16
    • 1970-01-01
    相关资源
    最近更新 更多