Jersey 测试框架中的 UnrecognizedPropertyException

UnrecognizedPropertyException in Jersey Test Framework

我是 Jersey 测试框架的新手。我正在尝试在我的项目中使用球衣测试框架来实现测试用例。我有一个 REST 服务,其 url 将是:

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 是一个 class,其中包含我在下面列出的属性的私有变量以及 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 只是一个 class,它将 JSON 字段映射到 class 属性(遵循 JavaBean 命名约定)。例如,对于这两个 JSON 字段

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

你可以class喜欢

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.