Stefan Falk
Stefan Falk

Reputation: 25387

How can I make a request parameter required per default without using @RequestParam

If I formalize my method like this:

@GetMapping("/dl4j/getWordVector")
public ResponseEntity<List<Double[]>> getWordVector(String modelName, String word) {
    // ...
}

and leave out a paramter, I will not see a MissingServletRequestParameterException. This works only if I use @RequestParam:

@GetMapping("/dl4j/getWordVector")
public ResponseEntity<List<Double[]>> getWordVector(@RequestParam(value="modelName", required = true) String modelName, String word) {      
    // ...
}

Why is this the case? Imho it should be a opt-out since I guess most of the parameters in a REST Api are required, are they not?

Upvotes: 0

Views: 46

Answers (1)

Klaus Groenbaek
Klaus Groenbaek

Reputation: 5035

@RequestParam was not invented for REST it was invented for form-submission and URL requests, and in those cases parameters are rarely optional.

Some people would argue that you should us @PathParam for accessing RESTfull resources, but @PathParam works even worse if parameters are optional, because ordering matters.

Another thing you need to remember is that the names of parameters are not automatically part of the bytecode, parameter names are only available if you compile with debug information, this is why you need an annotation. check this link for more info

Upvotes: 1

Related Questions