无法读取正确的JSON对象

时间:2019-10-30 19:22:52

标签: java maven gson

我有一个要解析为JSON字符串的对象。当我想读出它时,它说它无法将其解析回一个对象(我认为)。这是错误:enter image description here

正如您在控制台顶部看到的那样,正确设置了JSON对象,并在右括号中。我从JSONSyntaxException了解到的是,它无法将其识别为JSON对象。我已经从学校复制了此代码,并且在另一个项目中它确实起作用。我不知道为什么它在我的Maven项目中不起作用。

编辑 这是RESTcontroller.java的代码。

public PlayerDTO addPlayer(String name, String password) {
    PlayerDTO playerRequest = new PlayerDTO(NOTDEFINED, name, password);
    String queryPost = "/player";
    PlayerResponse response = executeQueryPost(playerRequest, queryPost);
    return response.getPlayers().get(0);
}

private PlayerResponse executeQueryPost(PlayerDTO playerRequest, String queryPost) {
    final String query = url + queryPost;
    log.info("[Query POST] : " + query);

    HttpPost httpPost = new HttpPost(query);
    httpPost.addHeader("content-type", "application/json");
    StringEntity params;
    try{
        params = new StringEntity(gson.toJson(playerRequest));
        System.out.println(gson.toJson(playerRequest));
        httpPost.setEntity(params);
    } catch (UnsupportedEncodingException e) {
        Logger.getLogger(SeaBattleGame.class.getName()).log(Level.SEVERE, null, e);
    }
    return executeHttpUriRequest(httpPost);
}

private PlayerResponse executeHttpUriRequest(HttpUriRequest httpUriRequest) {

    // Execute the HttpUriRequest
    try (CloseableHttpClient httpClient = HttpClients.createDefault();
         CloseableHttpResponse response = httpClient.execute(httpUriRequest)) {
        log.info("[Status Line] : " + response.getStatusLine());
        HttpEntity entity = response.getEntity();
        final String entityString = EntityUtils.toString(entity);
        log.info("[Entity] : " + entityString);
        return gson.fromJson(entityString, PlayerResponse.class);
    } catch (IOException e) {
        log.info("IOException : " + e.toString());
        PlayerResponse playerResponse = new PlayerResponse();
        playerResponse.setSuccess(false);
        return playerResponse;
    } catch (JsonSyntaxException e) {
        log.info("JsonSyntaxException : " + e.toString());
        PlayerResponse playerResponse = new PlayerResponse();
        playerResponse.setSuccess(false);
        return playerResponse;
    }
}

请记住,代码为我提供了此代码,并且确实可以在他们的项目中使用!

2 个答案:

答案 0 :(得分:0)

在我看来,您的请求失败,并出现以下错误:Problem accessing /seabattle/player Reason: request failed。它不是很具说明性,但是有些东西。

您的代码然后失败,并显示NullPointerException,我猜测response.getPlayers()返回了null

您应该真正尝试从curl命令或通过Postman应用程序向该端点发送类似的JSON,并查看其是否有效。或者,如果可以的话,请查看网络服务器日志以找出实际问题所在。

编辑:您实际上还应该在尝试解析响应状态之前检查其响应状态。这将摆脱NullPointerException,并使您的代码总体上更好。

答案 1 :(得分:0)

一些问题可以解决:

  1. 在方法executeQueryPost中,您必须为请求添加以下标头:
httpPost.setHeader("Accept", "application/json");
httpPost.setHeader("Content-type", "application/json");

代码应如下所示:

private PlayerResponse executeQueryPost(PlayerDTO playerRequest, String queryPost) {
    final String query = url + queryPost;
    log.info("[Query POST] : " + query);

    HttpPost httpPost = new HttpPost(query);
    httpPost.addHeader("content-type", "application/json");
    StringEntity params;
    try{
        params = new StringEntity(gson.toJson(playerRequest));
        System.out.println(gson.toJson(playerRequest));
        httpPost.setEntity(params);

        // Need to add the following headers
        httpPost.setHeader("Accept", "application/json");
        httpPost.setHeader("Content-type", "application/json");

    } catch (UnsupportedEncodingException e) {
        Logger.getLogger(SeaBattleGame.class.getName()).log(Level.SEVERE, null, e);
    }
    return executeHttpUriRequest(httpPost);
}

如果代码继续失败是因为它无法创建新播放器。您必须使用addPlayer()方法处理异常。在返回之前,请检查executeQueryPost返回的对象是否不为空。

更新

代码可能仅由于http://localhost:8090中的服务未运行而失败,这就是为什么它获得500作为响应的原因。