Java 如何在Spring引导服务中为端点指定默认媒体类型
我正在构建一个具有多个端点的Spring引导服务。我的服务需要同时支持Java 如何在Spring引导服务中为端点指定默认媒体类型,java,spring,spring-boot,Java,Spring,Spring Boot,我正在构建一个具有多个端点的Spring引导服务。我的服务需要同时支持json和xml输出。大多数端点将仅为json,一些端点将仅为xml。我可以使用注释@RequestMapping指定特定端点接受或返回的内容类型。例如: @RequestMapping(method = RequestMethod.POST, consumes = {MediaType.APPLICATION_XML_VALUE}, produces = {Me
json
和xml
输出。大多数端点将仅为json
,一些端点将仅为xml
。我可以使用注释@RequestMapping
指定特定端点接受或返回的内容类型。例如:
@RequestMapping(method = RequestMethod.POST,
consumes = {MediaType.APPLICATION_XML_VALUE},
produces = {MediaType.APPLICATION_XML_VALUE})
然而,由于我的应用程序的大多数端点将仅是json
,因此我希望避免编写
consumes = {MediaType.APPLICATION_JSON_VALUE},
produces = {MediaType.APPLICATION_JSON_VALUE}
在所有这些方面。有没有办法使带有@RequestMapping
注释的方法具有默认的消耗
和产生
媒体类型?每当我需要与默认值不同的内容时,我都可以指定它
我尝试过建立内容协商,但不适用于此。我认为我可能可以通过与自定义的ContentNegotiationStrategy
进行内容协商来实现这一点,但我需要该代码能够读取该请求的处理程序的注释(使用@RequestMapping
注释的特定方法),并且该代码只获得一个NativeWebRequest
是否有一个全局Spring配置来实现这一点
编辑:
设置内容协商
@Configuration
@EnableWebMvc
class ContentNegotiationConfiguration extends WebMvcConfigurerAdapter {
@Override
public void configureContentNegotiation(ContentNegotiationConfigurer configurer) {
configurer.favorParameter(false)
.favorPathExtension(true)
.ignoreAcceptHeader(true)
.ignoreUnknownPathExtensions(false)
.useJaf(false)
.defaultContentType(MediaType.APPLICATION_JSON);
}
}
并以
@RequestMapping(method = RequestMethod.GET)
然后调用端点
GET https://localhost:8080/endpoint.xml
返回
xml
输出和HTTP200
而不是HTTP406
查看ContentNegotiationConfigurer它允许您为整个应用程序指定内容类型。请参阅以下问题:将此bean添加到您的配置中:
@EnableWebMvc
@Configuration
@ComponentScan
public class MyWebConfig extends WebMvcConfigurerAdapter {
@Override
public void configureContentNegotiation (ContentNegotiationConfigurer configurer) {
configurer.defaultContentType(MediaType.APPLICATION_JSON);
}
}
请使用此Java配置
public void configureContentNegotiation(ContentNegotiationConfigurer configurer) {
configurer.favorPathExtension(true).
favorParameter(false).
parameterName("mediaType").
ignoreAcceptHeader(false).
useJaf(false).
defaultContentType(MediaType.APPLICATION_JSON).
mediaType("xml", MediaType.APPLICATION_XML).
mediaType("json", MediaType.APPLICATION_JSON);
}
这完全是关于您返回的内容类型。如果您这样定义内容协商:
@Override
public void configureContentNegotiation(ContentNegotiationConfigurer configurer) {
configurer
.favorPathExtension(false)
.favorParameter(true)
.mediaType("json", MediaType.APPLICATION_JSON)
.mediaType("xml", MediaType.APPLICATION_XML);
}
然后,您可以使用一种方法来处理不同的内容类型,例如:
@RequestMapping(value = "/process/{json}", method = RequestMethod.GET)
public ResponseEntity<?> process(@PathVariable("json") boolean processJson) {
if (processJson) {
final HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
return new ResponseEntity<>("someJSONObject", headers, HttpStatus.OK);
} else {
final HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_XML);
return new ResponseEntity<>("someXMLObject", headers, HttpStatus.OK);
}
}
@RequestMapping(value=“/process/{json}”,method=RequestMethod.GET)
公共响应属性进程(@PathVariable(“json”)布尔进程json){
if(processJson){
最终HttpHeaders=新HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
返回新的ResponseEntity(“someJSONObject”,标题,HttpStatus.OK);
}否则{
最终HttpHeaders=新HttpHeaders();
headers.setContentType(MediaType.APPLICATION\uXML);
返回新的ResponseEntity(“someXMLObject”,headers,HttpStatus.OK);
}
}
如果需要将对象直接传递给响应,以便正确序列化对象,那么还需要使用消息转换器。例如:
@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
converters.add(new MappingJackson2HttpMessageConverter(objectMapper));
converters.add(new MappingJackson2XmlHttpMessageConverter(objectMapper));
}
@覆盖
public void configureMessageConverters(列表不符合您的想法。我将提供示例。编辑不会更改初始问题,该问题明确表示内容协商不会满足我的要求:)。两个人独立地将您指向ContentNegotiationConfigurer。我现在出去了。