2011-08-04 9 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>を使用していません。私は良いオール '<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を実行する。私は提案されたPOSTアプローチを試しました(フォームを通じてURLに送信してください)、私はまだ同じエラーが発生しています。 – rayseaward

+0

さて、GETはどちらの方法でも動作しません。エンティティが割り当てられていないためです。あなたが見ているコードに例外を投稿できますか? –

答えて

0

HttpEntityに解析する本文があるようにPOSTリクエストを送信する必要があります。

RequestMappingアノテーションのmethod属性を使用して、マッピングコントローラメソッドが処理する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); 
} 
0

メソッドに@ResponseBodyアノテーションを追加しようとしましたか?

+0

はい。同じエラーがスローされます。 – rayseaward

関連する問題