Can not deserialize instance of java.lang.String out of START_OBJECT token

前端 未结 5 721
野的像风
野的像风 2020-12-08 12:42

I\'m running into an issue where my deployable jar hits an exception that doesn\'t happen when I run this locally in IntelliJ.

Exception:

5条回答
  •  天命终不由人
    2020-12-08 13:17

    Resolved the problem using Jackson library. Prints are called out of Main class and all POJO classes are created. Here is the code snippets.

    MainClass.java

    public class MainClass {
      public static void main(String[] args) throws JsonParseException, 
           JsonMappingException, IOException {
    
    String jsonStr = "{\r\n" + "    \"id\": 2,\r\n" + " \"socket\": \"0c317829-69bf- 
                 43d6-b598-7c0c550635bb\",\r\n"
                + " \"type\": \"getDashboard\",\r\n" + "    \"data\": {\r\n"
                + "     \"workstationUuid\": \"ddec1caa-a97f-4922-833f- 
                632da07ffc11\"\r\n" + " },\r\n"
                + " \"reply\": true\r\n" + "}";
    
        ObjectMapper mapper = new ObjectMapper();
    
        MyPojo details = mapper.readValue(jsonStr, MyPojo.class);
    
        System.out.println("Value for getFirstName is: " + details.getId());
        System.out.println("Value for getLastName  is: " + details.getSocket());
        System.out.println("Value for getChildren is: " + 
          details.getData().getWorkstationUuid());
        System.out.println("Value for getChildren is: " + details.getReply());
    
    }
    

    MyPojo.java

    public class MyPojo {
        private String id;
    
        private Data data;
    
        private String reply;
    
        private String socket;
    
        private String type;
    
        public String getId() {
            return id;
        }
    
        public void setId(String id) {
            this.id = id;
        }
    
        public Data getData() {
            return data;
        }
    
        public void setData(Data data) {
            this.data = data;
        }
    
        public String getReply() {
            return reply;
        }
    
        public void setReply(String reply) {
            this.reply = reply;
        }
    
        public String getSocket() {
            return socket;
        }
    
        public void setSocket(String socket) {
            this.socket = socket;
        }
    
        public String getType() {
            return type;
        }
    
        public void setType(String type) {
            this.type = type;
        } 
    }
    

    Data.java

    public class Data {
        private String workstationUuid;
    
        public String getWorkstationUuid() {
            return workstationUuid;
        }
    
        public void setWorkstationUuid(String workstationUuid) {
            this.workstationUuid = workstationUuid;
        }   
    }
    

    RESULTS:

    Value for getFirstName is: 2
    Value for getLastName  is: 0c317829-69bf-43d6-b598-7c0c550635bb
    Value for getChildren is: ddec1caa-a97f-4922-833f-632da07ffc11
    Value for getChildren is: true
    

提交回复
热议问题