我有一个调用rest服务的get请求(能够正常工作),我想要更改并使用Jersey。
我从官方网站下载了最新的JAR文件并将它们放在我的项目中。
除了这个:String response = invocationBuilder.get(String.class);
,我的所有响应都抛出了异常。
javax.ws.rs.NotAcceptableException: HTTP 406 Not Acceptable
at org.glassfish.jersey.client.JerseyInvocation.convertToException(JerseyInvocation.java:1014)
at org.glassfish.jersey.client.JerseyInvocation.translate(JerseyInvocation.java:816)
at org.glassfish.jersey.client.JerseyInvocation.access$700(JerseyInvocation.java:92)
at org.glassfish.jersey.client.JerseyInvocation$2.call(JerseyInvocation.java:700)
at org.glassfish.jersey.internal.Errors.process(Errors.java:315)
at org.glassfish.jersey.internal.Errors.process(Errors.java:297)
at org.glassfish.jersey.internal.Errors.process(Errors.java:228)
at org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:444)
at org.glassfish.jersey.client.JerseyInvocation.invoke(JerseyInvocation.java:696)
at org.glassfish.jersey.client.JerseyInvocation$Builder.method(JerseyInvocation.java:420)
at org.glassfish.jersey.client.JerseyInvocation$Builder.get(JerseyInvocation.java:316)
代码:
ClientConfig clientConfig = new ClientConfig();
Client client = ClientBuilder.newClient(clientConfig);
//not so orthodox...
WebTarget webTarget = client.target(uri);//uri= https://www.foo.bar/api/v1.0/sms/sendMessage?accessKeyId=34TR&idCampaign=4&fromNumber=Test&toNumber=393281234567&message=Inserisci+la+seguente+password%3A+8c495b
Invocation.Builder invocationBuilder2 =
webTarget.request(MediaType.APPLICATION_JSON);
Invocation.Builder invocationBuilder =
webTarget.request();//------NO MEDIA TYPE SPECIFICATION
Invocation.Builder invocationBuilder3 =
webTarget.request(MediaType.APPLICATION_XML_TYPE);
Invocation.Builder invocationBuilder4 =
webTarget.request(MediaType.TEXT_XML_TYPE);
Invocation.Builder invocationBuilder5 =
webTarget.request(MediaType.TEXT_PLAIN_TYPE);
Invocation.Builder invocationBuilder6 =
webTarget.request(MediaType.APPLICATION_FORM_URLENCODED);
try {
String response2 = invocationBuilder2.get(String.class);
} catch (Exception e) {System.out.println(e);}
try {
//WORKS ONLY THIS, WITH NO MEDIA TYPE SPECIFICATION
String response = invocationBuilder.get(String.class);
} catch (Exception e) {System.out.println(e);}
try {
String response3 = invocationBuilder3.get(String.class);
} catch (Exception e) {System.out.println(e);}
try {
String response4 = invocationBuilder4.get(String.class);
} catch (Exception e) {System.out.println(e);}
try {
String response5 = invocationBuilder5.get(String.class);
} catch (Exception e) {System.out.println(e);}
try {
String response6 = invocationBuilder6.get(String.class);
} catch (Exception e) {System.out.println(e);}
//NONE OF THIS WORKS (last part of the test):
try {
SmsResponse response02 = invocationBuilder2.get(SmsResponse.class);
} catch (Exception e) {System.out.println(e);}
try {
SmsResponse response0 = invocationBuilder.get(SmsResponse.class);
} catch (Exception e) {System.out.println(e);}
try {
SmsResponse response03 = invocationBuilder3.get(SmsResponse.class);
} catch (Exception e) {System.out.println(e);}
try {
SmsResponse response04 = invocationBuilder4.get(SmsResponse.class);
} catch (Exception e) {System.out.println(e);}
try {
SmsResponse response05 = invocationBuilder5.get(SmsResponse.class);
} catch (Exception e) {System.out.println(e);}
try {
SmsResponse response06 = invocationBuilder6.get(SmsResponse.class);
} catch (Exception e) {System.out.println(e);}
response字符串是一个JSON: {"status":"success","uid":"407077","numSms":1,"errorMsg":false}
但是在尝试获取SmsResponse对象时,我遇到了一个异常(代码的最后一部分),response0抛出了下面的异常(其他情况抛出了前面的406异常):
org.glassfish.jersey.message.internal.MessageBodyProviderNotFoundException: MessageBodyReader not found for media type=application/json;charset=UTF-8, type=class it.sian.zfab.SmsResponse, genericType=class it.sian.zfab.SmsResponse.
at org.glassfish.jersey.client.JerseyInvocation.translate(JerseyInvocation.java:808)
at org.glassfish.jersey.client.JerseyInvocation.access$700(JerseyInvocation.java:92)
at org.glassfish.jersey.client.JerseyInvocation$2.call(JerseyInvocation.java:700)
at org.glassfish.jersey.internal.Errors.process(Errors.java:315)
at org.glassfish.jersey.internal.Errors.process(Errors.java:297)
at org.glassfish.jersey.internal.Errors.process(Errors.java:228)
at org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:444)
at org.glassfish.jersey.client.JerseyInvocation.invoke(JerseyInvocation.java:696)
at org.glassfish.jersey.client.JerseyInvocation$Builder.method(JerseyInvocation.java:420)
at org.glassfish.jersey.client.JerseyInvocation$Builder.get(JerseyInvocation.java:316)
豆子(The bean):
@XmlRootElement
public class SmsResponse implements Serializable {
private static final long serialVersionUID = 1L;
@QueryParam("uid")
private String uid;
@QueryParam("status")
private String status;
@QueryParam("errorMsg")
private String errorMsg;
@QueryParam("numSms")
private Integer numSms;
//getter and setter...
}
我之前使用的方法(已经可行),在这里你可以看到它在处理内容为application/json时运作良好:
private <T> T restCallJson(String uri, Class<T> returnType)
{
T objectResponse = null;
try {
URL url = new URL(uri);
HttpsURLConnection connection = (HttpsURLConnection)url.openConnection();
connection.setRequestMethod("GET");
connection.setRequestProperty("Content-Type", "application/json");
InputStream inputStream = connection.getInputStream();
Gson gson = new Gson();
final BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
objectResponse = gson.fromJson(reader, returnType);
connection.disconnect();
}
catch (Exception e)
{
log.error("WebServiceUtility - restCallJson: " + e);
}
return objectResponse;
}
1) 为什么我不能指定MediaType?应该使用哪一个?
2) 为什么我不能直接检索SmsResponse对象?
{Content-Length=[63], Expires=[Thu, 19 Nov 1981 08:52:00 GMT], Set-Cookie=[PHPSESSID=omntv5nt9ekjlqoh949diaoqp3; path=/], Connection=[close], X-Powered-By=[PHP/5.3.3], Server=[Apache/2.2.15 (CentOS)], Pragma=[no-cache], Cache-Control=[no-store, no-cache, must-revalidate, post-check=0, pre-check=0], Vary=[negotiate], Date=[Mon, 19 Oct 2015 13:28:12 GMT], TCN=[choice], Content-Location=[sendMessage.php], Content-Type=[application/json; charset=UTF-8]}
。 - AccollativoinvocationBuilder2.get()
的主体是 `<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN"><html><head> <title>406 Not Acceptable</title> </head><body> <h1>Not Acceptable</h1> <p>An appropriate representation of the requested resource /api/v1.0/sms/sendMessage could not be found on this server.</p> Available variants: <ul> <li><a href="sendMessage.php">sendMessage.php</a> , type text/html</li> </ul> <hr> <address>Apache/2.2.15 (CentOS) Server at www.foo.bar Port 443</address> </body></html>`。 - Accollativorequest()
即可。您仍将得到Content-Type: application/json
。这就是您需要能够反序列化为SmsResponse
的全部。 - Paul Samsothahttps://www.foo.bar/api/v1.0/sms/sendMessage.php?accessKeyId=34rt&idCampaign=4&fromNumber=Test&toNumber=393281234567&message=Inserisci+la+seguente+password%3A+e71a3b
这样json头就被接受了。 谢谢,明天我会检查你对第二部分的建议。 - Accollativo