我與春天一起工作產生JSON/XML有多個方案4.0.7@RequestMapping(Accept)或ResponseEntity
關於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();
}
每一個做工精細,同時觀察了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();
}
每一個工作正常,同時觀察了XML,有和沒有擴展名:
- /getxmlperson
- /getperson.xml
現在約寧靜我有以下幾點:
@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
}
觀察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
);
直到這裏,因此我可以使用一個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);
}
我可以通過電話或通過激活該處理方法(@RequestMapping
):
- jQuery的使用Ajax的工作,我能以指示
Accept
的值(例如JSON) - Poster,通過
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);
}
問題二:
上面顯示的方法是什麼代碼必須被添加到自定義返回類型格式? 我的意思是,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);
}
但是:
如果我執行的網址:
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>
是的XML!
注意:我可以證實控制檯打印Ok JSON
如果我執行的網址:
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>
問題三
必須添加上面顯示的方法的哪些代碼才能修復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);
}
查看內容協商。 – 2014-10-31 15:38:23
請參閱'Alpha'部分。它的工作原理,記得我在URL中使用.json和.xml。 – 2014-10-31 18:39:58
如果你想使用URL,你不能設置內容類型,你只能這樣做,如果你控制呼叫(如在JavaScript中)。你可以做的一件事是將服務器上的默認內容類型設置爲JSON(而不是現在的HTML)。不能控制內容類型是合乎邏輯的,因爲來自HTML的鏈接應該導致HTML。 – 2014-11-06 06:37:50