多个场景@RequestMapping与Accept或ResponseEntity一起生成JSON/XML

Man*_*dan 36 rest spring spring-mvc spring-3 spring-4

我正在使用Spring 4.0.7

关于Spring MVC,出于研究目的,我有以下内容:

@RequestMapping(value="/getjsonperson", 
                method=RequestMethod.GET, 
                produces=MediaType.APPLICATION_JSON_VALUE)
public @ResponseBody Person getJSONPerson(){
    logger.info("getJSONPerson - getjsonperson");
    return PersonFactory.createPerson();
}

@RequestMapping(value="/getperson.json", method=RequestMethod.GET)
public @ResponseBody Person getPersonJSON(){
    logger.info("getPerson - getpersonJSON");
    return PersonFactory.createPerson();
}
Run Code Online (Sandbox Code Playgroud)

每个都工作正常,观察JSON,有和没有扩展:

  • / getjsonperson
  • /getperson.json

对于XML也是如此

@RequestMapping(value="/getxmlperson",
                method=RequestMethod.GET,
                produces=MediaType.APPLICATION_XML_VALUE
                )
public @ResponseBody Person getXMLPerson(){
    logger.info("getXMLPerson - getxmlperson");
    return PersonFactory.createPerson();
}

@RequestMapping(value="/getperson.xml", method=RequestMethod.GET)
@ResponseBody
public Person getPersonXML(){
    logger.info("getPerson - getpersonXML");
    return PersonFactory.createPerson();
}
Run Code Online (Sandbox Code Playgroud)

每个都工作正常,观察XML,有和没有扩展:

  • / getxmlperson
  • /getperson.xml

现在关于Restful我有以下内容:

@RequestMapping(value="/person/{id}/", 
                method=RequestMethod.GET,
                produces={MediaType.APPLICATION_JSON_VALUE, 
                          MediaType.APPLICATION_XML_VALUE})
public ResponseEntity<Person> getPersonCustomizedRestrict(@PathVariable Integer id){
    Person person = personMapRepository.findPerson(id);
    return new ResponseEntity<>(person, HttpStatus.FOUND);//302     
}
Run Code Online (Sandbox Code Playgroud)

MediaType对于JSON和XML,观察它是混合的

通过RestTemplate我可以指出Accept

    if(type.equals("JSON")){
        logger.info("JSON");
        headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));
    }
    else if(type.equals("XML")){
        logger.info("XML");
        headers.setAccept(Arrays.asList(MediaType.APPLICATION_XML));
    }
    ….

    ResponseEntity<Person> response =
                restTemplate.exchange("http://localhost:8080/spring-utility/person/{id}/customizedrestrict",
                                      HttpMethod.GET,
                                      new HttpEntity<Person>(headers),  
                                      Person.class,
                                       id
                                     ); 
Run Code Online (Sandbox Code Playgroud)

在此之前,我可以使用一个URL/URI来获取XML或JSON格式的数据.它工作正常

我的问题是Spring MVC ......只是考虑一下

@RequestMapping(value="/{id}/person", 
                method=RequestMethod.GET,
                produces={MediaType.APPLICATION_JSON_VALUE,  
                          MediaType.APPLICATION_XML_VALUE})
public @ResponseBody Person getPerson(@PathVariable Integer id){
    return personMapRepository.findPerson(id);
}
Run Code Online (Sandbox Code Playgroud)

我可以@RequestMapping通过以下方式调用或激活该处理程序方法():

  1. jQuery使用Ajax,我能够指示Accept值(例如JSON)
  2. 海报,通过Headers按钮,我可以设置Accept

问题一:

但是对于一个共同的链接?我怎么设置Accept价值?有可能吗?

我想以其他方式解决这个问题.

  • http://localhost:8080/spring-utility/person/getpersonformat?format=json
  • http://localhost:8080/spring-utility/person/getpersonformat?format=xml

注意:

  • ?format

因此

@RequestMapping(value="/getpersonformat", 
                method=RequestMethod.GET,
                produces={MediaType.APPLICATION_JSON_VALUE,  
                          MediaType.APPLICATION_XML_VALUE})
