5

次のように単純なプレーンテキスト文字列を返したい:

@RestController 
@RequestMapping("/test")
public class TestController {
    @ResponseStatus(HttpStatus.OK)
    @RequestMapping(value = "/my", method = RequestMethod.GET, produces="text/plain")
    public String test() {
        return "OK";
    }

問題: 次のようなグローバル ContentNegotiation フィルターもあります。

@Configuration
public class ContentNegotiationAdapter extends WebMvcConfigurerAdapter {
    @Override
    public void configureContentNegotiation(ContentNegotiationConfigurer configurer) {
        configurer.favorPathExtension(false)
                .favorParameter(true)
                .ignoreAcceptHeader(true)
                .useJaf(false)
                .defaultContentType(MediaType.APPLICATION_XML);
    }

    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        super.configureMessageConverters(converters);
    }
}

結果: スプリング コントローラーにアクセスするたびに、次のエラーが発生します。

Could not find acceptable representation

質問: コンテンツ ネゴシエーション (保持する必要がある) 内で XML のみが構成されている場合でも、コントローラーがプレーン テキストを返すように強制するにはどうすればよいですか?

4

3 に答える 3

0

よく思い出せば、@ResponseBody でメソッドにアノテーションを付けると、プレーン テキストが返されます。

これをチェックして

于 2015-10-29T13:06:14.053 に答える
-2

@ResponseBody次のようなテスト メソッドの注釈を指定する必要があります 。

public @ResponseBody String test(){}
于 2015-10-29T13:36:28.143 に答える