@RequestBody无法正常工作 - HTTP状态415

时间:2015-10-21 05:13:03

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

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

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

4 个答案:

答案 0 :(得分:1)

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

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

答案 1 :(得分:1)

这也可能是由于缺少消息转换器。尝试注册一个(杰克逊在你的情况下)。需要将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>

答案 2 :(得分:0)

我找到了解决方案。问题出在servlet xml:

<tx:annotation-driven/>

应替换为

<mvc:annotation-driven/>

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

答案 3 :(得分:0)

  

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

tx:annotation-driven - 用于启用@Transactional

等事务注释

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

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