java - “没有访问器来设置属性 private final java.util.Map org.json.JSONObject.map!”+SpringBoot REST API
问题描述
我使用了 SpringBoot REST API 微服务。在 GET 方法中,我尝试获取所有分数,但在邮递员中,当我使用 GET 时,我会在控制台中收到此错误
"No accessor to set property private final java.util.Map org.json.JSONObject.map!"
我想,这个问题是因为我在我的分数类中使用 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
解决方案
您不应该使用 JSONArray 来实现列表属性。取而代之的是使用 java.util.List 或数组,杰克逊会将其序列化为 JsonArray 而不会出现问题。
可能它会解决问题。您还必须声明包含历史信息的对象并在其上定义访问器(getter 和 setter)
推荐阅读
- sql - 使用 BigQuery SQL 的平均值和最新值
- amazon-web-services - 什么是阈值:AWS CloudWatch 警报中的最大 EnvironmentHeath 值
- javascript - 如何根据特定特征将数据保存在不同的数组中?
- android-inapp-purchase - 其他地区的应用内购买测试 (Google)
- ios - TextField 在 Voice Over 上宣布 Label 和 AccessibilityLabel
- javascript - 是否可以在 java spring boot 中使用 json 类型?
- google-earth-engine - 从 ImageCollection 中导出图像,并将名称作为元数据的属性
- button - Marquee 在按钮单击时开始
- r - 条形图在 y 轴上没有刻度
- python - 如何从此网页中抓取一个数字(在 python 中)