HttpMessageNotReadableException和MismatchedInputException

问题描述

我在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 [dispatcherServlet]的Servlet.service()在路径[]的上下文中引发异常[请求处理失败嵌套的异常是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;
    }

问题可能是什么?任何想法?抱歉,如果这些类没有帮助,我会在需要时尝试添加其他类。

解决方法

错误是说Cannot construct instance of se.kau.cs.sy.board.Ttype (although at least one Creator exists): cannot deserialize from Object value (no delegate- or property-based Creator) at

似乎您应该在Jackson的Ttype类中添加一个Jackson创建者,以便在从Json反序列化时,它将知道如何创建Ttype的实例。

另外,看看这个线程No Creators,like default construct,exist): cannot deserialize from Object value (no delegate- or property-based Creator