public @ResponseBody Person getPerson(@RequestParam String format){
    return personMapRepository.findPerson(id);
}
Run Code Online (Sandbox Code Playgroud)

问题二:

必须添加上面显示的方法的代码来自定义返回类型格式?我的意思是,JSON或XML,有可能吗?

我想在下面:

@RequestMapping(value="/getpersonformataltern",
        method=RequestMethod.GET
        produces={MediaType.APPLICATION_JSON_VALUE, 
                  MediaType.APPLICATION_XML_VALUE}
        )
public ResponseEntity<Person> getPersonFormat(@RequestParam String format){
    logger.info("getPersonFormat - format: {}", format);
    HttpHeaders httpHeaders = new HttpHeaders();
    if(format.equals("json")){
        logger.info("Ok JSON");
        httpHeaders.setContentType(MediaType.APPLICATION_JSON);
    }
    else{
        logger.info("Ok XML");
        httpHeaders.setContentType(MediaType.APPLICATION_XML);
    }
    return new ResponseEntity<>(PersonFactory.createPerson(), httpHeaders, HttpStatus.OK);
}
Run Code Online (Sandbox Code Playgroud)

但:

如果我执行URL:

  • http://localhost:8080/spring-utility/person/getpersonformataltern?format=json

我明白了

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<person>
    <id>1</id>
    <firstName>Manuel</firstName>
    <lastName>Jordan</lastName>
…
</person>
Run Code Online (Sandbox Code Playgroud)

是的XML!

注意:我可以确认控制台打印Ok JSON

如果我执行URL:

  • http://localhost:8080/spring-utility/person/getpersonformataltern?format=xml

我明白了

This XML file does not appear to have any style information associated with it. 
The document tree is shown below.

<person>
    <id>1</id>
    <firstName>Manuel</firstName>
    <lastName>Jordan</lastName> 
    …
</person>
Run Code Online (Sandbox Code Playgroud)

问题三

必须添加上面显示的方法的哪些代码才能修复JSON输出?我不知道出了什么问题或遗失了......

有三个问题.

谢谢

Α

@Override
public void configureContentNegotiation(ContentNegotiationConfigurer configurer) {
    Map<String,MediaType> mediaTypes = new LinkedHashMap<>();
    mediaTypes.put("json", MediaType.APPLICATION_JSON);
    mediaTypes.put("xml", MediaType.APPLICATION_XML);
    configurer.mediaTypes(mediaTypes);
    configurer.defaultContentType(MediaType.TEXT_HTML);
}
Run Code Online (Sandbox Code Playgroud)

Edd*_*dez 37

使用Accept标头很容易从REST服务获取格式json或xml.

这是我的控制器,看看生产部分.

@RequestMapping(value = "properties", produces = {MediaType.APPLICATION_JSON_VALUE, MediaType.APPLICATION_XML_VALUE}, method = RequestMethod.GET)
    public UIProperty getProperties() {
        return uiProperty;
    }
Run Code Online (Sandbox Code Playgroud)

为了使用REST服务,我们可以使用下面的代码,其中header可以是MediaType.APPLICATION_JSON_VALUE或MediaType.APPLICATION_XML_VALUE

HttpHeaders headers = new HttpHeaders();
headers.add("Accept", header);

HttpEntity entity = new HttpEntity(headers);

RestTemplate restTemplate = new RestTemplate();
ResponseEntity<String> response = restTemplate.exchange("http://localhost:8080/properties", HttpMethod.GET, entity,String.class);
return response.getBody();
Run Code Online (Sandbox Code Playgroud)

编辑01:

要使用application/xml,请添加此依赖项

<dependency>
    <groupId>com.fasterxml.jackson.dataformat</groupId>
    <artifactId>jackson-dataformat-xml</artifactId>
</dependency>
Run Code Online (Sandbox Code Playgroud)

  • 节省我的时间!忘记在pom中添加`jackson-dataformat-xml` (2认同)