1

I have a spring boot app where I am planning to write an API which is a GET request but has many request parameters in it. So I have created a POJO which maps the request params to the POJO. I wanted to deserialize it before mapping the request params to POJO.

How can I achieve that?

I have created the custom deserializer and annotated the POJO with @JsonDeserialize(using = "CustomDeserializer.class"). Call is not landing to the deserializer but mapping the fields directly to the POJO.

//pojo
@Getter
@Setter
@JsonDeserialize(using = CustomDeserializer.class)
public class GetRequest implements Serializable {

    private int id;
    private String date;
    private List<Test> List;
    private int uiRank;
    private int fr;
    private String city;
    private String state;
    private String country;
}

//Deserializer

@JsonComponent
public class CustomDeserializer extends JsonDeserializer<GetRequest> {

    @Override
    public GetRequest deserialize(JsonParser p, DeserializationContext ctxt) throws IOException, JsonProcessingException {
        //logic to deserialize
    }

}

//controller
@RestController
public class TestController {

    @GetMapping("/details")
    public void getTestDetails(GetRequest getRequest) {
        //logic
    }
}

Cannot use queryparam annotation on an API in a controller as I have many query params. I need to use a POJO which is mapped with all the query params but before that deserializer needs to be executed.

Sana
  • 360
  • 3
  • 13
  • Spring automatically binds parameters to the object. You don't need to do anything for that. – M. Deinum Aug 06 '19 at 10:25
  • spring does the deserialization for you, you don't need a custom deserializer, instead, if you want custom mapping you can use mapstruct, to map your pojo to your model or to another pojo. – Mehdi Benmesssaoud Aug 06 '19 at 10:29
  • How can I achieve if the request param contains something like below: http://localhost:9080?occupancy=2-4,2&occupancy=2 – Sindhura Gudarada Aug 19 '19 at 06:08

0 Answers0