Spring 行为@RequestMapping';s通过RestTemplate和Web浏览器生成
我正在使用Spring4.0.7 我有一个用JSON和XML表示的实体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",
@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
然后响应是xml格式的Accept=application/xml,application/json
然后响应是json格式的Accept=application/json,application/xml
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