“没有访问者可以设置属性私有最终java.util.Map org.json.JSONObject.map!” + SpringBoot REST API

时间:2020-06-22 09:21:56

标签: java json spring-boot microservices

我使用了SpringBoot REST API微服务。 在GET方法中,我尝试获取所有分数,但是在邮递员中使用GET时,我会在控制台中收到此错误

"No accessor to set property private final java.util.Map org.json.JSONObject.map!"

我想,这个问题是由于我在Score类中将JSONARRAY用作历史对象的一种类型,但是我不知道如何解决它。 得分类别:

@Document(collection = "score")
public class Score {
    @Id
    @NotBlank
    @JsonView(Views.class)
    private String scoreid;
    @JsonView(Views.class)
    private Long score;
    @NotBlank
    @JsonView(Views.class)
    private String player;
    @NotBlank
    @JsonView(Views.class)
    private String gamecode;
    @JsonView(Views.class)
    private Date date;
    public JSONArray history;
    
    
    
    public Score(@NotBlank String scoreid, Long score, @NotBlank String player, @NotBlank String gamecode, Date date,
            JSONArray history) {
        super();
        this.scoreid = scoreid;
        this.score = score;
        this.player = player;
        this.gamecode = gamecode;
        this.date = date;
        this.history = history;
    }
    public String getScoreid() {
        return scoreid;
    }
    public void setScoreid(String scoreid) {
        this.scoreid = scoreid;
    }
    public Long getScore() {
        return score;
    }
    public void setScore(Long score) {
        this.score = score;
    }
    public String getPlayer() {
        return player;
    }
    public void setPlayer(String player) {
        this.player = player;
    }
    public String getGamecode() {
        return gamecode;
    }
    public void setGamecode(String gamecode) {
        this.gamecode = gamecode;
    }
    public Date getDate() {
        return date;
    }
    public void setDate(Date date) {
        this.date = date;
    }
    
    public JSONArray getHistory() {
        return history;
    }
    public void setHistory(JSONArray history) {
        this.history = history;
    }
    @Override
    public String toString() {
        return "Score [scoreid=" + scoreid + ", score=" + score + ", player=" + player + ", gamecode=" + gamecode + "]";
    }
    

}

得分控制者:

 public class ScoreController {
    @Autowired
    private ScoreRepository srepo;
    @Autowired
    private PlayerRepository prepo;
    @Autowired
    private GameRepository grepo;
    
    //POST NEW SCORE
    @PostMapping("/score")
    public ResponseEntity<?> createScore(@RequestBody @JsonView(Views.class) @Valid  Score score) {
        Player p = prepo.findByNickname(score.getPlayer());
        Game g = grepo.findByCode(score.getGamecode());
        if ((srepo.findByScoreid(score.getScoreid())) == null) {
            if((p!= null) && (g!=null)) {
            
            JSONObject jo = new JSONObject();
            jo.put("score", score.getScore());
            jo.put("date", score.getDate());
            JSONArray ja = new JSONArray();
            ja.put(jo);
            score.setHistory(ja);           
            srepo.save(score);
            return ResponseEntity.status(201).body("Created!");
            }
            else return ResponseEntity.status(400).body("Bad Request!");
        }
        
        else
            return ResponseEntity.status(409).body("Conflict!");
        }
    //GET ALL SCORES
    @GetMapping("/score")
    public List<Score> getAllScores(){
        return srepo.findAll();
    }

在我使用的Application.properties中:

spring.jackson.serialization.FAIL_ON_EMPTY_BEANS=false

1 个答案:

答案 0 :(得分:0)

您不应使用JSONArray来实现列表属性。代替它使用java.util.List或数组,杰克逊会将其序列化为JsonArray不会出现问题。

可能会解决该问题。您还必须声明包含历史记录信息的对象,并在其上定义访问器(获取器和设置器)