0

I'm using a very convenient set of lombok annotations for my DTOs:

    @Value
    @Builder
    @Jacksonized
    public class Stuff {
        String a;
        String b;
        ...
    }

Jackson is able to deserialize my objects from request body:

    @PostMapping("/stuff")
    public void postStuff(
        @RequestBody Stuff stuff        
    ) {
        ...
    }

However if I use such object in a GET request (GET /stuff?a=xxx&b=yyy) spring is unable to assemble it from request parameters and I get the "No primary or default constructor found for class" Exception.

    @Value
    @Builder
    @Jacksonized
    public class GetStuffParameters {
        String a;
        String b;
        ...
    }

    @GetMapping("/stuff")
    public List<Stuff> getStuff(
        GetStuffParameters parameters
    ) {
        ...
    }

How do I force Spring Boot to use lombok builders while assembling DTO from request parameters?

igor
  • 33
  • 3
  • Jackson needs a default constructor without parameters or annotations on the constructor paramterts to find out which field in the JSON object should be mapped to the parameter. Maybe this helps: https://stackoverflow.com/questions/57204802/jackson-invaliddefinitionexception-cannot-construct-instance-because-no-default – blafoo Feb 10 '22 at 11:58
  • Does this answer your question? [Using Jackson to deserialize with Lombok builder](https://stackoverflow.com/questions/66399749/using-jackson-to-deserialize-with-lombok-builder) – Turo Feb 10 '22 at 12:00
  • @Turo It doesn't - Jacksonized works for me usually. It's not really a jackson question, more like a spring boot question - i want to find a way for it to initiate my dto from request parameters by using lombok builder – igor Feb 10 '22 at 13:20
  • I've reproduce the code and it worked fine for me. Please provide the full `GetStuffParameters` class or link to repository – Nick Feb 10 '22 at 14:48
  • 1
    Are you trying to put the request parameters in a 'Jackson' object? If this is the case, you are confusing how web apps work. Jackson is used to deserialise the **body** of PUT/POST/etc requests, not the request parameters. Spring has a feature to map request parameters to the controller method as answered [here](https://stackoverflow.com/questions/13213061/springmvc-requestmapping-for-get-parameters) – Augusto Feb 10 '22 at 15:04

0 Answers0