RAHUL ROY RAHUL ROY - 1 month ago 16
reST (reStructuredText) Question

JSON to POJO mapping in Rest

I am new to WebServices. I was able to implement normal get and post,but when I am trying to implement post method with MediaType.APPLICATION_JSON. I am getting errors. Please let me know what is wrong here.

MY Resource :



package com;
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

@Path("Hi")
public class Resource {
@GET()
@Produces(MediaType.TEXT_HTML)
public String x() {
return "HI HI";
}

@POST()
// @Consumes(MediaType.APPLICATION_FORM_URLENCODED)
@Produces(MediaType.TEXT_HTML)
public String x1() {
return "HI HI post normal";
}

@POST()
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.TEXT_HTML)
public String x2(Pojo p) {
return "HI HI post jason" + p.getName();

}

}


_----------------------

My Web.xml



.......

<servlet>
<servlet-name>Bakchodi</servlet-name>
<servlet-Class>
com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>com.sun.jersey.config.property.packages</param-name>
<param-value>com</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>Bakchodi</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>`





Pojo



package com;

import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement(name="someName")
public class Pojo {
private String name;

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String toString() {
return "Pojo[name =" + name + "]";
}

}





Jars included in pom.xml



asm,jersey{core,server,client,bundle,json,}andcommon-loggings

Please tell what I am missing.

Error:

status:415
server console:SEVERE: A message body reader for Java class com.Pojo, and Java type class com.Pojo, and MIME media type application/json was not found.
The registered message body readers compatible with the MIME media type are:
application/json ->
com.sun.jersey.json.impl.provider.entity.JSONJAXBElementProvider$App
com.sun.jersey.json.impl.provider.entity.JSONRootElementProvider$App
com.sun.jersey.json.impl.provider.entity.JSONListElementProvider$App
*/* ->
com.sun.jersey.core.impl.provider.entity.FormProvider
com.sun.jersey.core.impl.provider.entity.StringProvider
com.sun.jersey.core.impl.provider.entity.ByteArrayProvider
com.sun.jersey.core.impl.provider.entity.FileProvider
com.sun.jersey.core.impl.provider.entity.InputStreamProvider
com.sun.jersey.core.impl.provider.entity.DataSourceProvider com.sun.jersey.core.impl.provider.entity.XMLJAXBElementProvider$General
com.sun.jersey.core.impl.provider.entity.ReaderProvider
com.sun.jersey.core.impl.provider.entity.DocumentProvider
com.sun.jersey.core.impl.provider.entity.SourceProvider$StreamSourceReader
com.sun.jersey.core.impl.provider.entity.SourceProvider$SAXSourceReader
com.sun.jersey.core.impl.provider.entity.SourceProvider$DOMSourceReader
com.sun.jersey.json.impl.provider.entity.JSONJAXBElementProvider$General
com.sun.jersey.core.impl.provider.entity.XMLRootElementProvider$General
com.sun.jersey.core.impl.provider.entity.XMLListElementProvider$General
com.sun.jersey.core.impl.provider.entity.XMLRootObjectProvider$General
com.sun.jersey.core.impl.provider.entity.EntityHolderReader
com.sun.jersey.json.impl.provider.entity.JSONRootElementProvider$General
com.sun.jersey.json.impl.provider.entity.JSONListElementProvider$General

Answer

Same code run (believe me , without any change) ..after my system restarted( I guess, because it caused a restart of eclipse also).. And before it I was doing maven-clean, maven install, deletion of project from tomcat confguration at leasts 11-12 times, but was always getting this error.

So I guess this will now work for the guys who want an example for application/json in rest api.....( for sending request you can either use apps like Advanced rest client or postman , or you can create a form in html page and submit json object by changing your form by using stringify(), then submiting it..I had used ajax for this prupose)

Comments