我的 RESTful Web 服务再次出现问题:( 概括: 当我调用此 URL 时:
http://localhost:8080/iOSWebServices/resources/credits/1
我得到这个 JSON 输出:
{"id":1,"lastname":null,"firstname":"lalsdaksldlasds"}
但我想要这个 JSON 输出:
{"creditRequest":{"id":1,"lastname":null,"firstname":"lalsdaksldlasds"}}
长版: 我使用 netbeans、glassfish 3 和 jersey 1.9.1 来提供一些简单的测试 Web 服务。问题是,所有 json 输出都缺少输出中的类名...
对于上面的例子: CreditRequest.java
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;
@XmlRootElement(name = "creditRequest")
@XmlType(name = "creditRequestType")
@XmlAccessorType(XmlAccessType.FIELD)
public class CreditRequest {
private Long id;
private String firstname;
private String lastname;
public String getFirstname() {
return firstname;
}
public void setFirstname(String firstname) {
this.firstname = firstname;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getLastname() {
return lastname;
}
public void setLastname(String lastname) {
this.lastname = lastname;
}
}
CreditRequest.java
import javax.ws.rs.*;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.UriInfo;
@Path("credits")
@RequestScoped
public class CreditsResource {
@Context
private UriInfo context;
/** Creates a new instance of CreditsResource */
public CreditsResource() {
}
//OUTPUT AS ABOVE!
@GET
@Path("{id}")
@Produces(MediaType.APPLICATION_JSON)
public Response getCreditById(@PathParam("id") long id) {
System.out.println("getCreditById");
CreditRequest creditRequest = new CreditRequest();
creditRequest.setId(new Long(1));
creditRequest.setFirstname("lalsdaksldlasds");
return Response.ok().entity(creditRequest).build();
}
}
Jersey 用户指南解释了应该如何工作 - 引用...
Example 5.14. Keep XML root tag equivalent in JSON mapped JSON notation
JSONConfiguration.mapped().rootUnwrapping(false).build()
and get the following JSON for our Contact bean:
Example 5.15. XML root tag equivalent kept in JSON using mapped notation
{"contact":{ "id":"2"
,"name":"Bob"
,"addresses":{"street":"Long Street 1"
,"town":"Short Village"}}}
为此,我编写了一个简单的类,实现了 ContextResolver(也在用户指南中)
JAXBContextResolver.java//似乎不起作用
@Provider
public class JAXBContextResolver implements ContextResolver<JAXBContext> {
private JAXBContext context;
private Class[] types = {CreditRequest .class}; //FIXED
public JAXBContextResolver() throws Exception {
System.out.println("init");
this.context = new JSONJAXBContext(
JSONConfiguration.mapped().rootUnwrapping(false).build(), types);
}
@Override
public JAXBContext getContext(Class<?> objectType) {
System.out.println("get");
for (Class type : types) {
if (type == objectType) {
return context;
}
}
return null;
}
}
当我启动 Web 服务时,JAXBContextResolver 的构造函数中定义的输出将被写入“init” - 但是 “得到”不会......
在这个项目中,netbeans 控制我的 web 服务的资源 - 并且再次初始化 服务器启动时的 JAXBContextResolver - 但仍然没有 rootUnwrapping :(
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
</web-app>
有什么想法吗?我真的不知道为什么我的 JSON 输出没有变化:(
最佳答案
上周我也遇到了同样的问题。我在 web.xml 文件中找到了解决方案。删除以下行后:
<init-param>
<param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>
<param-value>true</param-value>
</init-param>
关于java - 热衷于使用 Jersey 和 Netbeans 编辑 JSON 对象输出?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7888587/