1
我跟着官方doc,並嘗試了很多已經問過的問題,但沒有任何工作。有人能幫我找到問題嗎?澤西JSON支持不工作
在這裏我添加了所有我能找到的細節。
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 + '\'' +
'}';
}
}
申請。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'調用。不知道你在「哪裏」部分想問什麼,但服務器運行在同一臺機器上,「GET」請求工作正常。 –
當你向Postman提出請求時,是否將MIME類型設置爲「application/javascript」?你還可以發佈工作GET端點嗎? –