2013-10-04 2 views
0

Я работаю над услугами службы отдыха в Джерси. пытаясь передать родовой объект (в моем случае AppObject) как пост запрос и ответ от сервера также является родового объекта (в моем случае AppObject) я ожидал строку {"license":"12345","list":[{"alternateId":"AlternateID","classificati‌​on":"1"}] } но InstEd из списка я получение dimRequirement, как показано ниже. Поэтому при разборе json я получаю исключение. Есть ли способ получить это как сам список без изменения кода. Или любая альтернатива может мне помочь.Нераспознанное поле, не обозначенное как неосведомленное, при разборе строки json

Json строка приема на клиенте

{"license":"12345","dimRequirement":[{"alternateId":"AlternateID","classificati‌​on":"1"}] } 

мой клиент

AppObject<DimRequirement> appObject = new AppObject<DimRequirement>(); 
     appObject.setClientKey(4L); 
     Client client = Client.create(); 
     WebResource webResource = client 
        .resource("http://localhost:8080/myproject/"); 

     JSONObject json = new JSONObject(appObject); 
     ClientResponse response = webResource.path("rest").path("requirement/getreq").type("application/json").accept("application/json") 
       .post(ClientResponse.class,json.toString()); 
     String output = response.getEntity(String.class); 
     System.out.println(output); 
     appObject= new ObjectMapper().readValue(
       output, AppObject.class); 

ошибка

Unrecognized field "dimRequirement" (Class com.vxl.AppObject), not marked as ignorable 
at [Source: [email protected]; line: 1, column: 49] (through reference chain: com.vxl.appanalytix.AppObject["dimRequirement"]) 
    at org.codehaus.jackson.map.exc.UnrecognizedPropertyException.from(UnrecognizedPropertyException.java:53) 
    at org.codehaus.jackson.map.deser.StdDeserializationContext.unknownFieldException(StdDeserializationContext.java:244) 
    at org.codehaus.jackson.map.deser.StdDeserializer.reportUnknownProperty(StdDeserializer.java:589) 
    at org.codehaus.jackson.map.deser.StdDeserializer.handleUnknownProperty(StdDeserializer.java:575) 
    at org.codehaus.jackson.map.deser.BeanDeserializer.handleUnknownProperty(BeanDeserializer.java:684) 
    at org.codehaus.jackson.map.deser.BeanDeserializer.deserializeFromObject(BeanDeserializer.java:515) 
    at org.codehaus.jackson.map.deser.BeanDeserializer.deserialize(BeanDeserializer.java:351) 
    at org.codehaus.jackson.map.ObjectMapper._readMapAndClose(ObjectMapper.java:2131) 
    at org.codehaus.jackson.map.ObjectMapper.readValue(ObjectMapper.java:1402) 
    at com.vxl.CheckJersy.main(CheckJersy.java:56) 

Общий класс

@XmlRootElement 
@XmlSeeAlso({DimRequirement.class }) 
public class AppObject<T> implements Serializable { 

    private List<T> list; 
    private Long clientKey; 

    public AppObject() { 
     list = new ArrayList<T>(); 

    } 

    public AppObject(List<T> list) { 
     this.list = list; 
    } 

    @XmlAnyElement(lax = true) 
    public List<T> getList() { 
     return list; 
    } 

    public void setList(List<T> list) { 
     this.list = list; 
    } 

    public Long getClientKey() { 
    return clientKey; 
} 

public void setClientKey(Long clientKey) { 
    this.clientKey = clientKey; 
} 

} 

службы

@Path("/requirement") 
public class DimRequirementManagerImpl { 
@POST 
    @Consumes({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON }) 
    @Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON }) 
    @Path("/getreq") 
    @Override 
    public AppObject getAllByClientNIsCurrent(
      AppObject<DimRequirement> appObj) { 
     List<DimRequirement> dimreqlist = dimRequirementDao 
       .getAllByClientNIsCurrent(appObj.getClientKey()); 
     AppObject appObject = new AppObject(); 
     appObject.setList(dimreqlist); 
     return appObject; 
    }} 

DimRequirement, который устанавливает, чтобы AppObject

@XmlRootElement 
public class DimRequirement extends BaseObject implements Serializable { 
private Long requirementKey; 
private String description; 
private String priority; 
@Id 
    @GeneratedValue(strategy=GenerationType.AUTO) 
    @Column(name="requirementKey") 
    public Long getRequirementKey() { 
     return this.requirementKey; 
    }  
    public void setRequirementKey(Long requirementKey) { 
     this.requirementKey = requirementKey; 
    } 
@Column(name="Description") 
    public String getDescription() { 
     return this.description; 
    }  
    public void setDescription(String description) { 
     this.description = description; 
    } 
@Column(name="Priority") 
    public String getPriority() { 
     return this.priority; 
    }  
    public void setPriority(String priority) { 
     this.priority = priority; 
    } 
} 

ответ

0

я изменил тип возврата службы в строку (JSON строку).

сервис

@Path("/requirement") 
public class DimRequirementManagerImpl { 
@POST 
@Consumes({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON }) 
@Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON }) 
@Path("/getreq") 
@Override 
public String getAllByClientNIsCurrent(AppObject<DimRequirement> appObj) { 
try { 
    List<DimRequirement> dimreqlist = dimRequirementDao.getAllByClientNIsCurrent(appObj.getClientKey()); 
    AppObject appObject = new AppObject(); 
    appObject.setList(dimreqlist); 
    JSONObject jsonget = new JSONObject(appObject); 
    return jsonget.toString(); 
    }catch (Exception e) { 
     AppObject<DimRequirement> appObject = new AppObject<DimRequirement>(); 
     appObject.setLicense("12345"); 
     JSONObject jsonget = new JSONObject(appObject); 
     return jsonget.toString(); 
    } 
}} 

это работает для того же клиента в вопросе returnig JSON { "лицензировать": "список" "12345",: [{ "alternateId": "AlternateID",» classificati на ":" 1" }]}

клиент

AppObject<DimRequirement> appObject = new AppObject<DimRequirement>(); 
     appObject.setClientKey(4L); 
     Client client = Client.create(); 
     WebResource webResource = client 
        .resource("http://localhost:8080/myproject/"); 

     JSONObject json = new JSONObject(appObject); 
     ClientResponse response = webResource.path("rest").path("requirement/getreq").type("application/json").accept("application/json") 
       .post(ClientResponse.class,json.toString()); 
     String output = response.getEntity(String.class); 
     AppObject<DimRequirement> appObjectclientns = new ObjectMapper() 
       .readValue(output, 
         new TypeReference<AppObject<DimRequirement>>() { 
         }); 
Смежные вопросы