Home > Back-end >  How to get data from URL with Spring Boot?
How to get data from URL with Spring Boot?

Time:10-12

Basically, I need to get a data from my URL.

The HTML code looks like this:

<h4><a th:text="${h.nombre}"  style="color: #1c1f23;text-decoration: none;" th:href="@{/hoteles/{id}(id=${h.getId()})}"></a></h4>

Example of the kind of URL I get:

localhost:8080/hoteles/3

Now, I need to get that number and put it on the controller.

My current controller looks like this:

@RequestMapping("/hoteles/{item}")
public @ResponseBody ModelAndView resultadoHotel(@PathVariable(value="item") String numerito,
                                                 @RequestParam Integer id) {
    List<Hotel> listaHoteles = hotelService.getAll();
    BuscadorID numero = new BuscadorID(id);
    Hotel definitivo = buscadorService.Comparar(numero,listaHoteles);
    ModelAndView model = new ModelAndView("hotelWeb");
    model.addObject("definitivo", definitivo);
    return model;
}

I just don't know if I am doing something wrong. I don't get how the RequestParam works.

CodePudding user response:

In RequestParam without value, by default, it takes an empty String as per doc. try this...
@RequestMapping("/hoteles/{item}") public @ResponseBody ModelAndView resultadoHotel(@PathVariable(value="item") String numerito, @RequestParam(value = "id") Integer id) { List listaHoteles = hotelService.getAll(); BuscadorID numero = new BuscadorID(id); Hotel definitivo = buscadorService.Comparar(numero,listaHoteles); ModelAndView model = new ModelAndView("hotelWeb"); model.addObject("definitivo", definitivo); return model; }

  • Related