-2

I am new to JAVA Springboot and I have an error when trying to send a json by GET.

For the tests I use POSTMAN and this error sends me:

{
"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.

Ram
  • 313
  • 1
  • 2
  • 17
  • The error "Required request parameter 'tags' for method parameter type List is not present" is very self-explaining: You did not provide it in your GET call. – Seelenvirtuose Apr 26 '23 at 05:34
  • I understand that it looks simple but it is not that simple for me. I have searched and I have not been able to send that json to get the results I need, I get that error constantly. – marcelo sazo Apr 26 '23 at 05:34
  • If you want to have your request parameter `tags` as optional, then you need `@RequestParam(name="tags", required = false)` – Seelenvirtuose Apr 26 '23 at 05:36
  • I already did it, it does not generate an error but it does not find the data, which in itself is quite populated. – marcelo sazo Apr 26 '23 at 05:37
  • That is a completely pther question than the error you posted. And it makes me assume, that you do _not_ want to have that request parameter optional. – Seelenvirtuose Apr 26 '23 at 05:40
  • data is: [ { "id": 1, "titulo": "Análisis de la calidad del agua en el río X sdauiyuadiossd", "autor": "Juan", "tags": [ "agua", "calidad", "contaminación", "metales pesados", "nutrientes" ], "recursosNecesarios": "Reactivo de nitrato, reactivo de fosfato, reactivo de metales pesados", "estadoPropuesta": 1 } ] – marcelo sazo Apr 26 '23 at 05:40
  • "I have not been able to send that json to get the results I need" => What does "sending that json" mean for you? You are talking about a GET call with a query parameter `tags` that you did not provide. There is no JSON involved in the request. What else do you need to know? – Seelenvirtuose Apr 26 '23 at 05:42
  • I understand what you mean, but the code is quite clear and the error I get is that. I have searched for this same error but have not found solutions. maybe I'm missing something and it must be simple but even my eyes do not see it. – marcelo sazo Apr 26 '23 at 05:43
  • check this img and tell me https://ibb.co/X72LKMT (https://imgbb.com/) – marcelo sazo Apr 26 '23 at 05:45
  • now an image without required = false https://ibb.co/4P7gfdx – marcelo sazo Apr 26 '23 at 05:47
  • What shall I tell you, that was not already told? In your screenshot the URL is `http://localhost:8080/propuesta/search`. There is no query parameter `tags` in it, isn't it? – Seelenvirtuose Apr 26 '23 at 05:48
  • Besides that: GET requests usually do not have a body. You seem to think that a request parameter must be provided in the body. No, it is a query paremeter in the URL. – Seelenvirtuose Apr 26 '23 at 05:49
  • localhost:8080/propuesta/search?tags=agua,contaminada it should not be like this? i dont get it yet my friend – marcelo sazo Apr 26 '23 at 05:51
  • @GetMapping("/search") public ResponseEntity> searchByTags(@RequestBody Map> body) { List tags = body.get("tags"); List propuestas = propuestaServices.SearchTags(tags); return ResponseEntity.ok(propuestas); } its work, in repository FindbyTagsIn(list<>, tags) Thanks @Seelenvirtuose – marcelo sazo Apr 26 '23 at 06:22

1 Answers1

0

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> tag) {
    List<PropuestaEntity> propuesta = propuestaServices.SearchTags(tag);
    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
times29
  • 2,782
  • 2
  • 21
  • 40