即使在设置内容类型后,也会抛出415错误代码

时间:2016-10-11 14:47:04

标签: postgresql web-services heroku jersey jax-rs

我使用以下代码创建表:

CREATE TABLE   IF NOT EXISTS TRENDING (ID SERIAL   PRIMARY KEY , TOPIC TEXT , CATEGORY   TEXT ,QUESTION TEXT,ANSWER TEXT,PROFILEPIC TEXT NOT NULL,TICK TIMESTAMP,url TEXT);

我正在使用以下代码接受发布请求并将日期插入postgres db。

@POST
@Path("addTrending")
@Consumes({ MediaType.APPLICATION_JSON })
@Produces(MediaType.APPLICATION_JSON)
public static String addTrending(Trending trending){
    String songString = "requesting";
    ArrayList<Success> output = new ArrayList();

    try {

        Connection connection = MyResource.getConnection();
        PreparedStatement stmt = connection
        .prepareStatement("INSERT INTO TRENDING (TOPIC,CATEGORY,QUESTION,ANSWER,PROFILEPIC,TICK,URL) VALUES(?, ? , ? , ? , ? , ? , ? )");
        stmt.setString(1, trending.getTopic());
        stmt.setString(2, trending.getCategory());
        stmt.setString(3, trending.getQuestion());
        stmt.setString(4, trending.getAnswer());
        stmt.setString(5, trending.getProfilePic());
        stmt.setTimestamp(6, trending.getTimeStamp());
        stmt.setString(7, trending.getUrl());
        songString = "success";

    } catch(Exception ex){
        songString = "failure";
    }
    output.add(new Success(songString));

    return output.toString();
}

以下是我正在制作的JSON请求:

{
  "timeStamp": "2016-10-11 14:11:52.516596",
  "question": "what?",
  "answer": "yes",
  "profilepic": "assa",
  "topic": "Paul",
  "id": 2,
  "category": "FIlter",
  "url": "a"   
}

我已正确使用了消耗注释。我还将application/json设置为我的内容类型,但仍然得到:

  

HTTP状态415 - 不支持的媒体类型

我怎样才能解决这个问题?

1 个答案:

答案 0 :(得分:1)

添加此依赖

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