簡體   English   中英

Jersey測試框架中的UnrecognizedPropertyException

[英]UnrecognizedPropertyException in Jersey Test Framework

我是Jersey測試框架的新手。 我正在嘗試在項目中使用jersey測試框架來實現測試用例。 我有一個REST服務,其網址將是:

http://localhost:8080/EzSupportBackend/a/dealerservices/getdealerdetails/FAB/ACZOKuBmtvyni16eMJ3AoSVg_HxL3bh3Lz0WiWNJhXudh9M90LSc8bDHD-Y2JpcbISZcC_DM2PL4yqSsmXUKA65ZmvuoiQ_wotgU1OvA8GGw_yPMwVnXGg==

我嘗試使用以下代碼使用Jersey測試此服務:

public class DealerServicesTest extends JerseyTest{
@Override
protected AppDescriptor  configure() {
    return new WebAppDescriptor.Builder().build();
}

@Test
public void testGetDealerDetails() throws JSONException,URISyntaxException {
    WebResource webResource = client().resource("http://localhost:8080/");
    JSONObject json = webResource.path("EzSupportBackend/a/dealerservices/getdealerdetails/FAB/ACZOKuBmtvyni16eMJ3AoSVg_HxL3bh3Lz0WiWNJhXudh9M90LSc8bDHD-Y2JpcbISZcC_DM2PL4yqSsmXUKA65ZmvuoiQ_wotgU1OvA8GGw_yPMwVnXGg==").get(JSONObject.class);

    assertEquals("ONLINE", json.get("companyType"));
    assertEquals("FabFurnish", json.get("companyName"));        
    assertEquals("Gurgoan,Haryana, India", json.get("companyAddress"));
    assertEquals("04222456803", json.get("phoneNumber"));
    assertEquals("ACTIVE", json.get("status"));     
    assertEquals("customerservice@fabfurnish.com", json.get("emailId"));
}
}

當我通過Postman(Chrome擴展程序)測試服務時,我得到如下正確答復:

{
  "companyType": "ONLINE",
  "companyName": "FabFurnish",
  "companyAddress": "Gurgoan,Haryana, India",
  "companyLocation": null,
  "longitude": "77.023419",
  "phoneNumber": "04222456803",
  "serviceRating": 0,
  "repairRating": 0,
  "warrantyRating": 0,
  "shopRating": 0,
  "status": "ACTIVE",
  "createdOn": 1446613095557,
  "updatedOn": 1446613095557,
  "companyId": "FAB",
  "lattitde": "28.47427",
  "emailId": "customerservice@fabfurnish.com",
  "createdby": null,
  "updatedby": null
}

但是通過此測試,我得到以下異常:

com.sun.jersey.api.client.ClientHandlerException:     com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field "companyType" (class org.json.JSONObject), not marked as ignorable (0 known properties: ])
 at [Source: sun.net.www.protocol.http.HttpURLConnection$HttpInputStream@45b4c3a9; line: 1, column: 17] (through reference chain: org.json.JSONObject["companyType"])
    at com.sun.jersey.api.client.ClientResponse.getEntity(ClientResponse.java:563)
    at com.sun.jersey.api.client.ClientResponse.getEntity(ClientResponse.java:506)
    at com.sun.jersey.api.client.WebResource.handle(WebResource.java:674)
    at com.sun.jersey.api.client.WebResource.get(WebResource.java:191)
    at com.vs.mhs.ezsupport.services.DealerServicesTest.testGetDealerDetails(DealerServicesTest.java:27)

任何人都可以幫助我找出原因嗎?

編輯:我的代碼如下所示:

@PermitAll
@GET
@Path("/getdealerdetails/{companyId}/{token_id}")
public Response getDealerDetails(@PathParam("companyId") String companyId, @Context SecurityContext userContext, @Context HttpServletRequest request){
    boolean isUserAuthorised = isUserAuthenticated(userContext);
    DealerDetails dealer = null;
    DealerDetailsView getDealerView = null;
    if(isUserAuthorised){
        EntityManager em = (EntityManager) request.getAttribute(FilterConstants.ENTITYMANAGER);
        DealerDetailsBDL dealerbdl = new DealerDetailsBDL(em);
        dealer = dealerbdl.getDealerDetails(companyId);
        getDealerView = new DealerDetailsView(dealer);
    }
    return Response.ok(getDealerView).build();          
}

DealerDetailsView是一個具有私有變量的類,用於下面列出的屬性以及getter和setter:

private String companyid;
private String companyType;
private String companyName;
private String companyAddress;
private String companyLocation;
private String lattitude;
private String longitude;
private String phoneNumber;
private String emailid;
private int serviceRating;
private int repairRating;
private int warrantyRating;
private int shopRating;
private String status;

Jackson是JSON提供程序,Jackson通常使用模型POJO,而JSONObject不是。 傑克遜(Jackson)正在JSONObject尋找companyType屬性,而該屬性沒有。 這就是為什么您要獲得例外。 如果您沒有專門用於該JSON的POJO,則只需將其作為字符串獲取,然后使用該字符串創建JSONObject

String jsonString = webResource...get(String.class);
JSONObject json = new JSONObject(jsonString);

更新

Jackson的POJO只是一個將JSON字段映射到類屬性的類(遵循JavaBean命名約定)。 例如,對於這兩個JSON字段

"companyType": "ONLINE",
"companyName": "FabFurnish"

你可以像

public class CompanyInfo {
    private String companyType;
    private String companyName;

    public CompanyInfo() {}

    public String getCompanyInfo() { return companyInfo; }
    public void setCompanyInfo(String info) { this.companyInfo = info; }

    public String getCompanyType() { return companyType; }
    public void setCompanyType(String type) { this.companyType = type; }
}

為了使JSON字段與Java屬性匹配,屬性(getter / setter)應以get/set和字段的確切名稱為前綴,首字母大寫,如上所示。 因此,只需使用其他所有JSON字段完成POJO,然后序列化就可以與CompanyInfo

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM