网站建设技术流程,wordpress菜单颜色,上海app软件开发,哪家公司设计网站jax-rs jax-wsJSON是所有REST * API的王者#xff0c;但是您仍然可能需要公开多种表示形式#xff0c;包括XML。 使用JAX-RS和Spring MVC都非常简单。 实际上#xff0c;唯一要做的就是用JAXB注释对从API调用返回的POJO进行注释。 但是在我看来#xff0c;序列化对象列表时… jax-rs jax-ws JSON是所有REST * API的王者但是您仍然可能需要公开多种表示形式包括XML。 使用JAX-RS和Spring MVC都非常简单。 实际上唯一要做的就是用JAXB注释对从API调用返回的POJO进行注释。 但是在我看来序列化对象列表时JAX-RS会比Spring MVC更好。 让我们来看看。 POJO 两者的唯一要求假设使用JAXB是使用JAXB注释对POJO进行注释 XmlRootElement
public class Incident {}JAX-RS方式 GET
Path(user/{userId}/incident)
public ListIncident getUserIncidents(PathParam(userId) long userId) {// return
} 当以application/json作为可接受的表示执行上述方法时JAX-RS会将返回的列表正确序列化为JSON如下所示 [{description: Lorem ipsum... ,status: NEW},{description: Lorem ipsum... ,status: NEW}
] 没有特殊的包装对象。 生成的XML可能如下所示 incidentsincidentdescriptionLorem ipsum .../descriptionstatusNEW/status/incidentincidentdescriptionLorem ipsum .../descriptionstatusNEW/status/incident
/incidents 它只是工作。 没有包装对象。 没有额外的工作。 我们完了。 Spring MVC方式JAXB 在Spring中您将如何做比如说Spring Boot因为它起步最快 RequestMapping(value user/{userId}/incident)
public ListIncident getUserIncidents(PathVariable(userId) long userId) {// return
} 使用以下请求请求JSON表示形式后 $ curl -i http://localhost:8080/user/3/incident 结果与JAX-RS相同。 要使服务器呈现XML而不是JSON您可能必须发送Accept: text/xml标头 $ curl -i -H Accept: text/xml http://localhost:8080/user/3/incident 但是结果将是 406不可接受。 在这种情况下找不到可接受的表示 。 Spring MVC方式jackson-dataformat-xml 使用Spring MVC有一种解决方案可以立即使用类似于JAX-RS但输出效果较差 。 该解决方案使用jackson-dataformat-xml。 向您的项目添加依赖项 dependencygroupIdcom.fasterxml.jackson.dataformat/groupIdartifactIdjackson-dataformat-xml/artifactId
/dependency 使用新的依赖关系对XML表示的调用应返回如下内容 ArrayListitemdescriptionLorem ipsum .../descriptionstatusNEW/status/itemitemdescriptionLorem ipsum .../descriptionstatusNEW/status/item
/ArrayList 请注意jackson-dataformat-xml不需要使用JAXB批注。 翻译自: https://www.javacodegeeks.com/2015/04/jax-rs-2-x-vs-spring-mvc-returning-an-xml-representation-of-a-list-of-objects.htmljax-rs jax-ws