简体   繁体   English

clientResponse.getEntity(String.class)返回HTML而不是JSON

[英]clientResponse.getEntity(String.class) returns HTML instead of JSON

I'm trying to fetch an object of PreRegisterJsonResponse through web service with jersey client by sending preRegisterId as path variable to the service, tested it on Advanced REST client and Postman - it works but when I consume web service through jersey client from java code I get HTML returned instead of JSON. 我正在尝试通过使用jersey客户端通过Web服务获取PreRegisterJsonResponse的对象, PreRegisterJsonResponse是将preRegisterId作为路径变量发送到服务,并在Advanced REST客户端和Postman上对其进行了测试-它可以正常工作,但是当我通过jersey客户端从Java代码使用Web服获取HTML而不是JSON。

Here's the code 这是代码

WebService - 网络服务 -

@GET
@ResponseBody
@RequestMapping("/userJSON2/{preRegisterId}")
public PreRegisterJsonResponse userJson2(@PathVariable long preRegisterId){
    PreRegisterJsonResponse pjr = new PreRegisterJsonResponse();
    try{
        PreRegister preRegUser = preRegisterDao.get(preRegisterId);
        pjr.setFirstName(preRegUser.getFirstName());
        pjr.setMiddleName(preRegUser.getMiddleName());
        pjr.setLastName(preRegUser.getLastName());
        pjr.setLoginId(preRegUser.getLoginId());
        pjr.setRoleId(preRegUser.getRole().getId());
        pjr.setUrlCode(preRegUser.getUrlCode());
        pjr.setCreatedBy(preRegUser.getCreatedBy().toString());
        if(preRegUser.getInvitedUnder() != null){
            pjr.setInvitedUnderId(preRegUser.getInvitedUnder().getId());
        }else{
            pjr.setInvitedUnderId(0);
        }
        pjr.setSkipBiometrics(preRegUser.isSkipBiometrics());
        pjr.setMainRecordOfficer(preRegUser.getMainRecordOfficer());
        pjr.setEmployeeId(preRegUser.getEmployeeId());
        if(preRegUser.getFond() != null){
            pjr.setFondId(preRegUser.getFond().getId());
        }else{
            pjr.setFondId(0);
        } 
    } catch(Exception ex){
        logger.error("Exception in WebServiceController:userJson2() "+ex.getMessage());
    }

    return pjr;
}

Jersey Client - 泽西岛客户-

    public static void getInvitedUser(){
    try{
        Gson gson = new Gson();
        Client client = Client.create();
        WebResource webResource = client.resource("http://localhost:8080/webApplication/rest/UserJSON2/"+preRegisterId);
        ClientResponse response = webResource.accept(MediaType.APPLICATION_JSON).get(ClientResponse.class);

        if (response.getStatus() != 200) {
           throw new Exception("Exception in getInvitedUser() : "+ response.getStatus());
        }

        String output = response.getEntity(String.class);
        System.out.println("Output from Server .... \n");
        System.out.println(output);
        preRegisterUser = gson.fromJson(output, PreRegister.class);
    }catch(Exception ex){
        ex.printStackTrace();
    }
    }

After consuming web service through Postman and ARC- 通过Postman和ARC使用网络服务后,

{
"firstName": "fname",
"middleName": null,
"lastName": "lname",
"loginId": "mith11395@gmail.com",
"roleId": 4,
"urlCode": "1c5f8f37dfb1483db1d694d82fbdcae5",
"createdBy": "50",
"invitedUnderId": 0,
"skipBiometrics": true,
"mainRecordOfficer": null,
"employeeId": "777777",
"fondId": 0
}

After consuming web service through jersey client as shown above I get HTML content instead of JSON - 如上所示,通过jersey客户端使用了Web服务后,我得到了HTML内容而不是JSON-

Output from Server .... 
<HTML content .......>

And then I get this exception - 然后我得到这个例外-

com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 1 column 1 path $ 

I know the cause for this, it can not convert JSON to object because we haven't received JSON response altogrther(That is the actual problem). 我知道原因,它不能将JSON转换为对象,因为我们还没有收到Altogrther的JSON响应(这是实际问题)。 Also I don't get anything in error logs. 另外,我在错误日志中什么也没得到。 What could be the reason, am I missing something ? 是什么原因,我错过了什么吗?

Any help would be appreciated. 任何帮助,将不胜感激。

In jersey if you want to return JSON you should declare it using @Produces(MediaType.APPLICATION_JSON) 在球衣中,如果要返回JSON,则应使用@Produces(MediaType.APPLICATION_JSON)声明。

The @Produces annotation is used to specify the MIME media types or representations a resource can produce and send back to the client. @Produces批注用于指定资源可以生成并发送回客户端的MIME媒体类型或表示形式。

In your method add line: 在您的方法中添加以下行:

@RequestMapping("/userJSON2/{preRegisterId}")
@Produces(MediaType.APPLICATION_JSON)
public PreRegisterJsonResponse userJson2(@PathVariable long preRegisterId) {

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM