为什么HttpEntity在Spring中导致HTTP状态415错误?

时间:2011-08-04 02:28:59

标签: spring spring-mvc

这已经困扰了我好几天,我正在寻求社区的帮助。我一直在尝试使用Spring 3 docs建议的HttpEntity访问请求正文和标题。每次我将HttpEntity作为参数引入时,我总是会收到以下错误:

  

服务器拒绝了此请求,因为请求实体所采用的方法所请求的资源不支持该格式()。

所以,这有效:

@RequestMapping("/handle")
public HttpEntity<String> handle() { //
    HttpHeaders responseHeaders = new HttpHeaders();
    responseHeaders.set("MyResponseHeader", "MyValue");
    return new HttpEntity<String>("Hello World", responseHeaders);
}

但是,这不是:

@RequestMapping("/handle")
public HttpEntity<String> handle(HttpEntity<String> requestEntity) { //
    HttpHeaders responseHeaders = new HttpHeaders();
    responseHeaders.set("MyResponseHeader", "MyValue");
    return new HttpEntity<String>("Hello World", responseHeaders);
}

我没有使用<mvc:annotation-driven>。我正在使用好的<context:annotation-driven>但是我已经尝试按照建议添加到我的配置here,但没有任何运气。我还涉及创建一个没有任何运气的bean后处理器。我想我的想法/谷歌搜索已经不多了。

这是我当前的Spring配置:

<?xml version="1.0" encoding="windows-1252"?>
<beans xmlns="http://www.springframework.org/schema/beans"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xmlns:p="http://www.springframework.org/schema/p"
   xmlns:aop="http://www.springframework.org/schema/aop"
   xmlns:tx="http://www.springframework.org/schema/tx"
   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-3.0.xsd
   http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
   http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
   http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
   http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd">

<context:annotation-config />
<context:component-scan base-package="com.gn" />
<tx:annotation-driven />

<bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
    <property name="basename" value="classpath:messages" />
    <property name="defaultEncoding" value="UTF-8" />
</bean>

<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"
    p:location="classpath:gn.properties" />

<!-- values come from resources/properties/jdbc.properties -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
    destroy-method="close"
    p:driverClassName="${jdbc.driverClassName}"
    p:url="${jdbc.databaseurl}"
    p:username="${jdbc.username}"
    p:password="${jdbc.password}" />

<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <property name="configLocation">
        <value>classpath:hibernate.cfg.xml</value>
    </property>
    <property name="configurationClass">
        <value>org.hibernate.cfg.AnnotationConfiguration</value>
    </property>
    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">${jdbc.dialect}</prop>
            <prop key="hibernate.show_sql">true</prop>
        </props>
    </property>
</bean>

<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
    <property name="sessionFactory" ref="sessionFactory" />
</bean>

<!--bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
    <property name="messageConverters">
        <util:list>
            <bean id="byteArrayMessageConverter" class="org.springframework.http.converter.ByteArrayHttpMessageConverter"/>
            <bean id="stringHttpMessageConverter" class="org.springframework.http.converter.StringHttpMessageConverter">
                <property name="supportedMediaTypes" value = "text/plain;charset=UTF-8" />
            </bean>
        </util:list>
    </property>
</bean-->

<!--bean id="encodingPostProcessor" class="com.glowpinion.core.postprocessor.EncodingPostProcessor" /-->

谢谢!

2 个答案:

答案 0 :(得分:0)

您是否尝试过将@ResponseBody注释添加到方法中?

答案 1 :(得分:0)

您需要发送一个POST请求,以便有一个正文可以解析为HttpEntity

我还建议使用method注释的RequestMapping属性,以便指定映射控制器方法处理的HTTP方法。

@RequestMapping(value = "/handle" method = RequestMethod.POST)
public HttpEntity<String> handle(HttpEntity<String> requestEntity) {
    HttpHeaders responseHeaders = new HttpHeaders();
    responseHeaders.set("MyResponseHeader", "MyValue");
    return new HttpEntity<String>("Hello World", responseHeaders);
}

我相信你也可以做这样的事情来处理请求体String

@RequestMapping(value = "/handle" method = RequestMethod.POST)
public String handle(@RequestBody String body) {
    HttpHeaders responseHeaders = new HttpHeaders();
    responseHeaders.set("MyResponseHeader", "MyValue");
    return new HttpEntity<String>("Hello World", responseHeaders);
}