POST到Jersey REST服务获取错误415不支持的媒体类型 [英] POST to Jersey REST service getting error 415 Unsupported Media Type

查看:165
本文介绍了POST到Jersey REST服务获取错误415不支持的媒体类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用带有Jersey和Tomcat的JAX-RS Web应用程序。获取请求很好但是当我尝试发布JSON时,我得到HTTP状态415 - 不支持的媒体类型。

I am using a JAX-RS web application with Jersey and Tomcat. Get requests are fine however when I try to post JSON I get an HTTP status 415 - Unsupported Media Type.

这是我的简单HelloWorld.java:

Here is my simple HelloWorld.java:

package service;

import javax.ws.rs.*;

@Path("hello")
public class HelloWorld {
    @GET
    @Produces("text/plain")
    public String get() {
        return "hello world";
    }

    @POST
    @Consumes("application/json")
    public String post(JS input) {
        return input.hello;
    }

    public static class JS {
        public String hello;
    }
}

这是我在Postman尝试的请求(带'' application / json'header):

Here is the request I try in Postman (with 'application/json' header):


以下是带库的项目布局:

Here is the project layout with libraries:

我正在使用:


  • Java 7 x64

  • Jersey 2.17

  • Tomcat 7.0.62 x64

谢谢!

推荐答案

Jersey发行版没有JSON / POJO支持。您需要添加依赖项/ jar。

The Jersey distribution doesn't come with JSON/POJO support out the box. You need to add the dependencies/jars.

添加所有这些

  • jersey-media-json-jackson-2.17
  • jackson-jaxrs-json-provider-2.3.2
  • jackson-core-2.3.2
  • jackson-databind-2.3.2
  • jackson-annotations-2.3.2
  • jackson-jaxrs-base-2.3.2
  • jackson-module-jaxb-annotations-2.3.2
  • jersey-entity-filtering-2.17

随着下面的Maven将在上面提取所有上述内容

With Maven, below will pull all the above in

<dependency>
    <groupId>org.glassfish.jersey.media</groupId>
    <artifactId>jersey-media-json-jackson</artifactId>
    <version>2.17</version>
</dependency>

对于未使用Jersey 2.17(并直接使用罐子代替Maven)的未来读者,你可以去此处查找您正在使用的Jersey版本,并查看传递版本您需要的依赖版本。此泽西岛依赖的当前版本使用Jackson 2.3.2。这是你需要注意的主要事项。

For any future readers not using Jersey 2.17 (and using jars directly instead of Maven), you can go here to find the Jersey version you are using, and see what transitive dependency versions you need. The current version of this Jersey dependency uses Jackson 2.3.2. That's the main thing you need to look out for.

这篇关于POST到Jersey REST服务获取错误415不支持的媒体类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