我在ServiceRESTClient中反序列化Score的实例时遇到问题。
这是我的JPA实体:
@Entity
@NamedQuery( name = "Score.getBestScores",
query = "SELECT s.game, s.player, s.points, s.playedon, s.id FROM Score s WHERE s.game = :game ORDER BY points DESC")
public class Score implements Comparable<Score>, Serializable {
private String game;
private String player;
private int points;
@Temporal(TemporalType.DATE)
private Date playedon;
@Id
@GeneratedValue
private int id;
public Score(String game, String player, int points, Date playedon) {
this.game = game;
this.player = player;
this.points = points;
this.playedon = playedon;
}
public Score(){}
...
在服务器上我使用REST API从这个实体获取JSON输出(使用JPA entityManager):
// http://localhost:8080/rest/score/mines
@GET
@Path("/{game}")
@Produces(MediaType.APPLICATION_JSON)
public List<Score> getBestScores(@PathParam("game") String game) throws ScoreException {
return scoreService.getBestScores(game);
}
如果我使用邮递员获得分数(http://localhost:8080/rest/score/mines),我会得到输出:
[
[
"mines",
"Ondrej",
96,
"2017-04-24",
38
],
[
"mines",
"Ondrej",
94,
"2017-04-24",
13
]
]
但如果我尝试从客户端获取输出:
@Override
public List<Score> getBestScores(String game) throws ScoreException {
try {
Client client = ClientBuilder.newClient();
return client.target(URL)
.path("/" + game)
.request(MediaType.APPLICATION_JSON)
.get(new GenericType<List<Score>>() {
});
} catch (Exception e) {
throw new ScoreException("Error loading score", e);
}
}
我总是得到同样的错误:
com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of sk.tuke.gamestudio.server.entity.Score out of START_ARRAY token
at [Source: org.glassfish.jersey.message.internal.ReaderInterceptorExecutor$UnCloseableInputStream@76165093; line: 1, column: 2] (through reference chain: java.util.ArrayList[0])
at org.glassfish.jersey.client.JerseyInvocation.translate(JerseyInvocation.java:875) ~[jersey-client-2.23.2.jar:na]
at org.glassfish.jersey.client.JerseyInvocation.access$800(JerseyInvocation.java:92) ~[jersey-client-2.23.2.jar:na]
at org.glassfish.jersey.client.JerseyInvocation$3.call(JerseyInvocation.java:724) ~[jersey-client-2.23.2.jar:na]
at org.glassfish.jersey.internal.Errors.process(Errors.java:315) ~[jersey-common-2.23.2.jar:na]
at org.glassfish.jersey.internal.Errors.process(Errors.java:297) ~[jersey-common-2.23.2.jar:na]
at org.glassfish.jersey.internal.Errors.process(Errors.java:228) ~[jersey-common-2.23.2.jar:na]
at org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:444) ~[jersey-common-2.23.2.jar:na]
at org.glassfish.jersey.client.JerseyInvocation.invoke(JerseyInvocation.java:720) ~[jersey-client-2.23.2.jar:na]
at org.glassfish.jersey.client.JerseyInvocation$Builder.method(JerseyInvocation.java:430) ~[jersey-client-2.23.2.jar:na]
at org.glassfish.jersey.client.JerseyInvocation$Builder.get(JerseyInvocation.java:321) ~[jersey-client-2.23.2.jar:na]
at sk.tuke.gamestudio.server.service.ScoreServiceRestClient.getBestScores(ScoreServiceRestClient.java:38) ~[classes/:na]
感谢您的帮助。