3

Spring 3 を使用して REST Remoting を実装しようとしていますが、「406 Not Acceptable」エラーを回避できません... アプリケーション/xml コンテンツを返す 1 つのサービスをリモートしようとしています。" " でリクエストを送信するたびに、Accept=application/xml406 エラーが発生します。異なる「Accept」ヘッダーを付けて送信するたびに、404 (および handleNoSuchRequestHandlingMethod 例外) が発生します。ログで確認できるように、サービス自体が呼び出されます。私が見つけたのは、サーブレットの初期化中に次のエラーが発生することですが、それが問題かどうかはわかりません。

委任する ViewResolvers が見つかりませんでした。ContentNegotiatingViewResolver の「viewResolvers」プロパティを使用して設定してください

さまざまな構成を試しましたが、成功しませんでした。たぶん、私の実装でエラーを見つけることができますか?

extService-servlet.xml

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

    <context:component-scan base-package="ch.epimmo.immogreen.backend.mvc" />

    <mvc:annotation-driven />

    <oxm:jaxb2-marshaller id="jaxbMarshaller">
        <oxm:class-to-be-bound name="ch.epimmo.immogreen.common.dto.ExpertDto" />
    </oxm:jaxb2-marshaller>


    <bean
        class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver"
        p:order="#{T(org.springframework.core.Ordered).HIGHEST_PRECEDENCE}">
        <property name="defaultContentType" value="text/html" />
        <!-- <property name="ignoreAcceptHeader" value="true" /> -->
        <property name="favorPathExtension" value="true" />
        <property name="mediaTypes">
            <map>
                <entry key="xml" value="application/xml" />
            </map>
        </property>
        <property name="defaultViews">
            <list>
                <bean class="org.springframework.web.servlet.view.xml.MarshallingView">
                    <property name="marshaller" ref="jaxbMarshaller" />
                </bean>
            </list>
        </property>
    </bean>
</beans>

コントローラ

@Controller
@RequestMapping( { "extService" })
public class ExtServiceController {

    protected final static Logger LOGGER = Logger.getLogger(ExtServiceController.class);

    @Autowired
    private UserManagementService userManagementService;    

    // @RequestMapping(value = "/experts", method = RequestMethod.GET, headers = { "Accept=*/*" })
    // @RequestMapping(value = "experts", method = RequestMethod.GET)
    @RequestMapping(value = "experts", method = RequestMethod.GET, headers = { "Accept=application/xml, text/xml" })
    public @ResponseBody
    ExpertDto getExperts() {            
              return new ExpertDto();
    }
}

テスト

DefaultHttpClient httpClient = new DefaultHttpClient();

String expertsUrl = "http://localhost:8080/extService/experts";

HttpGet getRequest = new HttpGet(expertsUrl);
getRequest.setHeader(new BasicHeader("Accept", "application/xml"));
// getRequest.setHeader(new BasicHeader("Accept", "text/html"));
// getRequest.setHeader(new BasicHeader("Accept", "*/*"));

HttpResponse response = httpClient.execute(getRequest);
HttpEntity entity = response.getEntity();
InputStream content = entity.getContent();
4

2 に答える 2

0

@RequestMapping アノテーションを次のように変更します

@RequestMapping(value = "experts", method = RequestMethod.GET, headers = { "Accept=application/xml" })

于 2013-12-08T11:42:11.517 に答える
0

@ResponseBody の代わりに次のアプローチを使用することを検討してください

@RequestMapping(value = "experts", method = RequestMethod.GET)
public @ResponseBody
void getExperts(org.springframework.ui.Model model) {            
      model.addAttribute(new ExpertDto());
}

「委譲するViewResolversが見つかりませんでした...」は単なる警告であり、無視できます。

于 2012-08-18T18:36:42.740 に答える