0

我有个问题。我尝试使用 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/>

4

4 回答 4

1

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

@RequestMapping(value = "/books/manage", method = RequestMethod.POST,consumes="application/json")
于 2015-10-21T05:52:33.587 回答
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>
于 2015-10-21T06:05:57.180 回答
0

我想出了解决方案。问题出在 servlet xml 中:

<tx:annotation-driven/>

应该替换为

<mvc:annotation-driven/>

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

于 2015-10-21T08:09:18.333 回答
0

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

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

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

这里有一些类似的问题servlet 中的 <mvc:annotation-driven /> 和 <context:annotation-config /> 有什么区别?

于 2015-10-21T08:19:59.043 回答