【问题标题】:How do i show my api all json response data in the thymeleaf page?如何在 thymeleaf 页面中显示我的 api 所有 json 响应数据?
【发布时间】:2021-03-19 03:51:20
【问题描述】:

第一件事:我想从 API 读取 json 数据并想在我的 thymeleaf 模板页面中显示。以下是我的代码:

我的 JSON 类:

package com.mslapiagent.entity;
import java.math.BigInteger;

import javax.persistence.Entity;

public class MSLApiAgent{
    private int id;
    private BigInteger tranId;
    private String clientTranId;
    private String msisdn;
    private String msgbody;

    public MSLApiAgent() {
    }
    public MSLApiAgent(int id, BigInteger tranId, String clientTranId, String msisdn, String msgbody) {
        this.id = id;
        this.tranId = tranId;
        this.clientTranId = clientTranId;
        this.msisdn = msisdn;
        this.msgbody = msgbody;
    }
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public BigInteger getTranId() {
        return tranId;
    }
    public void setTranId(BigInteger tranId) {
        this.tranId = tranId;
    }
    public String getClientTranId() {
        return clientTranId;
    }
    public void setClientTranId(String clientTranId) {
        this.clientTranId = clientTranId;
    }
    public String getMsisdn() {
        return msisdn;
    }
    public void setMsisdn(String msisdn) {
        this.msisdn = msisdn;
    }
    public String getMsgbody() {
        return msgbody;
    }
    public void setMsgbody(String msgbody) {
        this.msgbody = msgbody;
    }
    @Override
    public String toString() {
        return "MSLApiAgent [id=" + id + ", tranId=" + tranId + ", clientTranId=" + clientTranId + ", msisdn=" + msisdn
                + ", msgbody=" + msgbody + "]";
    }

}

我的控制器:

@Controller
public class ApiAgentController {

    @RequestMapping("/test02")
    public String attaComsianTest02(Model model, MSLApiAgent mslApiAgent) {
        // request url
        String url = "http://localhost:8080/MSLSystem_3/api/v1/messages/4";

        // create an instance of RestTemplate
        RestTemplate restTemplate2 = new RestTemplate();

        // make an HTTP GET request

        HttpHeaders headers = new HttpHeaders();

        headers.setContentType(MediaType.APPLICATION_JSON);
        headers.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON));

        HttpEntity request = new HttpEntity(headers);
        
        ResponseEntity<MSLApiAgent> exchange = restTemplate2.exchange(url, HttpMethod.GET,request,MSLApiAgent.class);
        
        MSLApiAgent body = exchange.getBody();
        int id = body.getId();
        BigInteger tranId = body.getTranId();
        String clientTranId = body.getClientTranId();
        String msisdn = body.getMsisdn();
        String msgbody = body.getMsgbody();

        model.addAttribute("exchanges", body);

        return "test02";
    }
}

最后是我的查看页面:

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.w3.org/1999/xhtml">
<head>
<meta charset="UTF-8">
<title>test01</title>
</head>
<body>
    <h1>Here is your API data</h1>
    <table>
        <thead>
            <tr>
                <td>Id</td>
                <td>Transaction Id</td>
                <td>Client Id</td>
                <td>Mobile No.</td>
                <td>Messages</td>
            </tr>
        </thead>
        <tbody>
            <tr data-th-each="exchange : ${exchanges}">
                <td data-th-text="${exchange.id}">...</td>
                <td data-th-text="${exchange.tranId}">...</td>
                <td data-th-text="${exchange.clientTranId}">...</td>
                <td data-th-text="${exchange.msisdn}">...</td>
                <td data-th-text="${exchange.msgbody}">...</td>
            </tr>
        </tbody>
    </table>


</body>
</html>

当我选择 1 条记录时

String url = "http://localhost:8080/MSLSystem_3/api/v1/messages/4";

然后它显示在我的视图中。但是当我使用

String url = "http://localhost:8080/MSLSystem_3/api/v1/messages";

然后它现在向我显示任何数据并且我收到错误:

Servlet.service() 用于路径 [] 上下文中的 servlet [dispatcherServlet] 引发异常 [请求处理失败;嵌套异常是 org.springframework.web.client.RestClientException: Error while extracting response for type [class com.mslapiagent.entity.MSLApiAgent] and content type [application/json];嵌套异常是 org.springframework.http.converter.HttpMessageNotReadableException:JSON 解析错误:无法从 START_ARRAY 令牌中反序列化 com.mslapiagent.entity.MSLApiAgent 的实例;嵌套异常是 com.fasterxml.jackson.databind.exc.MismatchedInputException: 无法从 START_ARRAY 令牌中反序列化 com.mslapiagent.entity.MSLApiAgent 的实例 在 [来源:(PushbackInputStream);行:1,列:1]] 有根本原因 com.fasterxml.jackson.databind.exc.MismatchedInputException:无法从 START_ARRAY 令牌中反序列化 com.mslapiagent.entity.MSLApiAgent 的实例 在 [来源:(PushbackInputStream);行:1,列:1]

我该怎么办?

第二件事:实际上我想随机检查这个数据,就像 1 秒间隔时间表一样,并将最后的记录存储到我的数据库中。 什么是好的方法?

我已经尝试使用 Timer 和 TimerTask 准备 API URL。但我失败了。

【问题讨论】:

  • 如果您有多个问题,请创建一个单独的问题。还包括您尝试过的内容以及失败的内容。

标签: java json api thymeleaf schedule


【解决方案1】:

当您使用http://localhost:8080/MSLSystem_3/api/v1/messages 时,它很可能会返回一个 JSON 对象数组。

这里不能序列化成MSLApiAgent类:

restTemplate2.exchange(url, HttpMethod.GET,request,MSLApiAgent.class);

您应该创建一个代表MSLApiAgent 对象集合的新类:

public class MSLApiAgentCollection {

    private List<MSLApiAgent> agents;

    // getter and setter
}

然后使用:

restTemplate2.exchange(url, HttpMethod.GET,request,MSLApiAgentCollection.class);

【讨论】:

  • 对不起@Wim Deblauwe。它不工作。同样的错误。
猜你喜欢
  • 1970-01-01
  • 2020-04-20
  • 1970-01-01
  • 2021-05-04
  • 1970-01-01
  • 1970-01-01
  • 2017-11-04
  • 2021-10-16
  • 2019-09-16
相关资源
最近更新 更多