如何在thymeleaf页面中显示我的api所有json响应数据?

k4aesqcs  于 2021-07-03  发布在  Java
关注(0)|答案(1)|浏览(528)

第一件事:我想从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>

当我选择一个像

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

然后它就出现在我的视野里。但当我使用

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

现在它向我显示任何数据,我得到一个错误:
路径为[]的上下文中servlet[dispatcherservlet]的servlet.service()引发异常[请求处理失败;嵌套的异常是org.springframework.web.client.restclientexception:提取类型[class com.mslapiagent.entity.mslapiagent]和内容类型[application/json]的响应时出错;嵌套异常为org.springframework.http.converter.httpMessageNotradableException:json分析错误:无法反序列化的示例 com.mslapiagent.entity.MSLApiAgent 启动外\u数组令牌;嵌套异常为com.fasterxml.jackson.databind.exc.missmatchdinputException:无法反序列化的示例 com.mslapiagent.entity.MSLApiAgent [source:(pushbackinputstream)处的启动\u数组令牌不足;行:1,列:1]]具有根本原因com.fasterxml.jackson.databind.exc.missmatchdinputException:无法反序列化的示例 com.mslapiagent.entity.MSLApiAgent [source:(pushbackinputstream)处的启动\u数组令牌不足;行:1,列:1]
我该怎么办?
第二件事:事实上,我想检查这个数据的随机基础,就像1秒间隔计划和存储最后的记录到我的数据库。好的方法是什么?
我已经尝试使用timer和timertask准备api url。但我失败了。

fdbelqdn

fdbelqdn1#

当你使用 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);

相关问题