0

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?

Stefan Falk
  • 23,898
  • 50
  • 191
  • 378
  • I don't readily see another way, but you can do this as functionally equivalent but a little less verbose: `@RequestParam("modelName") String modelName` – riddle_me_this Jan 26 '17 at 18:17

1 Answers1

1

@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

Community
  • 1
  • 1
Klaus Groenbaek
  • 4,820
  • 2
  • 15
  • 30