简体   繁体   English

使用海报扩展在 Java REST Web 服务中使用 POST 时出现 415 错误

[英]415 Error when using POST in Java REST web services using poster extension

I am passing JSON data like我正在传递 JSON 数据,例如

{
    "empname" : "seo"
}  

to the POST url but it returns 415 error ie Unsupported media typePOST url 但它返回 415 错误,即不支持的媒体类型
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.故障排除后,我发现海报扩展中的内容类型应该是“application/json” ,我尝试了相同但没有奏效。
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 ie fb这是我的类,其中包含变量的 setter 和 getter,我将它们作为参数传递给上述方法并使用其对象(即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

<?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 asm-3.1.jar
  • gson-2.2.1.jar gson-2.2.1.jar
  • jersey-client-1.0.3.jar球衣客户端1.0.3.jar
  • jersey-core-1.0.3.jar球衣核心1.0.3.jar
  • jersey-server-1.0.3.jar jersey-server-1.0.3.jar
  • joda-time-2.0.jar joda-time-2.0.jar
  • jsr311-api-1.0.jar jsr311-api-1.0.jar
  • mysql-connector-java-5.0.8-bin.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 .我是 Java REST Web 服务的新手,但我正在努力解决这个问题。

I solved my problem of sending data in the form of JSON to the POST url by adding genson library to my project jars.通过将genson库添加到我的项目 jar 中,我解决了以 JSON 形式将数据发送到 POST url 的问题。 It provides methods to serialize Java objects to JSON and deserialize JSON streams to Java objects.它提供了将 Java 对象序列化为 JSON 并将 JSON 流反序列化为 Java 对象的方法。 It is a json<>java streaming and databinding api.它是一个 json<>java 流和数据绑定 API。 It integrates well with jersey.它与球衣很好地融合在一起。 Here is the Link这是链接
Thank you Stackoverflow and Lathy for helping me :)感谢 Stackoverflow 和 Lathy 帮助我:)

Hi Siddhesh Kalgaonkar,嗨,Siddhesh Kalgaonkar,

Check this out 看一下这个

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM