【发布时间】:2021-02-12 01:58:18
【问题描述】:
我有一个名为“Scotland Yard”的 Java 游戏。在后端,它是用 java 编码的,前端是用 angular 编码的。从一开始,游戏就包含枚举作为运输类型,但后来我们的任务是动态运输类型。所以我们删除了枚举并用一个名为 Ttype 的类替换它,我们可以添加新的传输类型作为类 Ttype 的实例。我们在 arraylist 的帮助下做到了这一点。因此,在代码(包含许多类)中,我们将代码中的所有内容从枚举类型替换为我的新类 Ttype。它工作得很好,但是当我尝试启动游戏时出现错误。错误很长,说:
2020-10-29 15:25:41.901[0;39m [31mERROR[0;39m [35m2596[0;39m [2m---[0;39m [2m[nio-7001-exec-2][ 0;39m [36mo.accC[.[.[/].[dispatcherServlet] [0;39m [2m:[0;39m Servlet.service() for servlet [dispatcherServlet] 在路径 [] 的上下文中抛出异常 [请求处理失败的;嵌套异常是 org.springframework.web.client.RestClientException:提取类型 [class se.kau.cs.sy.board.Board] 和内容类型 [application/json] 的响应时出错;嵌套异常是 org.springframework.http.converter.HttpMessageNotReadableException:JSON 解析错误:无法构造 se.kau.cs.sy.board.Ttype 的实例(尽管至少存在一个 Creator):无法从 Object 值反序列化(没有委托-或基于财产的创作者);嵌套异常是 com.fasterxml.jackson.databind.exc.MismatchedInputException:无法构造 se.kau.cs.sy.board.Ttype 的实例(尽管至少存在一个 Creator):无法从 Object 值反序列化(没有委托或属性- 基于创作者) 在 [来源:(PushbackInputStream);行:1,列:139](通过引用链:se.kau.cs.sy.board.Board["nodes"]->java.util.ArrayList[0]->se.kau.cs.sy.board .Node["links"]->java.util.HashSet[0]->se.kau.cs.sy.board.Link["type"])] 有根本原因
com.fasterxml.jackson.databind.exc.MismatchedInputException:无法构造 se.kau.cs.sy.board.Ttype 的实例(尽管至少存在一个 Creator):无法从 Object 值反序列化(没有委托或属性- 基于创作者) 在 [来源:(PushbackInputStream);行:1,列:139](通过引用链:se.kau.cs.sy.board.Board["nodes"]->java.util.ArrayList[0]->se.kau.cs.sy.board .Node["links"]->java.util.HashSet[0]->se.kau.cs.sy.board.Link["type"])
我不明白该怎么做,我认为错误是现在当我不再有枚举时,Ttype 会导致问题。这是我认为与错误有关的类。可以来请帮助我。我知道有很多值得一看的地方,但我真的不知道该怎么做。对不起,我的英语不好。希望大家理解
我的链接类:
package se.kau.cs.sy.board;
import java.io.Serializable;
public class Link implements Serializable {
private static final long serialVersionUID = 1L;
private int[] nodes = new int[2];
private Ttype type;
//Only for json deserialization
public Link() {
}
public Link(int nodea, int nodeb, Ttype t) {
nodes[0] = nodea;
nodes[1] = nodeb;
type = t;
}
public int[] getNodes() {
return nodes;
}
public Ttype getType() {
return type;
}
//Only for json deserialization
public void setNodes(int[] nodes) {
this.nodes = nodes;
}
//Only for json deserialization
public void setType(Ttype type) {
this.type = type;
}
}
我的 Ttype 类:
package se.kau.cs.sy.board;
import java.util.ArrayList;
import java.util.Objects;
public class Ttype{
public String Transport = "";
public Ttype(String type) {
this.Transport = type;
}
public String getTransport() {
return Transport;
}
public void setTransport(String type) {
this.Transport = type;
}
}
我的董事会课程:
package se.kau.cs.sy.board;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.StringTokenizer;
import java.util.UUID;
import java.util.Random;
import java.util.Scanner;
import java.io.IOException;
import se.kau.cs.sy.match.Readconfig;
import se.kau.cs.sy.util.FileHandler;
public class Board implements Serializable {
private static final long serialVersionUID = 1L;
private static Board londonBoard;
private final UUID id;
private String name = "";
private List<Node> nodes = new ArrayList<>();
static Readconfig read = new Readconfig();
static {
londonBoard = loadMap();
londonBoard.setName(read.array1[0]);
}
private Board(String name) {
id = UUID.randomUUID();
this.name = name;
}
//Only for json deserialization
public Board() {
this("");
}
public static Board create() {
return londonBoard;
}
public UUID getId() {
return id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public List<Node> getNodes() {
return new ArrayList<>(nodes);
}
public Set<Link> getLinks(int node) {
return getLinks(node, null);
}
public Set<Link> getLinks(int node, Ttype type) {
Set<Link> links = nodes.get(node).getLinks();
if (type != null) {
links.removeIf(l -> l.getType() != type);
}
return links;
}
public Location getLocation(int node) {
return nodes.get(node).getLocation();
}
public Set<Integer> getNeighbourNodes(int node) {
return getNeighbourNodes(node, null); //unknown
}
public Set<Integer> getNeighbourNodes(int node, Ttype type) {
Set<Integer> result = new HashSet<>();
Set<Link> links = this.getLinks(node, type);
for (Link l : links) {
result.add(l.getNodes()[0]);
result.add(l.getNodes()[1]);
}
result.remove(node);
return result;
}
public boolean connected(int nodeA, int nodeB, Ttype type) {
return getNeighbourNodes(nodeA, type).contains(nodeB);
}
public boolean connected(int nodeA, int nodeB) {
return getNeighbourNodes(nodeA, null ).contains(nodeB);
}
public int lastNodeIndex() {
return nodes.get(nodes.size() - 1).getId();
}
public boolean nodeExists(int number) {
return number > 0 && number <= lastNodeIndex();
}
private static Board loadMap() {
Board map = new Board();
try {
FileHandler mapHandler = new FileHandler("se/kau/cs/sy/" +read.array1[1]+".txt");
// RandomAccessFile map=new RandomAccessFile(f,"r"); //kanske bra och använda
String buffer=mapHandler.readLine();
StringTokenizer token;
token=new StringTokenizer(buffer);
int nrNodes=Integer.parseInt(token.nextToken());
Location locs[] = readMapPositions();
for (int i = 0; i < nrNodes; i++ ) {
Node newNode = new Node(i);
newNode.setLocation(locs[i]);
map.nodes.add(newNode);
}
buffer=mapHandler.readLine();
while(buffer!=null && buffer.trim().length()>0) {
token=new StringTokenizer(buffer);
int node1=Integer.parseInt(token.nextToken());
int node2=Integer.parseInt(token.nextToken());
String strType=token.nextToken();
Ttype type =null; //unknown
for(int i = 0; i < read.typearray.size(); i++) {
if(read.typearray.get(i).Transport.equals(strType)) {
type=read.typearray.get(i); break;//ny
}
}
Link newLink = new Link(node1, node2, type);
map.nodes.get(node1).addLink(newLink);
map.nodes.get(node2).addLink(newLink);
buffer=mapHandler.readLine();
}
mapHandler.close();
}
catch(Exception e) {
e.printStackTrace();
}
return map;
}
private static Location[] readMapPositions() {
Location result[] = null;
try {
FileHandler map = new FileHandler("se/kau/cs/sy/" +read.array1[2]+ ".txt");
String buffer = map.readLine();
StringTokenizer token;
token = new StringTokenizer(buffer);
int numPos = Integer.parseInt(token.nextToken());
result = new Location[numPos];
for(int i = 0; i < numPos; i++)
{
buffer = map.readLine();
token = new StringTokenizer(buffer);
int pos = Integer.parseInt(token.nextToken());
int posX = Integer.parseInt(token.nextToken());
int posY = Integer.parseInt(token.nextToken());
result[pos] = new Location(posX, posY);
}
}
catch(Exception e) {
System.exit(1);
}
return result;
}
//Only for json deserialization
public void setNodes(List<Node> nodes) {
this.nodes = nodes;
}
}
这是一个错误。我的代码在“ MatchConfigurationImpl.Builder builder = new MatchConfigurationImpl.Builder();”之后崩溃
@PostMapping("/matches")
public UUID createMatch(@RequestBody MatchConfigurationDTO conf) {
MatchConfigurationImpl.Builder builder = new MatchConfigurationImpl.Builder();
Board board = restTemplate.getForObject(integration.getBoardDetailsServiceUrl(), Board.class); //my code crashes here
if (board != null) {
builder.board(board)
.detectives(conf.getNrOfDetectives())
.startPositions(conf.getStartingPositions())
.surfacingTurns(conf.getSurfacingTurns())
.turns(conf.getNrOfTurns())
.ticketsForDetectives(conf.getTicketsdetectives()) //ny
.ticketsForMrX(conf.getTicketsMrx())
.ticketNames(conf.getTicketNames()); //ny
Match newMatch = new Match(builder.build());
//TODO: add Mr. X properly
newMatch.registerMrX(new RandomPlayer(PlayerRole.MR_X));
storage.add(newMatch, "New match");
return newMatch.getId();
}
else
return null;
}
问题可能是什么?任何想法?抱歉,如果这些课程没有帮助,我会在需要时尝试添加其他课程。
【问题讨论】:
标签: java angular http inputmismatchexception