Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/306.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在Spring引导服务中为端点指定默认媒体类型_Java_Spring_Spring Boot - Fatal编程技术网

Java 如何在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

我正在构建一个具有多个端点的Spring引导服务。我的服务需要同时支持
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
输出和HTTP
200
而不是HTTP
406

查看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。我现在出去了。