简体   繁体   中英

Fasterxml Jackson databind MismatchedInputException

I am using com.fasterxml.jackson.databind in a spring boot application. When I send a request to my endpoint I receive the following exception:

com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot construct instance of 
org.pacakge.domain.controller.Mycontroller (although at least one Creator exists): cannot
deserialize from Object value (no delegate- or property-based Creator)\n at 
[Source: (PushbackInputStream); line: 2, column: 3]  Is the body of the request formatted correctly?

My controller processes a request body that has the following structure:

{
  "portalId": 123,
  "objectType": "TYPE",
  "objectTypeId": "0-3",
  "objectId": 123,
  "properties": { ... }
}

The only property that I need is objectId . I've constructed a class to process this object like so:

@lombok.Value
private static class MyObject {
  @JsonAlias("objectId")
  private final String dealId;
} 

I've designed a controller that looks like this

@Slf4j
@RestController
@RequestMapping(path = "/entrypoint")
public class MyController {
   @Autowired
   public MyController(){}
   /**
   * REST endpoint handles MyObject
   */
  @PostMapping(value = "/endpoint")
  public void handleRequest(
      @Valid @RequestBody MyObject command
      ) {
          log.debug(command.getDealId());
   }
   @lombok.Value
   private static class MyObject {
     @JsonAlias("objectId")
     private final String dealId;
   } 
}

What is interesting about this problem is that my request is processed just fine if I change MyObject to the following structure:

@lombok.Value
  private static class MyObject {
    @JsonAlias("objectId")
    private final String dealId;
    private final JSONObject properties;  // TODO we shouldn't need this.  Fix.
  }

I cannot seem to figure out what the issue is. I would love some help on this problem. Maybe there is annotation that I am missing? I am hoping someone else has experienced this issue. I haven't found any information on it by just searching the web.

I added the following line to lombok.config in the root directory of the project:

lombok.anyConstructor.addConstructorProperties=true

And after that managed to deserialize your JSON using this DTO using @JsonIgnoreProperties annotation:

@Value
@JsonIgnoreProperties(ignoreUnknown = true)
public class MyObject {
    @JsonProperty("objectId")
    String dealId;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM