大佬教程收集整理的这篇文章主要介绍了多个场景@RequestMapping与Accept或ResponseEntity一起生成JSON / XML,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
使用Accept标头真的很容易从REST服务中获取Json或xml格式。
这是我的控制器,看一下产生部分。
@requestMapPing(value = "propertIEs", produces = {MediaType.APPliCATION_JsON_VALUE, MediaType.APPliCATION_XML_value}, method = requestMethod.GET)
public UIProperty getPropertIEs() {
return uiProperty;
}
为了使用REST服务,我们可以使用下面的代码,其中标头可以是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", httpR_782_11845@ethod.GET, entity,String.class);
return response.getbody();
为了配合使用application/xml
,请添加此依赖项
<dependency>
<groupID>com.fasterxml.jackson.dataformat</groupID>
<artifactID>jackson-dataformat-xml</artifactID>
</dependency>
我正在使用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();
}
每一个都可以正常工作,无论有无扩展名,都可以观察JSON:
与XML相同
@requestMapping(value="/getxmlperson",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进行观察,并带有和不带有扩展名:
现在关于 Restful 我有以下内容:
@requestMapping(value="/person/{iD}/",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
}
注意@H_14_7@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",httpR_782_11845@ethod.GET,new httpEntity<Person>(headers),Person.class,id
);
因此,在此之前,我可以使用一个URL / URI来获取XML或JSON格式的一些数据。工作正常
我的问题是Spring MVC…考虑一下
@requestMapping(value="/{iD}/person",MediaType.APPLICATION_XML_value})
public @ResponseBody Person getPerson(@PathVariable Integer id){
return personMapRepository.findPerson(id);
}
我可以@requestMapping
通过以下方式调用或激活该处理程序方法():
Accept
值(例如JSON)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",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);
}
但:
如果执行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>
是的,在 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>
问题三
必须添加上面显示的方法的什么代码才能修复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);
}
以上是大佬教程为你收集整理的多个场景@RequestMapping与Accept或ResponseEntity一起生成JSON / XML全部内容,希望文章能够帮你解决多个场景@RequestMapping与Accept或ResponseEntity一起生成JSON / XML所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。