获取工作POST不起作用

时间:2017-04-25 09:03:15

标签: java json rest google-app-engine jersey

我正在使用Jersey开发API,并希望将其部署到Google App Engine。但是,当我在Postman上测试时,GET功能工作但不是POST功能。我只收到一条简短的错误消息,即"错误415不支持的媒体类型"我无法确定哪里出错了。

package com.yihwei95.gatewaynetworkinterface.resource;

import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

import com.yihwei95.gatewaynetworkinterface.model.AppDataRequest;
import com.yihwei95.gatewaynetworkinterface.service.AppDataService;

@Path("/v1/")
public class AppDataResource {
    AppDataService ads = new AppDataService();

    @Produces(MediaType.APPLICATION_JSON)
    @GET
    @Path("/v2")
    public String getAppData(){
        return "No";
    }

    @Consumes(MediaType.APPLICATION_JSON)
    @Produces(MediaType.APPLICATION_JSON)
    @POST
    @Path("/v3")
    public Response getSAppData(AppDataRequest adr) {
        Response data = ads.getSAppData(adr.getId(), adr.getEmail(), adr.getPassword());
        return data;
    }
}

请求资源类

package com.yihwei95.gatewaynetworkinterface.service;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.ws.rs.core.Response;

import com.google.gson.Gson;
import com.yihwei95.gatewaynetworkinterface.model.AppData;
import com.yihwei95.gatewaynetworkinterface.model.AppDataRequest;
import com.yihwei95.gatewaynetworkinterface.model.Data;

public class AppDataService {
    Map<AppDataRequest, Data> DataHM = new HashMap<>();

    static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
    static final String DB_URL = "jdbc:mysql://****:3306/demo";

    static final String USER = "****";
    static final String PASS = "****";

    public AppDataService(){
        Connection conn = null;
        Statement stat = null;
        try{
            Class.forName("com.mysql.jdbc.Driver");
            conn = DriverManager.getConnection(DB_URL, USER, PASS);
            stat = conn.createStatement();
            String sql = "SELECT * FROM testdata";
            ResultSet resu = stat.executeQuery(sql);
            while(resu.next()){
                int id = resu.getInt("app_id");
                String email = resu.getString("email");
                String password = resu.getString("password");
                String token = resu.getString("token");
                DataHM.put(new AppDataRequest(id, email, password), new Data(token));
            }
            resu.close();
            stat.close();
            conn.close();
        }
        catch(SQLException se){
            se.printStackTrace();
        }
        catch(Exception e){
            e.printStackTrace();
        }
        finally{
            try{
                if(stat!=null){
                    stat.close();
                }
            }
            catch (SQLException se2){
                se2.printStackTrace();
            }
            try{
                if(conn!=null){
                    conn.close();
                }
            }
            catch(SQLException se3){
                se3.printStackTrace();
            }
        }       
    }

    public Response getSAppData(int id, String email, String password){ 
        Map<String, AppData> AppDataHM = new HashMap<>(); 
        Map<String, Data> DataHM1 = new HashMap<>();
        Map<String, List<String>> DataHM2 = new HashMap<>();
        HashMap<Object, Object> ADHMDHM = new HashMap<>();

        List<String> message = new ArrayList<>();
        Data data = DataHM.get(new AppDataRequest (id, email, password));
        List<String> data2 = new ArrayList<>();

        if(data != null){
            message.add("");
            AppDataHM.put("AppData", new AppData("success", message));
            DataHM1.put("Data", data);
            ADHMDHM.putAll(AppDataHM);
            ADHMDHM.putAll(DataHM1);
            String ADHMDHM1 = new Gson().toJson(ADHMDHM);
            return Response.status(200).entity(ADHMDHM1).build();
        }
        else{
            message.add("Your login information is invalid. Please try with the correct information");
            AppDataHM.put("AppData", new AppData("error", message));
            DataHM2.put("Data", data2);
            ADHMDHM.putAll(AppDataHM);
            ADHMDHM.putAll(DataHM2);
            String ADHMDHM2 = new Gson().toJson(ADHMDHM);
            return Response.status(200).entity(ADHMDHM2).build();
        }   
    }
}

申请服务类

<servlet>
    <servlet-name>Jersey Web Application</servlet-name>
    <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
    <init-param>
        <param-name>jersey.config.server.provider.packages</param-name>
        <param-value>com.yihwei95.gatewaynetworkinterface.resource</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>Jersey Web Application</servlet-name>
    <url-pattern>/api/*</url-pattern>
</servlet-mapping>

WEB.XML

POSTman Error

提前感谢所有帮助我指出并解决问题并回答我的问题的人。

2 个答案:

答案 0 :(得分:0)

添加此依赖

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

答案 1 :(得分:0)

当您尝试命中的资源没有提供正确的Payload时,即资源消耗不正确的数据时,会出现此问题 根据共享的问题详细信息,问题可以是以下任何一种:

  1. 使用Postman客户端,您没有指定正确的内容类型。在您的情况下,它必须是REST客户端标题部分中的Content-Type: application/jsonAccept: application/json
  2. 问题在于bean AppDataRequest的反序列化,其中一个原因似乎是bean类没有使用@JsonCreater@JsonProperty注释很好地注释,因为这些注释很好您发送的模型AppDataRequest的Json数据无法在资源端进行反序列化。
  3. 如果您确定第1点和第2点不适用于您,请确保添加了解析Json对象的 Jackson 依赖项,如:

        com.sun.jersey     球衣,JSON     1.8

  4. 您可以在http://www.mkyong.com/webservices/jax-rs/json-example-with-jersey-jackson/

    上关注Jax-R的运动衫实施