Java HttpMessageTreadableException和不匹配的InputException
我在爪哇有一个叫“苏格兰场”的游戏。在后端用java编码,前端用angular编码。从一开始,游戏就将枚举作为传输类型,但后来我们的任务是动态传输类型。因此,我们删除了枚举,并将其替换为一个名为Ttype的类,因为我们可以添加新的传输类型作为Ttype类的实例。我们在arraylist的帮助下做到了这一点。因此,在代码(包含许多类)中,我们将代码中的所有内容从enum type替换为我的新类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.a.c.c.[dispatcherServlet][0;39m[2m][0;39m Servlet.service(),用于路径[]中的Servlet[dispatcherServlet]引发异常[请求处理失败;嵌套异常为org.springframework.web.client.RestClientException:提取类型[class se.kau.cs.sy.board.board]和内容类型[application/json]的响应时出错];嵌套异常为org.springframework.http.converter.httpMessageEndableException:JSON解析错误:无法构造se.kau.cs.sy.board.Ttype的实例(尽管至少存在一个创建者):无法从对象值反序列化(无委托或基于属性的创建者);嵌套异常为com.fasterxml.jackson.databind.exc.MismatchedInputException:无法构造se.kau.cs.sy.board.Ttype的实例(尽管至少存在一个创建者):无法从对象值反序列化(无委托或基于属性的创建者) 在[Source:(PushbackInputStream);第1行,第139列](通过引用链:se.kau.cs.sy.board.board[“节点”]->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的实例(尽管至少存在一个创建者):无法从对象值反序列化(无委托或基于属性的创建者) 在[Source:(PushbackInputStream);第1行,第139列](通过引用链:se.kau.cs.sy.board.board[“节点”]->java.util.ArrayList[0]->se.kau.cs.sy.board.Node[“链接”]->java.util.HashSet[0]->se.kau.cs.sy.board.Link[“类型”]) 我不知道该怎么办,我想错误是现在当我没有枚举时,Ttype导致了问题。这是我认为与错误有关的类。可以过来帮我一下吗。我知道这很难看,但我真的不知道该怎么办。对不起,我的英语不好。我希望你能理解 我的链接类:Java HttpMessageTreadableException和不匹配的InputException,java,angular,http,inputmismatchexception,Java,Angular,Http,Inputmismatchexception,我在爪哇有一个叫“苏格兰场”的游戏。在后端用java编码,前端用angular编码。从一开始,游戏就将枚举作为传输类型,但后来我们的任务是动态传输类型。因此,我们删除了枚举,并将其替换为一个名为Ttype的类,因为我们可以添加新的传输类型作为Ttype类的实例。我们在arraylist的帮助下做到了这一点。因此,在代码(包含许多类)中,我们将代码中的所有内容从enum type替换为我的新类Ttype。它工作得很好,但是当我尝试开始游戏时,我会出错。错误很长,并表示: 2020-10-29 15
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;
}
}
问题可能是什么?知道吗?如果这些类没有帮助,很抱歉,如果需要,我将尝试添加其他类。错误是
无法构造se.kau.cs.sy.board.Ttype的实例(尽管至少存在一个创建者):至少无法从对象值反序列化(没有委托或基于属性的创建者)
似乎您应该向Ttype类中添加一个Jackson creator,这样当从Json反序列化时,它将知道如何创建Ttype的实例
另外,看看这个线程这个jackson creator应该在我的Ttype类中?它会把我的java代码转换成Json代码吗?或者它到底做什么?如果可能的话,请用一个例子解释一下。我真的不擅长这种编程,所以如果你能简单地为我解释一下这是如何做的,以及这是什么,我将不胜感激。Thanks@Mohammed尝试将“@JsonCreator”注释添加到您的tType构造函数中。如下所示:
@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;
}