0

We have this request body which is supposed to map query params when an endpoint is accessed

// Uses Lombok @Data
public class RetrieveTxnRequest {

  private Short pageStart = 0;

  private Short pageSize = Short.MAX_VALUE;
}

But when we call the endpoint like this:

serverUrl/ourEndpoint?pageSize=

pageSize is set to null. If we supply a value it works. I noticed that if instead of using a class we use @RequestParams, the problem does not occur:

@GetMapping("/ourEndpoint")
public OurResponse getTransactions(@RequestParam(required = false, defaultValue = "0") Short pageStart,
                                   @RequestParam(required = false, defaultValue = "50") Short pageSize) 

because default values are set.

I tried the ff:

// Uses Lombok @Data
public class RetrieveTxnRequest {

  private Short pageStart = 0;

  @Value("50")
  private Short pageSize = Short.MAX_VALUE;
}

But it doesn't seem to work, pageSize is still null if we use the endpoint mentioned above

tldr: Is there a way to set default values in Spring @RequestBody classes?

Justin
  • 407
  • 6
  • 16
  • 1
    It looks like you are reinventing `Pageable` btw. – skubski Jul 14 '22 at 07:32
  • 1
    Lombok `Data` will create a AllArgsConstructor for you so the values you set on your fields are ignored because they will be overriden anyway when spring calls this constructor. – Michał Krzywański Jul 14 '22 at 10:30
  • @MichałKrzywański that seems to make sense. Is the best answer to use `@RequestParam`? What if there are many fields? – Justin Jul 15 '22 at 04:18

3 Answers3

0

use nulls=Nulls.SKIP

@JsonSetter(nulls = Nulls.SKIP)
private Short pageSize = 50;
Tintin
  • 2,853
  • 6
  • 42
  • 74
0

You can set the value in the class and it will be treated as a default value.

public class RetrieveTxnRequest {

  private Short pageStart = 0;

  private Short pageSize = "default_value";
}

Also refer to the link for this response

Puneet Kundliya
  • 264
  • 1
  • 4
0

I just tried recreating your problem I think in this way you try putting your default values in request body classes.

I created a RequestBody class like this

public class RetrieveTxnRequest {

  private Short pageStart = 0;
  
  private Short pageSize = 1;

  public Short getPageStart() {
    return pageStart;
}

public void setPageStart(Short pageStart) {
    this.pageStart = pageStart;
}

public Short getPageSize() {
    return pageSize;
}

public void setPageSize(Short pageSize) {
    this.pageSize = pageSize;
}

}

And created a request handler method like this

@GetMapping("/ourEndpoint")
public RetrieveTxnRequest getTransactions(@RequestBody RetrieveTxnRequest rTxnRequest) {
    return rTxnRequest;
}

And I tried sending data through postman body every time if the data is there it is ignored otherwise default data is coming. postman

Ravi Kiran
  • 44
  • 4
  • I must add that adding a body to a get request that alters the response does not follow the the HTTP/1.1 spec, section 4.3 recommendation as explained [here](https://stackoverflow.com/questions/978061/http-get-with-request-body#:~:text=Yes.,semantic%20meaning%20to%20the%20request.). So, your answer is advocating bad practices. Please change your answer accordingly. – skubski Jul 14 '22 at 11:33