1

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        
RAHUL ROY
  • 126
  • 2
  • 13
  • Possible duplicate of [Jersey Exception : SEVERE: A message body reader for Java class](http://stackoverflow.com/questions/8594707/jersey-exception-severe-a-message-body-reader-for-java-class) – Manuel Spigolon Oct 22 '16 at 12:15
  • @Eomm: please tell me the solution.as I can see my issue is not mentioned there.....I have all the configuration guys there have suggested. Please tell me what am I missing ...Thanks in advance – RAHUL ROY Oct 22 '16 at 18:02

2 Answers2

1

There are couple of informations missing in your post, such as the URI structure, the version of the Servlet spec you're using, the way how you are calling the resource, ...

Anyway, you can check the following things:

  1. You're using the annotation @POST() which is strange to me, so you should better remove the parentheses:

    @POST
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces(MediaType.TEXT_HTML)
    public String x2(Pojo p) {
        return "HI HI post jason" + p.getName();
    }
    
  2. As you specified, you're expecting a POST request which means you have to send the JSON text in the body of the request, for example:

    { "name": "RAHUL" }
    
  3. Unless you use an HTML form, normal browsers do not provide a way to send POST requests(at least not known to me); so you need some browser plugin to send JSON as request body; you might use Postman for Chrome or RESTClient for Firefox, ..., OR use REST client frameworks like Apache Wink, ...

  4. After installing your favorite browser plugin, you have to set the request header Content-Type to application/json so that the framework knows you are sending a JSON body; I think this is even the reason why you're getting status:415).

ujulu
  • 3,289
  • 2
  • 11
  • 14
  • Thanks for suggestions... How ever same code run (believe me , without any change) ..after my system restarted( I guess, because it caused a restart of eclipse also)...Anyway, Thanks again. – RAHUL ROY Oct 24 '16 at 06:49
0

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)

RAHUL ROY
  • 126
  • 2
  • 13