2

I am writing a RESTful API program to deployed on AWS. I am getting JSON output from the program. When I testing the program, it return the JSON output which is not same as the expected output. The orientation, upper case and lower case also not correct as I want it to be.

Expected Output:

[
  {
    "Username": "****",
    "UUID": "****",
    "Tenant_ID": "****",
    "Site_ID": "****"
  }
]

Output Received:

[
  {
    "UUID": "****",
    "site_ID": "****",
    "tenant_ID": "****",
    "userName": "****"
  }
]

Resource Code:

public List<Data> getData(){
    List<Data> list = new ArrayList<>();
    Connection conn = null;
    Statement stat = null;
    try{
        Class.forName("com.mysql.jdbc.Driver");
        conn = DriverManager.getConnection(DB_URL);
        stat = conn.createStatement();
        String sql = "SELECT * FROM data";
        ResultSet resu = stat.executeQuery(sql);
        while(resu.next()){
            String UserName = resu.getString("username");
            String UUID = resu.getString("UUID");
            String Tenant_ID = resu.getString("tenant_id");
            String Site_ID = resu.getString("site_id");
            list.add(new Data(UserName, UUID, Tenant_ID, Site_ID));
        }
        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();
        }
    }
    return list;
}

Service Code:

@Produces(MediaType.APPLICATION_JSON)
@GET
@Path("/v2")
public List<Data> getData(){
    return ds.getData();
}

Data Class:

public class Data {
    private String UserName;
    private String UUID;
    private String Tenant_ID;
    private String Site_ID;

    public Data(){

    }

    public Data(String userName, String uUID, String tenant_ID, String site_ID) {
        super();
        UserName = userName;
        UUID = uUID;
        Tenant_ID = tenant_ID;
        Site_ID = site_ID;
    }

    public Data(String uUID, String tenant_ID, String site_ID) {
        super();
        UUID = uUID;
        Tenant_ID = tenant_ID;
        Site_ID = site_ID;
    }

    @JsonProperty("Username")
    public String getUserName() {
        return UserName;
    }

    public void setUserName(String userName) {
        UserName = userName;
    }

    @JsonProperty("UUID")
    public String getUUID() {
        return UUID;
    }

    public void setUUID(String UUID) {
        this.UUID = UUID;
    }

    @JsonProperty("Tenant_ID")
    public String getTenant_ID() {
        return Tenant_ID;
    }

    public void setTenant_ID(String tenant_ID) {
        Tenant_ID = tenant_ID;
    }

    @JsonProperty("Site_ID")
    public String getSite_ID() {
        return Site_ID;
    }

    public void setSite_ID(String site_ID) {
        Site_ID = site_ID;
    }
}

Thanks for every member that help on this question.

Yih Wei
  • 537
  • 3
  • 21
  • Either you need to change code in rest api for returning JSON or accept that json and manipulate at your side. – Nitin Dhomse May 11 '17 at 05:22
  • Hi. @NitinDhomse Above is my code in REST API for return JSON and I wish to modify it to get expected output but I don't know where to modify it. – Yih Wei May 11 '17 at 05:26
  • Can you post your imports at the top of your `Data` class? I suspect you are importing a slightly wrong annotation so that `@JsonProperty` doesn't do what you're expecting. – Ryan Weir May 11 '17 at 05:35
  • Hi. @RyanWeir My import is `import com.fasterxml.jackson.annotation.JsonProperty;`. – Yih Wei May 11 '17 at 05:38

2 Answers2

0

There's a good chance you're importing the jackson annotation @JsonProperty annotation from the older org.codehaus.jackson package, when you want to be including only the newer version of jackson (which is in a different project) com.fasterxml.jackson into your project.

Check your dependencies for the older "codehaus" version, and update it to only reference the new one (this change should be forward-compatible with your code).

Then try a rebuild to see if it works as expected.

See here: https://stackoverflow.com/a/30782762/808532

Community
  • 1
  • 1
Ryan Weir
  • 6,377
  • 5
  • 40
  • 60
  • Hi. @RyanWeir Thanks for your reply. I checked my pom.xml. I included ` com.fasterxml.jackson.core jackson-annotations 2.3.2 `. I also unable to find any 'codehaus' on my pom.xml. – Yih Wei May 11 '17 at 07:31
  • It's possible that another dependency is causing Jackson v1 to be pulled in at build time - try running `mvn dependency:tree` in your project directory and search for any references to different versions to see if that's the case. – Ryan Weir May 11 '17 at 09:19
0

Try downgrading you jackson annotations to the older codehhaus version. When thus problem happens, there is a mix up with the version of jackson used by jersey and the version of annotations. Since your are using the new annotations, use the older ones. org.codehaus.jackson.annotate.JsonProperty

eDog
  • 173
  • 1
  • 5
  • Hi. @eDog Thanks for your answer. I tried to downgrading the fasterxml version to older codehhaus version. It is not working also. – Yih Wei May 11 '17 at 07:31