2

I am working on an API using Jersey and wish to make it ready to deploy to Google App Engine. However, when I test on Postman, the GET function working but not the POST function. I only receive a short error message which is "Error 415 Unsupported Media Type" and I can't identify where is wrong.

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;
    }
}

Request Resource Class

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();
        }   
    }
}

Request Service Class

<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

Thanks in advance for everyone that helps me to point out and solve the problem and answer my question.

Yih Wei
  • 537
  • 3
  • 21

2 Answers2

0

add this dependecy

<dependency>
    <groupId>org.glassfish.jersey.media</groupId>
    <artifactId>jersey-media-json-jackson</artifactId>
    <version>JERSEY_VERSION</version>
</dependency>
Wilder Valera
  • 986
  • 9
  • 11
  • Hi. @WilderValera. I do not have pom.XML. Mind I asking how should I add that on my web.XML? Will add on a jar like step 3 from #HarishVashist will be sufficient? – Yih Wei Apr 26 '17 at 01:19
  • Harish's comment refers to a different package from yours so you should add the same as you, I mean org.glassfish.jersey and not com.sun.jersey. If I were you I would try to download the jar from maven repository (searching the dependency with the same Jersey version). Try it. – Wilder Valera Apr 26 '17 at 12:43
  • For example: https://mvnrepository.com/artifact/org.glassfish.jersey.media/jersey-media-json-jackson/2.22.1 , thats for Jersey 2.22.1. There you can see the dependency info and a link to download the jar. – Wilder Valera Apr 26 '17 at 12:48
0

This issue occures when the resource you are trying to hit is not being supplied with the proper Payload i.e. data which resource consumes is not well formed as expected Based upon the problem details shared the issue can be any one of the following:

  1. With the Postman client you are not specifying the the correct content-type. In your case it has to be Content-Type: application/json and Accept: application/json in REST Client header section.
  2. The issue is in the deserialization of the bean AppDataRequest, one of the reason appears that the bean class not annoted well with the @JsonCreater and @JsonProperty annotations because of which the well formed Json data of model AppDataRequest you are sending fails to get deserialized at resource side.
  3. If you are sure that you are point 1 and 2 are not applicable to you then make sure you have added the Jackson dependency which parses the Json objects like:

    com.sun.jersey jersey-json 1.8

You can follow the jersey implementation of Jax-Rs at: http://www.mkyong.com/webservices/jax-rs/json-example-with-jersey-jackson/

  • Hi. @HarishVashist Thanks for your help. I followed the first step and it is not working as it return me the same 415 error code. About the second step, I receive error of JsonCreater and JsonProperty cannot be resolved to a type. Should I import any jar for the two annotations? For the third step, I added the Jackson jar to my build path. For the link you included, I found out that com.sun.jersey.api.json.POJOMappingFeature no longer available for Jersey 2, it is true? – Yih Wei Apr 26 '17 at 01:18