此应用程序只是mqtt代理和另一个REST接口之间的接口,因此它不是一个完整的REST应用程序。 (传入的mqtt消息被处理并通过POST请求发送到REST接口。)
如果我使用java.net.HttpURLConnection
和Gson
发送带有JSON有效负载的POST请求,它可以正常工作,但我也希望将其与javax.ws.rs.client.Client
和javax.ws.rs.core.Response
一起使用,因为以后可能需要SSL支持,我已经知道如何用Jersey提供。既然我认为我有正确的依赖关系,并且POJO有一个非常简单的结构和空构造函数,我无法找出导致问题的原因。以下是相关代码:
我想发送的POJO:
public class NumData {
private String meas_loc_site;
private int meas_loc_id;
private String gmt_event;
private String eu_db_site;
private int eu_db_id;
private int eu_type_code;
private float data_value;
public NumData(){
}
public NumData(String meas_loc_site, int meas_loc_id, String gmt_event, String eu_db_site, int eu_db_id,
int eu_type_code, float data_value) {
super();
this.meas_loc_site = meas_loc_site;
this.meas_loc_id = meas_loc_id;
this.gmt_event = gmt_event;
this.eu_db_site = eu_db_site;
this.eu_db_id = eu_db_id;
this.eu_type_code = eu_type_code;
this.data_value = data_value;
}
public String getMeas_loc_site() {
return meas_loc_site;
}
public void setMeas_loc_site(String meas_loc_site) {
this.meas_loc_site = meas_loc_site;
}
public int getMeas_loc_id() {
return meas_loc_id;
}
public void setMeas_loc_id(int meas_loc_id) {
this.meas_loc_id = meas_loc_id;
}
public String getGmt_event() {
return gmt_event;
}
public void setGmt_event(String gmt_event) {
this.gmt_event = gmt_event;
}
public String getEu_db_site() {
return eu_db_site;
}
public void setEu_db_site(String eu_db_site) {
this.eu_db_site = eu_db_site;
}
public int getEu_db_id() {
return eu_db_id;
}
public void setEu_db_id(int eu_db_id) {
this.eu_db_id = eu_db_id;
}
public int getEu_type_code() {
return eu_type_code;
}
public void setEu_type_code(int eu_type_code) {
this.eu_type_code = eu_type_code;
}
public float getData_value() {
return data_value;
}
public void setData_value(float data_value) {
this.data_value = data_value;
}
}
使用泽西图书馆发送请求的效用函数:
public static <T> Response sendRequest(String URI, String method, T payload) throws Exception {
Response response = null;
ClientConfig configuration = new ClientConfig();
configuration.property(ClientProperties.CONNECT_TIMEOUT, 30000);
configuration.property(ClientProperties.READ_TIMEOUT, 30000);
Client client = ClientBuilder.newClient(configuration);
WebTarget target = client.target(UriBuilder.fromUri(URI).build());
switch (method) {
case "GET":
response = target.request().header("Content-type", "application/json").get();
break;
case "POST":
System.out.println("test1");
response = target.request().header("Content-type", "application/json").post(Entity.json(payload));
System.out.println("test2");
break;
case "PUT":
response = target.request().header("Content-type", "application/json").put(Entity.json(payload));
break;
case "DELETE":
response = target.request().header("Content-type", "application/json").delete();
break;
default:
throw new Exception("Invalid method type was given to the Utility.sendRequest() method");
}
if (response == null || response.getStatus() == 500 || response.getStatus() == 400 || response.getStatus() == 404) {
throw new Exception("Response is null or the response status code is: 400, 404 or 500");
}
return response;
}
pom.xml依赖项:(版本为2.25.1)
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.glassfish.jersey</groupId>
<artifactId>jersey-bom</artifactId>
<version>${jersey.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet-core</artifactId>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-moxy</artifactId>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.1</version>
</dependency>
<dependency>
<groupId>org.eclipse.paho</groupId>
<artifactId>org.eclipse.paho.client.mqttv3</artifactId>
<version>1.1.1</version>
</dependency>
</dependencies>
我也尝试使用jersey-media-json-jackson
,但问题仍然存在。
和堆栈跟踪:(抱歉,不知道如何修复此处的副本)
org.glassfish.jersey.message.internal.WriterInterceptorExecutor$TerminalWriterInterceptor aroundWriteTo
SEVERE:找不到媒体类型= application / json的MessageBodyWriter,type = class my.package.NumData,genericType = class my.package.NumData。 org.glassfish.jersey.message.internal.MessageBodyProviderNotFoundException:找不到媒体类型= application / json的MessageBodyWriter,type = class my.package.NumData,genericType = class my.package.NumData。 at org.glassfish.jersey.message.internal.WriterInterceptorExecutor $ TerminalWriterInterceptor.aroundWriteTo(WriterInterceptorExecutor.java:247) 在org.glassfish.jersey.message.internal.WriterInterceptorExecutor.proceed(WriterInterceptorExecutor.java:162) 在org.glassfish.jersey.message.internal.MessageBodyFactory.writeTo(MessageBodyFactory.java:1130) 在org.glassfish.jersey.client.ClientRequest.doWriteEntity(ClientRequest.java:517) 在org.glassfish.jersey.client.ClientRequest.writeEntity(ClientRequest.java:499) 在org.glassfish.jersey.client.internal.HttpUrlConnector._apply(HttpUrlConnector.java:393) 在org.glassfish.jersey.client.internal.HttpUrlConnector.apply(HttpUrlConnector.java:285) 在org.glassfish.jersey.client.ClientRuntime.invoke(ClientRuntime.java:252) 在org.glassfish.jersey.client.JerseyInvocation $ 1.call(JerseyInvocation.java:684) 在org.glassfish.jersey.client.JerseyInvocation $ 1.call(JerseyInvocation.java:681) 在org.glassfish.jersey.internal.Errors.process(Errors.java:315) 在org.glassfish.jersey.internal.Errors.process(Errors.java:297) 在org.glassfish.jersey.internal.Errors.process(Errors.java:228) 在org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:444) 在org.glassfish.jersey.client.JerseyInvocation.invoke(JerseyInvocation.java:681) 在org.glassfish.jersey.client.JerseyInvocation $ Builder.method(JerseyInvocation.java:437) 在org.glassfish.jersey.client.JerseyInvocation $ Builder.post(JerseyInvocation.java:343) at eu.mantis.mqtt_mimosa.Utility.sendRequest(Utility.java:53) at eu.mantis.mqtt_mimosa.Main.sendEnviromentMeasToMimosa(Main.java:155) 在eu.mantis.mqtt_mimosa.Main.messageArrived(Main.java:79) 在org.eclipse.paho.client.mqttv3.internal.CommsCallback.deliverMessage(CommsCallback.java:477) 在org.eclipse.paho.client.mqttv3.internal.CommsCallback.handleMessage(CommsCallback.java:380) 在org.eclipse.paho.client.mqttv3.internal.CommsCallback.run(CommsCallback.java:184) 在java.lang.Thread.run(未知来源)