错误415不支持的媒体类型:如果是JSON,则POST不会到达REST,但如果是XML则不会

mze*_*eba 74 java rest post json web-services

我实际上是REST WS的新手,但我真的不明白415 Unsupported Media Type.

我正在使用Firefox上的Poster测试我的REST,GET对我来说工作正常,但是POST(当它是a时application/xml)但是当我尝试application/json它根本没有到达WS时,服务器拒绝它.

这是我的URL:http:// localhost:8081/RestDemo/services/customers/add

这是JSON我发送的:{"name": "test1", "address" :"test2"}

这是XML我发送的:

<customer>
    <name>test1</name>
    <address>test2</address>
</customer>
Run Code Online (Sandbox Code Playgroud)

这是我的资源类:

@Produces("application/xml")
@Path("customers")
@Singleton
@XmlRootElement(name = "customers")
public class CustomerResource {

    private TreeMap<Integer, Customer> customerMap = new TreeMap<Integer, Customer>();

    public  CustomerResource() {
        // hardcode a single customer into the database for demonstration
        // purposes
        Customer customer = new Customer();
        customer.setName("Harold Abernathy");
        customer.setAddress("Sheffield, UK");
        addCustomer(customer);
    }

    @GET
    @XmlElement(name = "customer")
    public List<Customer> getCustomers() {
        List<Customer> customers = new ArrayList<Customer>();
        customers.addAll(customerMap.values());
        return customers;
    }

    @GET
    @Path("/{id}")
    @Produces("application/json")
    public String getCustomer(@PathParam("id") int cId) {
        Customer customer = customerMap.get(cId); 
        return  "{\"name\": \" " + customer.getName() + " \", \"address\": \"" + customer.getAddress() + "\"}";

    }

    @POST
    @Path("/add")
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
    public String addCustomer(Customer customer) {
         //insert 
         int id = customerMap.size();
         customer.setId(id);
         customerMap.put(id, customer);
         //get inserted
         Customer result = customerMap.get(id);

         return  "{\"id\": \" " + result.getId() + " \", \"name\": \" " + result.getName() + " \", \"address\": \"" + result.getAddress() + "\"}";
    }

}
Run Code Online (Sandbox Code Playgroud)

编辑1:

这是我的Customer类:

@XmlRootElement 
public class Customer implements Serializable {

    private int id;
    private String name;
    private String address;

    public Customer() {
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

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

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

}
Run Code Online (Sandbox Code Playgroud)

小智 129

添加Content-Type: application/jsonAccept: application/json 在REST客户端标题部分

  • REST客户端标题部分在哪里? (4认同)
  • 对于那些从另一种语言到达这里并且该语言是Ruby并且您正在使用rest-client的人来说,这个答案对我有帮助.示例用法`response = RestClient.post"https://pod12.salesforce.com/services/data/v33.0/sobjects/Contact/",{Email:user.email,LastName:user.last_name,FirstName:user. first_name,电话:user.phone} .to_json,{授权:"Bearer#{access_token}",:"Content-Type"=>"application/json",:"Accept"=>"application/json"}` (2认同)

unl*_*udo 20

问题在于bean Customer的反序列化.您的程序知道如何在XML中使用JAXB,而Daniel正在编写,但很可能不知道如何在JSON中执行它.

这里有一个Resteasy/Jackson的例子 http://www.mkyong.com/webservices/jax-rs/integrate-jackson-with-resteasy/

与泽西岛相同:http: //www.mkyong.com/webservices/jax-rs/json-example-with-jersey-jackson/


Max*_*gee 16

以防这对其他人有帮助,这是我的轶事:

我发现这个线程是我在使用Postman将测试数据发送到我的RESTEasy服务器时遇到的问题的结果,其中 - 在重大代码更改之后 - 我得到的只是415不支持的媒体类型错误.

长话短说,我把所有东西都撕掉了,最后我试着运行我知道工作的琐碎文件上传示例; 它没有.那是我意识到问题出在我的邮递员请求上的时候.我通常不发送任何特殊标题,但在之前的测试中我添加了"Content-Type":"application/json"标题.当然,我试图上传"multipart/form-data".删除它解决了我的问题.

道德:在炸毁你的世界之前检查你的标题.;)

  • 为"确保烤面包机已插入"的建议+1.刚遇到同样的问题,当您确信问题出现在代码中时很容易被忽视. (3认同)

小智 5

就我而言,缺少以下依赖项:

<dependency>
  <groupId>io.quarkus</groupId>
  <artifactId>quarkus-resteasy-reactive-jackson</artifactId>
</dependency>
Run Code Online (Sandbox Code Playgroud)