Jersey: No suitable constructor found for type [simple type, class Thing]: can not instantiate from JSON object

前端 未结 2 1702
孤城傲影
孤城傲影 2020-12-29 21:04

I have a resource with a method like:

@PUT
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
@Path(\"/add\")
public Response putThi         


        
2条回答
  •  一个人的身影
    2020-12-29 21:51

    You need a no-arg constructor and setters, or use @JsonCreator. Easiest thing to do would be just to add the no-arg with setters. Jackson needs the setters when deserializing. For serialization, all that's needed are getters.

    EDIT

    To keep it immutable, you can use @JsonCreator on the constructor. For example

    @JsonCreator
    public Thing(@JsonProperty("symbol") String symbol, 
                 @JsonProperty("name") String name) {
    
        this.symbol = symbol;
        this.name = name;
    }
    

    See more Jackson Annotations: @JsonCreator demystified

提交回复
热议问题