2011-08-04 3 views
1

이것은 며칠 동안 나를 괴롭 히고 있으며 도움을 청하는 커뮤니티로 향하고 있습니다. Spring 3 docs에서 제안한대로 HttpEntity를 사용하여 요청 본문과 헤더에 액세스하려고했습니다. 이하지 않는,HttpEntity가 Spring에서 HTTP 상태 415 오류를 일으키는 이유는 무엇입니까?

@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); 
} 

I를

The server refused this request because the request entity is in a format not supported by the requested resource for the requested method().

그래서,이 작품 : 나는 매개 변수로 HttpEntity를 소개 할 때마다, 나는 항상 다음과 같은 오류가 <mvc:annotation-driven>을 사용하고 있지 않습니다. 나는 좋은 ol을 사용하고있다 '<context:annotation-driven>하지만 제안 된대로 내 설정을 추가하려고 시도했습니다 here 행운없이. 나는 행운이없이 빈 포스트 프로세서를 만드는 것으로도 손을 씻었다. 나는 아이디어/Google 검색이 부족하다고 생각합니다.

<?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" /--> 

감사 :

여기에 내 현재의 봄 설정입니다!

+1

어떤 HTTP 방법 당신은 당신의 요청을 보낼 때 사용하고 있습니까? 나는 그것이 여기에서 일하기위한 POST 일 필요가 있다고 생각한다. –

+0

@ nicholas.hauschild 직선 수행하기 GET here. 양식을 통해 URL에 제출하는 제안 된 POST 방법을 시도했지만 여전히 동일한 오류가 발생합니다. – rayseaward

+0

글쎄, 나는 GET이 어떤 방식 으로든 작동한다고 생각하지 않는다. 엔티티가 할당되어 있지 않기 때문이다. 보고있는 코드에 예외를 게시 할 수 있습니까? –

답변

0

HttpEntity으로 파싱 할 본문이 있도록 POST 요청을 보내야합니다.

지도 컨트롤러 메소드가 처리하는 HTTP 메소드를 지정할 수 있도록 RequestMapping 주석의 method 속성을 사용하는 것이 좋습니다.

@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 A와 요청 본문을 처리하기 위해이 같은 일을 할 수 있으리라 생각합니다 :

@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); 
} 
0

메소드에 @ResponseBody 주석을 추가해 보았습니까?

+0

예. 같은 오류가 발생합니다. – rayseaward

관련 문제