Why is HttpEntity causing an HTTP Status 415 error in Spring?
This has been bugging me for days and I'm turning to the community for help. I've been trying to access the request body and headers using the HttpEntity as suggested by the Spring 3 docs. Every time I introduce the HttpEntity as a parameter, I always get the following error:
The server refused this request because the request entity is in a format not supported by the requested resource for the requested method ().
So, this works:
@RequestMapping("/handle")
public HttpEntity<String> handle() { //
HttpHeaders responseHeaders = new HttpHeaders();
responseHeaders.set("MyResponseHeader", "MyValue");
return new HttpEntity<String>("Hello World", responseHeaders);
}
But, this does not:
@RequestMapping("/handle")
public HttpEntity<String> handle(HttpEntity<String> requestEntity) { //
HttpHeaders responseHeaders = new HttpHeaders();
responseHeaders.set("MyResponseHeader", "MyValue");
return new HttpEntity<String>("Hello World", responseHeaders);
}
I'm not us开发者_开发百科ing <mvc:annotation-driven>
. I'm using good ol' <context:annotation-driven>
but I've tried adding to my config as suggested here without any luck. I've also dabbled with creating a bean post processor without any luck. I think I'm running out of ideas/Google searches.
Here's my current Spring config:
<?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" /-->
Thanks!
You need to send a POST request so that there is a body to parse into an HttpEntity
.
I would also recommend using the method
attribute of the RequestMapping
annotation so that you can specify which HTTP methods your mapping controller method handles.
@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);
}
I believe you could also do something like this to deal with the request body as a 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);
}
Have you tried adding the @ResponseBody annotation to your methods?
精彩评论