java - 热衷于使用 Jersey 和 Netbeans 编辑 JSON 对象输出?

标签 java json rest jersey glassfish-3

我的 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/

相关文章:

php - 通过 PUT 将文件发送到 codeigniter REST_Controller

java - 具有给定长度单元测试的随机字符串

java - 安卓工作室 : "Gradle sync failed: Could not run JVM from the selected JDK."

Java应用程序与mysql服务器通信

json - Python打开json文件保存在内存中

c# - ASMX 网络服务 - 返回 JSON 而不是 XML

java - Android – Facebook SDK AppEventsLogger logEvent 不工作

javascript - 使用 JSON 响应

java - 带有(太多)复杂对象的 Restful 架构问题

rest - 如何限制 AzureRM 中 GET 请求中的资源计数