skip to Main Content

I am passing JSON data like

{
    "empname" : "seo"
}  

to the POST url but it returns 415 error i.e Unsupported media type
After troubleshooting I found out that the content-type should be “application/json” in poster extension and I tried the same but didn’t worked.
Below is my code for service

package webService;

import java.sql.Connection;
import java.util.ArrayList;

import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

import com.google.gson.Gson;

import dao.Database;
import dao.Project;
import dto.FeedObjects;
import model.ProjectManager;


@Path("/WebService")
public class FeedService 
{
    @GET
    @Path("/GetFeeds")
    @Produces("application/json")
    public String feed()
    {
    String feeds = null;
    try
    {
    ArrayList<FeedObjects> feedData = null;
    ProjectManager projectManager= new ProjectManager();
    feedData = projectManager.GetFeeds();
    Gson gson = new Gson();
    System.out.println(gson.toJson(feedData));
    feeds = gson.toJson(feedData);
    }

    catch (Exception e)
    {
    System.out.println("Exception Error"); //Console 
    }
    return feeds;
    }

    @GET
    @Path("/insert/{empname}/{empsalary}")

    public String insertEmpName(@PathParam("empname") String empname,@PathParam("empsalary") String empsalary) {

        String result = "Employee Insertion Failed!!!!";
        try {

            Database database = new Database();
            Connection connection = database.Get_Connection();

            Project n = new Project();
            boolean b = n.insertEmpName(connection, empname,empsalary);

            if (b == true) {
                result = "Employee Added SuccessFully!!!!";

            } else {
                result = "Employee Already Exists!!!";
            }

        } catch (Exception e) {
            System.out.println(e);
        }

        return result;

    }

    @POST
    @Path("/justTesting")
    @Consumes(MediaType.APPLICATION_JSON)
    public Response myresponse(FeedObjects fb)
    {
        System.out.println("Name is "+fb.getEmpname());
        return Response.status(201).entity("Tested !!").build();

    }


}  

and here is my class with setters and getters of variables that I am passing as an parameter to the above method and calling by using its object i.e fb

package dto;

public class FeedObjects
{

private String empname;
private String empsalary;
public FeedObjects() {
    // TODO Auto-generated constructor stub
}
public String getEmpname() {
    return empname;
}
public void setEmpname(String empname) {
    this.empname = empname;
}
public String getEmpsalary() {
    return empsalary;
}
public void setEmpsalary(String empsalary) {
    this.empsalary = empsalary;
}

}  

Web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>FirstProject</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
    <servlet>
        <servlet-name>ServletAdaptor</servlet-name>
        <servlet-class>
            com.sun.jersey.server.impl.container.servlet.ServletAdaptor
            </servlet-class>
<init-param>
  <param-name>com.sun.jersey.config.property.packages</param-name>
  <param-value>webService</param-value>
</init-param>
            <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>ServletAdaptor</servlet-name>
        <url-pattern>/REST/*</url-pattern>
    </servlet-mapping>
</web-app>

Libraries included are as below:

  • asm-3.1.jar
  • gson-2.2.1.jar
  • jersey-client-1.0.3.jar
  • jersey-core-1.0.3.jar
  • jersey-server-1.0.3.jar
  • joda-time-2.0.jar
  • jsr311-api-1.0.jar
  • mysql-connector-java-5.0.8-bin.jar

So please help me to solve the error . I am a newbie to the Java REST web services but I am trying very hard to solve this .

2

Answers


  1. Hi Siddhesh Kalgaonkar,

    Check this out

    Login or Signup to reply.
  2. I solved my problem of sending data in the form of JSON to the POST url by adding gensonlibrary to my project jars. It provides methods to serialize Java objects to JSON and deserialize JSON streams to Java objects. It is a json<>java streaming and databinding api. It integrates well with jersey. Here is the Link
    Thank you Stackoverflow and Lathy for helping me 🙂

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search