【问题标题】:@RequestBody not working - HTTP Status 415@RequestBody 不工作 - HTTP 状态 415
【发布时间】:2016-01-19 23:36:44
【问题描述】:

我有问题。我尝试使用 angularjs 将 http post 发送到我的控制器。

@RequestMapping(value = "/books/manage", method = RequestMethod.POST)
@ResponseBody
public void manageBooks(@RequestBody final BooksDTO dto)
        throws SystemException, IOException {
    System.out.println("DTO WAS SEND!");
    }
}

这里是 Angularjs

$http.post($scope.BooksUrl, {
                        'title':Title,
                        'booksUrl':Url,
                        'number':Number
                }).error(function (response) {
                    // error message
                }).then(function(){
                    // success message
                });

标题是

Accept:application/json, text/plain, */*
Accept-Encoding:gzip, deflate
Accept-Language:en-US,en;q=0.8,ru;q=0.6
Connection:keep-alive
Content-Length:72
Content-Type:application/json;charset=UTF-8

但它捕获“HTTP - 415 状态。服务器拒绝此请求,因为请求实体的格式不受所请求方法的请求资源支持。”我该如何解决?

响应标题

Content-Length:1048
Content-Type:text/html;charset=utf-8
Date:Wed, 21 Oct 2015 06:00:39 GMT
Server:Apache-Coyote/1.1
X-Content-Type-Options:nosniff
X-Frame-Options:SAMEORIGIN
X-XSS-Protection:1

pom.xml

 <dependency>
        <groupId>com.liferay.portal</groupId>
        <artifactId>portal-service</artifactId>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>com.liferay.portal</groupId>
        <artifactId>util-bridges</artifactId>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>com.liferay.portal</groupId>
        <artifactId>util-taglib</artifactId>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>com.liferay.portal</groupId>
        <artifactId>util-java</artifactId>
        <scope>provided</scope>
    </dependency>

    <dependency>
        <groupId>javax.portlet</groupId>
        <artifactId>portlet-api</artifactId>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>servlet-api</artifactId>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>javax.servlet.jsp</groupId>
        <artifactId>jsp-api</artifactId>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>jstl</artifactId>
    </dependency>

    <!--Spring dependencies-->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-aop</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-beans</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-core</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-expression</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc-portlet</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-tx</artifactId>
        <version>3.2.4.RELEASE</version>
    </dependency>

    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-databind</artifactId>
    </dependency>

servlet.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:tx="http://www.springframework.org/schema/tx"   xmlns="http://www.springframework.org/schema/beans"   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"    
   xmlns:context="http://www.springframework.org/schema/context"    xmlns:util="http://www.springframework.org/schema/util"
   xsi:schemaLocation="
http://www.springframework.org/schema/beans    http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/util    http://www.springframework.org/schema/util/spring-util-2.0.xsd
http://www.springframework.org/schema/tx   http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">

<context:component-scan base-package="com.example.books.**"/>
<tx:annotation-driven/>

【问题讨论】:

    标签: angularjs spring http spring-restcontroller http-status-code-415


    【解决方案1】:

    尝试在您的控制器中使用以下代码

    @RequestMapping(value = "/books/manage", method = RequestMethod.POST,consumes="application/json")
    

    【讨论】:

      【解决方案2】:

      这也可能是由于缺少消息转换器。尝试注册一个(在您的情况下为杰克逊)。需要将 java 对象转换为 JSON,这是由 JSON 消息转换器完成的。如果如果使用 @EnableWebMVC 或 mvc:annotation-driven 标记(在 xml 配置的情况下)并且 jackson 被添加到您的类路径中,则 MappingJacksonHttpMessageConverter 被隐式​​添加。查看您的 pom 是否具有 Jackson 依赖项,否则添加以下内容:

      <dependency>
         <groupId>com.fasterxml.jackson.core</groupId>
         <artifactId>jackson-databind</artifactId>
         <version>2.4.6</version>
      </dependency>
      

      【讨论】:

      • 你能描述一下你的意思吗?
      • Aww,你说得对,我忘了在杰克逊数据绑定中添加 maven 依赖。稍后我会尝试回复你
      • 将 jackson-databind 添加到 pom.xml 也没有帮助
      • 你能把你的配置和pom贴在这里吗?这可能会有所帮助。
      • 我忘了补充说我正在使用 Liferay。我尝试委托 servlet 进行休息服务
      【解决方案3】:

      我想出了解决办法。问题出在 servlet xml 中:

      <tx:annotation-driven/>
      

      应该替换为

      <mvc:annotation-driven/>
      

      我不知道有什么区别,但它有效(有人可以解释一下吗?) 谢谢大家的回答。

      【讨论】:

        【解决方案4】:

        我不知道有什么区别,但它有效(有人可以解释吗?)谢谢大家的回答。

        tx:annotation-driven - 用于启用事务注释,如@Transactional

        mvc:annotation-driven - 用于启用 Spring MVC 注释,如 @Controller

        这里有一些类似的问题What's the difference between <mvc:annotation-driven /> and <context:annotation-config /> in servlet?

        【讨论】:

          猜你喜欢
          • 2012-03-03
          • 1970-01-01
          • 2011-08-02
          • 1970-01-01
          • 2013-01-22
          • 2013-08-10
          • 2018-05-31
          • 1970-01-01
          • 2016-02-01
          相关资源
          最近更新 更多