Jersey JSON支持不起作用

时间:2015-07-11 16:41:31

标签: java json rest jersey

我跟随官方doc并尝试了很多已经问过的问题,但没有任何工作。有人可以帮我找到问题吗?

我在这里添加了我能找到的所有细节。

web.xml

<servlet>
    <servlet-name>REST Service</servlet-name>
    <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
    <!-- Register resources and providers under com.stxt.rest package. -->
    <init-param>
        <param-name>jersey.config.server.provider.packages</param-name>
        <param-value>com.stxt.supercentral.rest</param-value>
    </init-param>
    <init-param>
        <param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>
        <param-value>true</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>REST Service</servlet-name>
    <url-pattern>/rest/*</url-pattern>
</servlet-mapping>

资源api类

@Path("/agent")
@Component
public class AgentRestAPI {

@POST
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
@Path("/")
public Response getAgent(@QueryParam("id") String agentId, Profile profile) {
    return ApiResponseFactory.generateResponse(agentId);
}

@GET
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
@Path("/get")
public Response getAgent(@QueryParam("id") String agentId) {
    return ApiResponseFactory.generateResponse(agentId);
}

}

资源类

public class Profile {

    private String profileId;
    private String emailAddress;
    private String name;

    public String getProfileId() {
        return profileId;
    }

    public void setProfileId(String profileId) {
        this.profileId = profileId;
    }

    public String getEmailAddress() {
        return emailAddress;
    }

    public void setEmailAddress(String emailAddress) {
        this.emailAddress = emailAddress;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return "Profile{" +
                "profileId='" + profileId + '\'' +
                ", emailAddress='" + emailAddress + '\'' +
                ", name='" + name + '\'' +
                '}';
    }
}

application.wadl

<?xml version="1.0"encoding="UTF-8"standalone="yes"?>
<application xmlns="http://wadl.dev.java.net/2009/02">
    <doc xmlns:jersey="http://jersey.java.net/" jersey:generatedBy="Jersey: 2.2 2013-08-14 08:51:58"/>
    <grammars/>
    <resources base="http://localhost:8080/rest/">
        <resource path="/task">
            <resource path="/">
                <method id="getTask" name="GET">
                    <request>
                        <param xmlns:xs="http://www.w3.org/2001/XMLSchema" name="id" style="query" type="xs:string"/>
                    </request>
                    <response/>
                </method>
                <method id="apply" name="OPTIONS">
                    <request>
                        <representation mediaType="*/*"/>
                    </request>
                    <response>
                        <representation mediaType="application/vnd.sun.wadl+xml"/>
                    </response>
                </method>
                <method id="apply" name="OPTIONS">
                    <request>
                        <representation mediaType="*/*"/>
                    </request>
                    <response>
                        <representation mediaType="text/plain"/>
                    </response>
                </method>
                <method id="apply" name="OPTIONS">
                    <request>
                        <representation mediaType="*/*"/>
                    </request>
                    <response>
                        <representation mediaType="*/*"/>
                    </response>
                </method>
            </resource>
        </resource>
        <resource path="/customer">
            <resource path="/">
                <method id="getCustomer" name="GET">
                    <request>
                        <param xmlns:xs="http://www.w3.org/2001/XMLSchema" name="id" style="query" type="xs:string"/>
                    </request>
                    <response/>
                </method>
                <method id="apply" name="OPTIONS">
                    <request>
                        <representation mediaType="*/*"/>
                    </request>
                    <response>
                        <representation mediaType="application/vnd.sun.wadl+xml"/>
                    </response>
                </method>
                <method id="apply" name="OPTIONS">
                    <request>
                        <representation mediaType="*/*"/>
                    </request>
                    <response>
                        <representation mediaType="text/plain"/>
                    </response>
                </method>
                <method id="apply" name="OPTIONS">
                    <request>
                        <representation mediaType="*/*"/>
                    </request>
                    <response>
                        <representation mediaType="*/*"/>
                    </response>
                </method>
            </resource>
        </resource>
        <resource path="/agent">
            <resource path="/get">
                <method id="getAgent" name="GET">
                    <request>
                        <param xmlns:xs="http://www.w3.org/2001/XMLSchema" name="id" style="query" type="xs:string"/>
                    </request>
                    <response>
                        <representation mediaType="application/json"/>
                    </response>
                </method>
                <method id="apply" name="OPTIONS">
                    <request>
                        <representation mediaType="*/*"/>
                    </request>
                    <response>
                        <representation mediaType="application/vnd.sun.wadl+xml"/>
                    </response>
                </method>
                <method id="apply" name="OPTIONS">
                    <request>
                        <representation mediaType="*/*"/>
                    </request>
                    <response>
                        <representation mediaType="text/plain"/>
                    </response>
                </method>
                <method id="apply" name="OPTIONS">
                    <request>
                        <representation mediaType="*/*"/>
                    </request>
                    <response>
                        <representation mediaType="*/*"/>
                    </response>
                </method>
            </resource>
            <resource path="/">
                <method id="getAgent" name="POST">
                    <request>
                        <param xmlns:xs="http://www.w3.org/2001/XMLSchema" name="id" style="query" type="xs:string"/>
                        <representation mediaType="application/json"/>
                    </request>
                    <response>
                        <representation mediaType="application/json"/>
                    </response>
                </method>
                <method id="apply" name="OPTIONS">
                    <request>
                        <representation mediaType="*/*"/>
                    </request>
                    <response>
                        <representation mediaType="application/vnd.sun.wadl+xml"/>
                    </response>
                </method>
                <method id="apply" name="OPTIONS">
                    <request>
                        <representation mediaType="*/*"/>
                    </request>
                    <response>
                        <representation mediaType="text/plain"/>
                    </response>
                </method>
                <method id="apply" name="OPTIONS">
                    <request>
                        <representation mediaType="*/*"/>
                    </request>
                    <response>
                        <representation mediaType="*/*"/>
                    </response>
                </method>
            </resource>
        </resource>
        <resource path="application.wadl">
            <method id="getWadl" name="GET">
                <response>
                    <representation mediaType="application/vnd.sun.wadl+xml"/>
                    <representation mediaType="application/xml"/>
                </response>
            </method>
            <method id="apply" name="OPTIONS">
                <request>
                    <representation mediaType="*/*"/>
                </request>
                <response>
                    <representation mediaType="text/plain"/>
                </response>
            </method>
            <method id="apply" name="OPTIONS">
                <request>
                    <representation mediaType="*/*"/>
                </request>
                <response>
                    <representation mediaType="*/*"/>
                </response>
            </method>
            <resource path="{path}">
                <param xmlns:xs="http://www.w3.org/2001/XMLSchema" name="path" style="template" type="xs:string"/>
                <method id="geExternalGrammar" name="GET">
                    <response>
                        <representation mediaType="application/xml"/>
                    </response>
                </method>
                <method id="apply" name="OPTIONS">
                    <request>
                        <representation mediaType="*/*"/>
                    </request>
                    <response>
                        <representation mediaType="text/plain"/>
                    </response>
                </method>
                <method id="apply" name="OPTIONS">
                    <request>
                        <representation mediaType="*/*"/>
                    </request>
                    <response>
                        <representation mediaType="*/*"/>
                    </response>
                </method>
            </resource>
        </resource>
    </resources>
