如何隐藏[“java.util.LinkedList”,[

时间:2017-04-25 15:00:08

标签: java android eclipse jackson

如何隐藏[“java.util.LinkedList”,[

您好我需要使用网络服务器将我的xamarin表单应用程序连接到我的数据库但是当我执行我的请求时,返回看起来像这样

 {
  "id" : "123",
  "items" : [ "java.util.LinkedList", [ {
    "id" : 123,
    "codeItem" : "I123",
    "description" : "Item123",
    "quantite" : 1.0,
    "prixUnitaire" : 250.0
  }, {
    "id" : 456,
    "codeItem" : "I456",
    "description" : "Item456",
    "quantite" : 8.0,
    "prixUnitaire" : 5.0
  } ] ],
  "client" : {
    "id" : "123",
    "name" : "user1",
    "address" : "userAdress",
    "surnom" : "userSurnom",
    "age" : 18,
    "dateDeFete" : "1998-08-26 09:59:43.904"
  }
}

你知道如何隐藏[“java.util.LinkedList”,[

package com.rest.jersey2;

import java.util.LinkedList;
import java.util.List;

import com.fasterxml.jackson.annotation.JsonIgnore;

public class Soumission {

    private String id;
    private List<SoumissionItem> items;
    private Client client;

    public Soumission(){
        this.items = new LinkedList<SoumissionItem>();
    }

    public String getId() {
        return id;
    }
    public void setId(String id) {
        this.id = id;
    }
    public Client getClient() {
        return client;
    }
    public void setClient(Client client) {
        this.client = client;
    }
    public List<SoumissionItem> getItems() {
        return items;
    }
    public void setItems(List<SoumissionItem> items) {
        this.items = items;
    }
    @JsonIgnore
    public double getTotal() {
        if (this.items != null){
            double total =  0d;
            for (SoumissionItem item: this.items){
                total += item.getTotal();
            }

            return total;           
        }
        return 0d;
    }



}

这是我提出的要求

package com.rest.jersey2;

import java.util.Calendar;
import java.util.GregorianCalendar;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status;

@Path("/xmlbasedreverser")
public class XMLBasedReverser {

    @GET
    @Produces("application/json")
    public Response defaultReverser() {
        try {
            Soumission soumission = new Soumission();
            soumission.setId("123");
            Client client = new Client();
            client.setID("123");
            client.setName("user1");
            client.setAddress("userAdress");
            client.setSurnom("userSurnom");
            client.setAge(18);
            Calendar cal = GregorianCalendar.getInstance();
            cal.set(1998, 7, 26);
            client.setDateDeFete(cal.getTime());
            soumission.setClient(client);


            SoumissionItem item1 = new SoumissionItem();
            item1.setId(123);
            item1.setCodeItem("I123");
            item1.setDescription("Item123");
            item1.setQuantite(1);
            item1.setPrixUnitaire(250d);
            soumission.getItems().add(item1);


            SoumissionItem item2 = new SoumissionItem();

            item2.setId(456);
            item2.setCodeItem("I456");
            item2.setDescription("Item456");
            item2.setQuantite(8);
            item2.setPrixUnitaire(5d);
            soumission.getItems().add(item2);

            return Response.ok(soumission).type(MediaType.APPLICATION_JSON).build();
        } catch (Exception e) {
            throw new WebApplicationException(Status.INTERNAL_SERVER_ERROR);
        }
        // StringBuilder sb = new StringBuilder();
        // sb.append("ANKARA");
        //
        // JSONObject jsonObject = new JSONObject();
        // jsonObject.put("original", sb.toString());
        // jsonObject.put("reversed", sb.reverse().toString());
        //
        // String result = "" + jsonObject;
        // return Response.status(200).entity(result).build();
    }

    @Path("{word}")
    @GET
    @Produces("application/json")
    public Response reverser(@PathParam("word") String word) {

//      StringBuilder sb = new StringBuilder();
//      sb.append(word);
//
//      JSONObject jsonObject = new JSONObject();
//      jsonObject.put("original", sb.toString());
//      jsonObject.put("reversed", sb.reverse().toString());
//
//      String result = "" + jsonObject;
//      return Response.status(200).entity(result).build();
        return Response.ok().build();
    }

}

1 个答案:

答案 0 :(得分:0)

我的问题的答案是你通过linkedList重新列出所有列表,因为jackson尝试读取它们并解析它们。

package com.rest.jersey2;

import java.util.LinkedList;
import java.util.List;

import com.fasterxml.jackson.annotation.JsonIgnore;

public class Soumission {

    private String id;
    private List<SoumissionItem> items;
    private Client client;

    public Soumission(){
        this.items = new LinkedList<SoumissionItem>();
    }

    public String getId() {
        return id;
    }
    public void setId(String id) {
        this.id = id;
    }
    public Client getClient() {
        return client;
    }
    public void setClient(Client client) {
        this.client = client;
    }
    public List<SoumissionItem> getItems() {
        return items;
    }
    public void setItems(List<SoumissionItem> items) {
        this.items = items;
    }
    @JsonIgnore
    public double getTotal() {
        if (this.items != null){
            double total =  0d;
            for (SoumissionItem item: this.items){
                total += item.getTotal();
            }

            return total;           
        }
        return 0d;
    }



}

你像他们那样哄骗他们

package com.rest.jersey2;

import java.util.LinkedList;

import com.fasterxml.jackson.annotation.JsonIgnore;

public class Soumission {

    private String id;
    private LinkedList<SoumissionItem> items;

    private Client client;

    public Soumission(){
        this.items = new LinkedList<SoumissionItem>();
    }

    public String getId() {
        return id;
    }
    public void setId(String id) {
        this.id = id;
    }
    public Client getClient() {
        return client;
    }
    public void setClient(Client client) {
        this.client = client;
    }

    public LinkedList<SoumissionItem> getItems() {
        return items;
    }

    public void setItems(LinkedList<SoumissionItem> items) {
        this.items = items;
    }
    @JsonIgnore
    public double getTotal() {
        if (this.items != null){
            double total =  0d;
            for (SoumissionItem item: this.items){
                total += item.getTotal();
            }

            return total;           
        }
        return 0d;
    }



}