pustypawel
pustypawel

Reputation: 331

@RequestParam and @PathVariable encapsulation

Is there any way to encapsulate this two values into one object?

public ResponseEntity<TestResponse> test(@PathVariable("customerId") String customerId,
        @RequestParam(name = "reason", required = true) String reason,
        @RequestParam(name = "attribute", required = true) List<String> attributes) {

I think that I should be able to do it with this way:

public ResponseEntity<TestResponse> test(@MaybeSomeMagicAnnotation? Request request) {

where Request class has those three properties (customerId, reason, attributes).

I'm using spring boot 1.5.9

Upvotes: 1

Views: 441

Answers (1)

Todd
Todd

Reputation: 31710

You should be able to do this by defining an object that matches the request parameters, etc.

Example (untested):

public class MyRequest {
   @NotNull
   private String customerId;

   @NotNull
   private String reason;

   @NotNull
   @NotEmpty
   private List<String> attributes;

   // getters and setters left out for brevity.
}

And then in your controller:

public ResponseEntity<TestResponse> test(@Valid MyRequest request) {
    ...
}

Upvotes: 4

Related Questions