</application>

请求详细信息POST

一般:

Remote Address:[::1]:8080
Request URL:http://localhost:8080/rest/agent?id=23
Request Method:POST
Status Code:415 Unsupported Media Type

响应标题:

Cache-Control:must-revalidate,no-cache,no-store
Content-Length:352
Content-Type:text/html;charset=iso-8859-1
Server:Jetty(9.3.0.v20150612)

请求标题:

Accept:*/*
Accept-Encoding:gzip, deflate
Accept-Language:en-US,en;q=0.8
Connection:keep-alive
Content-Length:17
Content-Type:application/json
Cookie:JSESSIONID=1e6smzkea0dds17t964t3u7fc2
CSP:active
Host:localhost:8080
Origin:chrome-extension://hgmloofddffdnphfgcellkdfbfbjeloo
User-Agent:Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/43.0.2357.132 Safari/537.36

查询字符串参数:

id:23

请求有效负载:

{"profileId": "1"}

响应:

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html;charset=utf-8"/>
        <title>Error 415 Unsupported Media Type</title>
    </head>
    <body><h2>HTTP ERROR 415</h2>
        <p>Problem accessing /rest/agent. Reason:
            <pre>    Unsupported Media Type</pre></p><hr><a href="http://eclipse.org/jetty">Powered by Jetty:// 9.3.0.v20150612</a><hr/>

    </body>
</html>

申请详情获取

一般:

Remote Address:[::1]:8080
Request URL:http://localhost:8080/rest/agent/get?id=23
Request Method:GET
Status Code:200 OK

响应标题:

Cache-Control:no-cache, no-transform, max-age=0
Content-Length:4
Content-Type:application/json
Server:Jetty(9.3.0.v20150612)

请求标题:

Accept:text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
Accept-Encoding:gzip, deflate, sdch
Accept-Language:en-US,en;q=0.8
Cache-Control:max-age=0
Connection:keep-alive
Cookie:JSESSIONID=132gft2iqjdzgcrn6eliv77ox
Host:localhost:8080
User-Agent:Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/43.0.2357.132 Safari/537.36

查询字符串参数:

id:23

响应:

"23"

附加图书馆: 可能有额外的未使用的库(我可能在网上尝试一些解决方案时添加了这些库)。

asm-all-repackaged-2.2.0-b14.jar
cglib-2.2.0-b14.jar
hk2-api-2.2.0-b14.jar
hk2-locator-2.2.0-b14.jar
hk2-utils-2.2.0-b14.jar
jaxb-api-2.2.7.jar
jersey-client.jar
jersey-common.jar
jersey-container-servlet-core.jar
jersey-container-servlet.jar
jersey-server.jar
activation-1.1.jar
jackson-core-asl-1.9.2.jar
jackson-jaxrs-1.9.2.jar
jackson-mapper-asl-1.9.2.jar
jackson-xc-1.9.2.jar
jaxb-api-2.2.2.jar
jaxb-impl-2.2.3-1.jar
jersey-core-1.19.jar
jersey-json-1.19.jar
jettison-1.1.jar
stax-api-1.0-2.jar
org.osgi.core-4.2.0.jar
osgi-resource-locator-1.0.1.jar

1 个答案:

答案 0 :(得分:2)

问题解决了。在泽西岛2.*,我们需要手动注册MessageBodyReaderMessageBodyWritersThis SO个问题解释得很清楚。