I´m trying to make a request with or without parameters, according the code below:
#RequestMapping(value = "/threshold/list", method = RequestMethod.GET)
public List<Threshold> listThreshold(#RequestParam(required = false) String categoria, #RequestParam(required = false) String kpi, #RequestParam(required = false) String data, #RequestParam(required = false) String hora) {
return thresholdQuery.listThreshold(categoria, kpi, data, hora);
}
But when I call the endpoint this way:
http://localhost:8081/threshold/list?categoria=casa
I got the error below:
Not enough variable values available to expand 'categoria=casa'
I believe it should be 'name' instead of 'value'
#RequestParam(name="categoria",required = false)
Related
I am generating the swagger docs for my REST API using SpringFox.
I have added an optional parameter to my API now:
#ApiOperation(
value = "Get all cars"
)
#GetMapping(value = "/cars", produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<ResponseDTO<CarDTO>> getCars(
#RequestParam(defaultValue = "1") Integer page,
#RequestParam(required = false) String status) {
ResponseDTO<CarDTO> response = service.getCars(page, status);
return ResponseEntity.ok(response);
}
How do I highlight in the swagger docs that one is required and the other is optional?
You have the #ApiParam annotation you can use, it has a property required which you can put to true or false depending on your needs
#ApiOperation(
value = "Get all cars"
)
#GetMapping(value = "/cars", produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<ResponseDTO<CarDTO>> getCars(
#ApiParam(required = true) #RequestParam(defaultValue = "1") Integer page,
#ApiParam(required = false) #RequestParam(required = false) String status) {
ResponseDTO<CarDTO> response = service.getCars(page, status);
return ResponseEntity.ok(response);
}
As you can see in the documentation, it has other properties like
access
allowableValues
allowMultiple
defaultValue
name
value
How do I use #RequestParam to bind one parameters of type String which is mandatory and another one which is of type Map<String, String> which is optional ?
#RequestMapping(value = "users", method = RequestMethod.GET)
public String getUsers(#RequestParam(name = "mandatory") String mandatory,
#RequestParam(required = false) Map < String, String > optional)
throws Exception {
return userService.getUsers(mandatory, optional);
}
If what you want is to simply indicate that the "mandatory" parameter is required, you must add the required = true as follows:
#RequestMapping(value = "users", method = RequestMethod.GET)
public String getUsers(#RequestParam(name = "mandatory", required = true) String mandatory,
#RequestParam(required = false) Map < String, String > optional)
throws Exception {
return userService.getUsers(mandatory, optional);
}
My method is this:
#RequestMapping(value = "/asignar", method = RequestMethod.GET, headers = "Accept=application/json")
public #ResponseBody
ResponseViewEntity<ResultadoJSON> asignar(
#RequestParam(required = true, value = "usuario") String usuario,
#RequestParam(required = true, value = "clienteId") Long clienteId,
ListaLotes lotes) {
....
}
Object ListaLotes
public class ListaLotes {
private List<LoteForm> lotes;
}
Object LoteForm
public class LoteForm {
private Long loteId;
private Long cantidad;
}
But when i realize the petition throught PostMan, the object "lotes" its always null
PETITION REST
Rest Header
Rest body
What I should do for it works ? I can't modify my Java code its part of an API. Only can modify de REST Petition
As has already been commented, if you want to transfer data to your controller, you need to use the POST method and mark the paramter as #RequestBody.
// or #PostMapping
#RequestMapping(value = "/asignar", method = RequestMethod.POST, headers = "Accept=application/json")
public #ResponseBody
ResponseViewEntity<ResultadoJSON> asignar(
#RequestParam(required = true, value = "usuario") String usuario,
#RequestParam(required = true, value = "clienteId") Long clienteId,
#RequestBody ListaLotes lotes) {
....
}
User can search something on my list
<form action="/worldoffragrance">
<input name="search"/>
<input type="submit" value="search"/>
If list its empty, I'd like to make another website, where User can put what he want , How can i resolve this problem? :)
#RequestMapping("/")
public String fragrance() { return "fragrance"; }
#RequestMapping("/worldoffragrance")
public String worldoffragrance(
#RequestParam(value = "search") String search,
#RequestParam(value = "operation", required = false, defaultValue = "search") String operation, Model model) {
List<Fragrance> matchingPerfumes = advisor.findMatchingPerfume(search);
if (matchingPerfumes.isEmpty()) {
return "redirect:/fragrancenotfound";
}
model.addAttribute("matchingPerfumes", matchingPerfumes);
return "result";
}
#RequestMapping("/fragrancenotfound")
public String fragranceNotFound(
#RequestParam(value = "name", required = true) String getName,
#RequestParam(value = "ingredients", required = true) String getIngredients,
#RequestParam(value = "operation", required = false, defaultValue = "add" ) String operation, ModelMap model)
{
model.addAttribute("getName", "getIngredients");
return "redirect: /createNewFragrance";
}
#RequestMapping("/createnewfragrance")
public String createnewfragrance() {
return "createNewFragrance";}
you are redirecting to /fragrancenotfound which has a required parameter of name (and ingredient) which you are not providing. either provide these values or make them not required
I'm new in Spring Boot and I want to have the same Request Mapping method for JSON and simple request params, for example:
#RequestMapping(value = "/start")
public String startPostProcess(#RequestParam(value = "url",
required = false,
defaultValue = "https://goo.gl") String url,
#RequestParam(value = "word",
required = false,
defaultValue = "Search") String word,
#RequestBody String hereGoesJSON) {
//Do some stuff
}
So, when request goes with params, only #RequestParam will work, in other cases we will use #RequestBody annotation.
localhost:8080/start?url=htts://google.com&word=Luck
Or may bee I'll be able to write method like this, for accepting any params:
#RequestMapping(value = "/start")
public String startPostProcess(#RequestBody String anyParam) {
//Parse this anyParam
}
?
I've not found this trick in spring documentation, so I will appreciate any links to it.
Okay, I've solved the problem :D
All that I just needed was 2 methods with the same mapping and explicitly specify RequestMethod type:
#RequestMapping(value = "/start")
public String startPostProcess(#RequestParam(value = "url",
required = false,
defaultValue = "https://goo.gl") String url,
#RequestParam(value = "word",
required = false,
defaultValue = "Search") String word) throws InterruptedException {
//Do some stuff
}
#RequestMapping(value = "/start", method = RequestMethod.POST, consumes = "application/json")
public String startJsonProcess(#RequestBody String body) {
//Do another stuff with RequestBody
}
UPD: added "consumes = "application/json". It helps dividing simple POST requests and JSON POST requests.