javaspring-bootspring-mvchttp-request-parameters

I have a Java Spring Boot error @RequestParam("")


I am new to Java and Spring Boot and I have an error when trying to send a JSON by GET.

For the tests I use Postman and I get this error:

{
"status": "INTERNAL_SERVER_ERROR",
"message": "Required request parameter 'tags' for method parameter type List is not present"
}

JSON to prove:

"tags": [
  "potable",
  "contaminada",
  "tratada"
]

The layers are quite simple:

Controller:

@GetMapping("/search")
public ResponseEntity<List<PropuestaEntity>> SearchTags(@RequestParam("tags") List<String> tag) {
    List<PropuestaEntity> propuesta = propuestaServices.SearchTags(tag);
    return ResponseEntity.ok(propuesta);
}

Services:

public List<PropuestaEntity> SearchTags(List<String> tag) {
    return propuestaRepository.findByTags(tag);
}

Repository:

public interface PropuestaRepository extends MongoRepository<PropuestaEntity, Integer> {
    List<PropuestaEntity> findByTags(List<String> tag);
}

Data in mongobd:

{
  "titulo": "Análisis de la calidad del agua en el río X sdauiyuadiossd",
  "tags": ["agua", "calidad", "contaminación", "metales pesados", "nutrientes"],
  "estadoPropuesta":1
}

The main objective is to search for Proposals (English) by the tags that were previously assigned to them and list all the proposals containing those tags.


Solution

  • I don't really understand what you mean by "JSON to prove", but from the formatting it seems that you are presenting a request payload. In order to prove anything, you would need to show the whole request (including full request URL and payload).

    If you send the tags via request body, you need to change the annotation in your controller to look for the tags in the request payload:

    @GetMapping("/search")
    public ResponseEntity<List<PropuestaEntity>> SearchTags(@RequestBody List<String> tags) {
        List<PropuestaEntity> propuesta = propuestaServices.searchTags(tags);
        return ResponseEntity.ok(propuesta);
    }
    

    In this case, your payload should look like this so it can be matched to the List type in the controller method:

    [
      "potable",
      "contaminada",
      "tratada"
    ]
    

    If you really wanted to use the @RequestParam annotation, you would have to include the tags as query params in the URL like this:

    https://your-service.com/search?tags=potable&tags=contaminada&tags=tratada