Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/13.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/5/spring-mvc/2.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
Spring 行为@RequestMapping';s通过RestTemplate和Web浏览器生成_Spring_Spring Mvc_Spring 3_Spring 4 - Fatal编程技术网

Spring 行为@RequestMapping';s通过RestTemplate和Web浏览器生成

Spring 行为@RequestMapping';s通过RestTemplate和Web浏览器生成,spring,spring-mvc,spring-3,spring-4,Spring,Spring Mvc,Spring 3,Spring 4,我正在使用Spring4.0.7 我有一个用JSON和XML表示的实体 @Entity @Table(name="person") @XmlRootElement(name="person") @XmlType(propOrder = {"id",…,"address"}) public class Person implements Serializable { 我有以下方法: @RequestMapping(value="/{id}/customized",

我正在使用Spring4.0.7

我有一个用JSON和XML表示的实体

@Entity
@Table(name="person")
@XmlRootElement(name="person")
@XmlType(propOrder = {"id",…,"address"})
public class Person implements Serializable {
我有以下方法:

@RequestMapping(value="/{id}/customized", 
                method=RequestMethod.GET,
                produces={MediaType.APPLICATION_JSON_VALUE, MediaType.APPLICATION_XML_VALUE})
public ResponseEntity<Person> getPersonCustomized(@PathVariable Integer id){
    logger.info("PersonRestResponseEntityController  - getPersonCustomized - id: {}", id);
    Person person = personMapRepository.findPerson(id);
    return new ResponseEntity<>(person, HttpStatus.FOUND);//302     
}
它有一个
列表
,所以我决定测试和播放

如何看穿上面显示的代码,我有两个场景

  • headers.setAccept(Arrays.asList(MediaType.APPLICATION_XML,MediaType.APPLICATION_JSON))
  • headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON,MediaType.APPLICATION_XML))
每个人代表的地方

  • Accept=application/xml,application/json
  • Accept=application/json,application/xml
通过RestTemplate,我总能看到Person对象已经被转换或转换,因此没有什么新的东西

所以,只是玩玩而已

如果我设置headers值

  • Accept=application/xml,application/json
    然后响应是xml格式的
  • Accept=application/json,application/xml
    然后响应是json格式的
因此,我假设第二个被忽略

如果我是对的,那么

  • setAccept(列出acceptableMediaTypes)
    的意义是什么? 我的意思是发送一个
    列表
  • 如果我们发送列表,哪些案例有意义、有用或应该有效
  • 当我们发送接受列表时,预期的行为是什么

  • 文件说明了这一切:

    设置可接受的媒体类型列表

    因此,如果您将XML和JSON作为可接受的媒体类型发送,并且服务器能够至少生成其中一种媒体类型,那么它将返回一个成功的响应。如果服务器无法生成其中任何一个,它将返回错误响应


    这也回答了您的第一个问题:浏览器可能只发送HTML和XML作为可接受的媒体类型。您的服务器无法生成HTML,但能够生成XML,因此返回到浏览器。

    关于Web浏览器,我在
    @RequestMapping
    方法中使用参数
    @RequestHeader String accept
    时意识到
    控制台(通过SLF4j)显示
    accept:application/XML,text/XML,application/json,application/*+xml,application/*+json
    ,第一个列表是
    application/xml
    ,它可以回答为什么所有的web浏览器都有这种行为
    public Person getPersonCustomized(String id, String type){
    
        HttpHeaders headers = new HttpHeaders();
    
        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));
        }
        else if(type.equals("MIX01")){
            logger.info("MIX01 - XML _ JSON");
            headers.setAccept(Arrays.asList(MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON));
        }
        else if(type.equals("MIX02")){
            logger.info("MIX01 - JSON _ XML");
            headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML));
        }
    
        ResponseEntity<Person> response =
                restTemplate.exchange("http://localhost:8080/spring-utility/person/{id}/customized",
                                      HttpMethod.GET,
                                      new HttpEntity<Person>(headers),  
                                      Person.class,
                                       id
                                     );
        logger.info("status: {}", response.getStatusCode());
        logger.info("body: {}", response.getBody());
    
        return response.getBody();
    }
    
    public void setAccept(List<MediaType> acceptableMediaTypes)
    
    Set the list of acceptable media types, as specified by the Accept header.
    
    Parameters:
        acceptableMediaTypes - the acceptable media types