我一直在寻找这个问题的答案,但似乎无法找到解决问题的正确方法。我有一个服务,我在其中调用Web资源:
@POST
@Path("/save")
public Response addPushMessage(@FormParam("key") String key, @FormParam("pid") String pid,
@FormParam("url") String url, @FormParam("message") String message) throws Throwable {
if(key == null || key.isEmpty() || pid == null || pid.isEmpty())
return Response.ok().entity("Error: image key and profile Id are required for push message").build();
try{
Date now = new Date();
BasicDBObject pushMsg = new BasicDBObject("key", key).append("provider", pid).append("createdDate", now);
if(url!= null)
pushMsg.append("url", url);
if(message != null && !message.isEmpty())
pushMsg.append("message", message);
pushMsg.append("status", status);
String pushMsgId = ServiceUtil.mongo.put(GliifConstant.pushMsgCollectionName, pushMsg);
// Notify Event manager to push events if it's PROD
// Changed it delibrately to dev for local testing
if(ServiceUtil.env.equalsIgnoreCase("dev")){
PushEvent event = new PushEvent();
event.setCreatedDate(now);
event.setEventId(pushMsgId);
event.setKey(key);
event.setUrl(url);
event.setStatus(status);
WebResource clientResource = client.resource(pushURL);
ClientResponse resp = clientResource.type(MediaType.APPLICATION_JSON).post(ClientResponse.class, event);
}
if(pushMsgId != null)
return Response.ok().entity("OK").build();
else
return Response.ok().entity("Error: save push notification failed").build();
}
catch(Throwable t){
t.printStackTrace();
logger.error(PushService.class.getSimpleName()+" addPushNotification() failed on key: " + key + " \n", t);
return Response.status(GliifWebConstants.HTTP_GLIIF_SERVER_ERROR)
.header("HTTP_GLIIF_SERVER_ERROR","Gliif RestFull Service addPushNotification() operation caught runtime exception on saving push message on key:"+ key).build();
}
}
“推送活动”类的代码:
public class PushEvent implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
private String eventId = "";
private String key = null; //must have
private String url = "";
private String message = "";
private Date createdDate = null;
private int status = -1;
public String getKey() {
return key;
}
public void setKey(String key) {
this.key = key;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public String getEventId() {
return eventId;
}
public void setEventId(String eventId) {
this.eventId = eventId;
}
public Date getCreatedDate() {
return createdDate;
}
public void setCreatedDate(Date createdDate) {
this.createdDate = createdDate;
}
public int getStatus() {
return status;
}
public void setStatus(int status) {
this.status = status;
}
}
现在,当我运行此操作时,我收到此错误:
找不到Java类型的邮件正文编写器,类com.gliif.model.PushEvent
和MIME媒体类型application/json
我已经尝试了一切可能的方法来通过stackoverflow论坛来纠正这个问题,但我无济于事。有人可以帮我解决这个问题吗? :(
答案 0 :(得分:0)
您需要在项目中定义一个类,该类为您的PushEvent
类实现MessageBodyWriter。这可能看起来像这样。
@Provider
@Produces(MediaType.APPLICATION_JSON)
public final class PushEventWriter implements MessageBodyWriter<PushEvent> {
// implement methods and serialization
}
在本课程中,您必须实现将PushEvent转换为JSON的方式。
使用@Provider
注释,如果内容应该是JSON(由于@Produces
注释),Jersey将找到此编写器并使用它。
这是一篇描述该过程的好博文:http://programmingitch.blogspot.de/2014/03/creating-simple-jax-rs-messagebodywriter.html
希望这有帮助。
答案 1 :(得分:0)
我找到了解决方案。事实证明,球衣配置错了。我所做的只是在代码中添加这些行来创建泽西客户端。
DefaultClientConfig defaultClientConfig = new DefaultClientConfig();
defaultClientConfig.getClasses().add(JacksonJsonProvider.class);
Client client = Client.create(defaultClientConfig);
错误已解决。谢谢Chasmo指出我正确的方向:)。