我的Spring应用程序配置为使用GET参数进行内容协商。代码是Kotlin,但在Java中也是如此。
配置:
override fun configureContentNegotiation(configurer: ContentNegotiationConfigurer) { configurer.favorParameter(true) .parameterName("format") .ignoreAcceptHeader(false) .defaultContentType(MediaType.APPLICATION_JSON) .mediaType("text/plain", MediaType.TEXT_PLAIN) .mediaType("application/json", MediaType.APPLICATION_JSON) .mediaType("application/rdf+xml", MediaType("application", "rdf+xml")) }
以及下列控制器方法:
@GetMapping("/test", produces=["text/plain"]) fun testText() : String { return "Hello" @GetMapping("/test", produces=["application/json"]) fun testJson() : Map<String, String> { return mapOf("hello" to "world") @GetMapping("/test", produces=["application/rdf+xml"]) fun testRdf(response: HttpServletResponse) { // dummy response, to demonstrate using output stream. response.setContentType("application/rdf+xml")