无法编写JSON:503服务暂时不可用;嵌套的异常是com.fasterxml.jackson.databind.JsonMappingException

时间:2019-03-02 20:54:02

标签: java spring jackson

如果我的数据库中有2条或更少的记录,我的应用程序似乎可以正常工作,但是如果我添加了更多数据,则我在maven中收到此警告,并且响应错误:

2019-03-02 21:36:44.642  WARN 14734 --- [nio-8080-exec-1] .w.s.m.s.DefaultHandlerExceptionResolver : Resolved [org.springframework.http.converter.HttpMessageNotWritableException: Could not write JSON: 503 Service Temporarily Unavailable; nested exception is com.fasterxml.jackson.databind.JsonMappingException: 503 Service Temporarily Unavailable (through reference chain: java.util.ArrayList[2])]

这实际上很奇怪,我接收到数据并且能够将其输出为字符串,例如,但是如果我返回列表,则会在执行时崩溃。

我的代码:

package com.awesome2048.score;

import com.fasterxml.jackson.core.JsonProcessingException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
public class ScoreController {

    @Autowired
    ScoreRepository repository;

    @GetMapping("/scores")
    public List<Score> fetchScores() throws JsonProcessingException {
        List<Score> scores = (List<Score>) repository.findAll();
        return scores;
    }

}

实体得分:

package com.awesome2048.score;

import javax.persistence.*;

import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import org.springframework.web.client.RestTemplate;

@JsonSerialize(using = ScoreSerializer.class)
@Entity
@Table(name = "scores")
public class Score {

    private static final long serialVersionUID = -3009157732242241606L;

    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    private Integer id;

    @Column(name = "name", nullable = false)
    private String name;

    @Column(name = "score", nullable = false)
    private int score;

    @Column(name = "ip", nullable = false)
    private String ip;

    public Score(String name, int score) {
        this.name = name;
        this.score = score;
    }

    public Score() {}

    public String getName() {
        return this.name;
    }

    public int getScore() {
        return this.score;
    }

    public String getIp() {
        return this.ip;
    }

    public void setIp(String ip) {
        this.ip = ip;
    }

    public String getCountryCode() {
        String endpoint = "http://api.ipinfodb.com/v3/ip-country/?key=62ee2a10303261af0cf55d6eb2c807c8db5e6fa539fe5ba843c341f4062bfaea&ip= " + this.getIp();
        RestTemplate restTemplate = new RestTemplate();
        String countryCode = restTemplate.getForObject(endpoint, String.class).split(";")[3];
        return countryCode;
    }
}

我还实现了一个自定义序列化器:

package com.awesome2048.score;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.ser.std.StdSerializer;

import java.io.IOException;

public class ScoreSerializer extends StdSerializer<Score> {

    public ScoreSerializer() {
        this(null);
    }

    public ScoreSerializer(Class<Score> t) {
        super(t);
    }

    @Override
    public void serialize(Score score, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException {
        jsonGenerator.writeStartObject();
        jsonGenerator.writeStringField("name", score.getName());
        jsonGenerator.writeNumberField("score", score.getScore());
        jsonGenerator.writeStringField("countryCode", score.getCountryCode());
        jsonGenerator.writeEndObject();
    }
}

1 个答案:

答案 0 :(得分:1)

原因是您的get方法:

public String getCountryCode() {
 String endpoint = "http://api.ipinfodb.com/v3/ip-country/?key=62ee2a10303261af0cf55d6eb2c807c8db5e6fa539fe5ba843c341f4062bfaea&ip= " + this.getIp();
 RestTemplate restTemplate = new RestTemplate();
 String countryCode = restTemplate.getForObject(endpoint, String.class).split(";")[3];
 return countryCode;
}

您使用的ipinfodb API有限制。他们页面上的信息:

  

为保持免费服务的稳定性,速率限制为 2个查询   每秒应用于我们的API服务器。任何查询API的IP   快于此速度将被我们的防火墙暂时阻止。您的   如果您继续通过以下方式访问API服务器,则IP将被永久禁止   忽略此速率限制。

像这样的

getterside effect in programming的经典示例。您无法调用其他服务,而只能使用POJO变量设计的get/set方法将数据写入磁盘。相反,尝试创建将扫描scores表并更新所需信息的批处理作业。此实现应注意服务器限制2 requests per second