【问题标题】:Jersey JSON support not working泽西 JSON 支持不起作用
【发布时间】:2015-07-11 16:41:31
【问题描述】:

我关注了官方doc 并尝试了许多已经提出的 SO 问题,但没有任何效果。有人可以帮我找出问题吗?

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

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>

请求详细信息 GET

一般:

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

【问题讨论】:

  • 如何以及在哪里调用后端?
  • 我正在使用 Postman Rest 客户端扩展来拨打 POST 电话。不知道你想在where 部分问什么,但服务器在同一台机器上运行,GET 请求工作正常。
  • 当您向 Postman 发出请求时,您是否将 mime 类型设置为“application/javascript”?您还可以发布有效的 GET 端点吗?
  • 我更新了问题以包含 GET 详细信息,请参见上文。我没有设置 MIME 类型的详细信息。不知道该怎么做。但我确实将内容类型设置为application/json
  • 你能从 post getProfile 方法中删除 @Path("/") 注释吗?这对我来说似乎是错误的。

标签: java json rest jersey


【解决方案1】:

问题解决了。在泽西岛2.* 我们需要手动注册MessageBodyReaderMessageBodyWritersThis SO questions 解释的很好。

【讨论】:

    猜你喜欢
    • 2015-07-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多