Search code examples
javajerseyjersey-client

Jersery API Body Writer Issue


I`ve been searching for an answer for this problem for a while but cant seem to find the correct solution to the problem. I have a service in which i calling the Web Resource like this:

@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();
    }
}

The code for the "Push Event" class :

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;
}

}

Now when i run this i get this error:

A message body writer for Java type, class com.gliif.model.PushEvent, and MIME media type, application/json, was not found

I have tried every possible way to correct this problem by going through the stackoverflow forums but i was to no avail. Can someone please help me solve this problem? :(


Solution

  • I found the solution. turns out the jersey config was wrong. all i did was to add these lines inside the code for creating the jersey client.

    DefaultClientConfig defaultClientConfig = new DefaultClientConfig();
    defaultClientConfig.getClasses().add(JacksonJsonProvider.class);
    Client client = Client.create(defaultClientConfig);
    

    And the error was resolved. Thanks Chasmo for pointing me to the right direction :) .