Java 如何用xml表示响应体
您好,我使用的是spring框架,默认表示形式是JSON。我试图用3种格式表示响应体。Xml、json和html。默认方式是json,但在用xml和html表示时遇到问题。您希望响应主体的xml和html格式如何。我在运行代码时遇到一个406错误 在我的pom xml中:我声明Java 如何用xml表示响应体,java,xml,spring-mvc,response,Java,Xml,Spring Mvc,Response,您好,我使用的是spring框架,默认表示形式是JSON。我试图用3种格式表示响应体。Xml、json和html。默认方式是json,但在用xml和html表示时遇到问题。您希望响应主体的xml和html格式如何。我在运行代码时遇到一个406错误 在我的pom xml中:我声明 <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.6.3</version>
</dependency>
<dependency>
<groupId>org.codehaus.woodstox</groupId>
<artifactId>woodstox-core-asl</artifactId>
<version>4.4.1</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.6.3</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-xml</artifactId>
<version>2.6.3</version>
</dependency>
com.fasterxml.jackson.core
杰克逊核心
2.6.3
org.codehaus.woodstox
woodstox core asl
4.4.1
com.fasterxml.jackson.core
杰克逊数据绑定
2.6.3
com.fasterxml.jackson.dataformat
jackson数据格式xml
2.6.3
在我的dispatcher-servlet.xml中:
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping" />
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter" >
<property name="messageConverters">
<list>
<bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"/>
<bean class="org.springframework.http.converter.ByteArrayHttpMessageConverter"/>
<bean class="org.springframework.http.converter.xml.SourceHttpMessageConverter"/>
<bean class="org.springframework.http.converter.FormHttpMessageConverter"/>
<bean class="org.springframework.http.converter.StringHttpMessageConverter"/>
</list>
</property>
</bean>
最后是我的控制器方法:
@RequestMapping(value="person/{id}", method=RequestMethod.GET, params="format=xml", produces=MediaType.APPLICATION_XML_VALUE)
@ResponseBody
public ResponseEntity<?> PersoninXML(@PathVariable("id")Long id) {
Person person = personDAO.findById(id);
if(person!=null){
return new ResponseEntity<Person>(person, HttpStatus.OK);
}
else
return new ResponseEntity<String>("Id doesn't exist", HttpStatus.NOT_FOUND);
}
@RequestMapping(value=“person/{id}”,method=RequestMethod.GET,params=“format=xml”,products=MediaType.APPLICATION\u xml\u value)
@应答器
公共响应PersoninXML(@PathVariable(“id”)长id){
Person-Person=personDAO.findById(id);
if(person!=null){
返回新的响应属性(person,HttpStatus.OK);
}
其他的
返回新的响应属性(“Id不存在”,HttpStatus.NOT_FOUND);
}
。看到这个了吗link@VinayPrajapati我试图通过RESTAdvanced client/postman而不是通过.jsp进行查看page@VinayPrajapati此外,我还在Pojo对象中添加了XmlRootElement,但仍然没有成功。请检查一下医生link@VinayPrajapati我仍然会遇到错误,因为我无法在xml中显示它。我知道这与我在dispatcher中的配置有关。请你帮我一下好吗。看到这个了吗link@VinayPrajapati我试图通过RESTAdvanced client/postman而不是通过.jsp进行查看page@VinayPrajapati此外,我还在Pojo对象中添加了XmlRootElement,但仍然没有成功。请检查一下医生link@VinayPrajapati我仍然会遇到错误,因为我无法在xml中显示它。我知道这与我在dispatcher中的配置有关。请你帮我一下好吗。