我正在传递JSON数据{ "empname" : "seo"}到POST网址,但返回415错误,即不受支持的媒体类型故障排除后,我发现海报扩展中的内容类型应为“ application / json”,我尝试了相同的操作,但没有成功。以下是我的服务代码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(); }}这是我的带有变量的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 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>包含的库如下:asm-3.1.jargson-2.2.1.jarjersey-client-1.0.3.jarjersey-core-1.0.3.jarjersey-server-1.0.3.jarjoda-time-2.0.jarjsr311-api-1.0.jarmysql-connector-java-5.0.8-bin.jar因此,请帮助我解决错误。我是Java REST Web服务的新手,但我正在努力解决这一问题。 最佳答案 通过将genson library添加到我的项目jar中,我解决了将JSON格式的数据发送到POST URL的问题。它提供了将Java对象序列化为JSON并将JSON流反序列化为Java对象的方法。它是一个json java流和数据绑定api。它与球衣很好地集成在一起。这是Link谢谢Stackoverflow和Lathy对我的帮助:)关于java - 415在使用海报扩展程序的Java REST Web服务中使用POST时出错,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/36194249/ 10-10 15:07