简体   繁体   中英

Why am I getting a 404 error in my Spring Boot Web app?

I'm new to Spring and I'm creating a simple MVC web app that displays information on Bands. I have a controller like so:

@Controller
@RequestMapping("/band")
public class BandController {

    @Autowired
    BandRepository bandRepository;

    @RequestMapping("")
    public String index(Model model) {

        List<Band> bands = bandRepository.findAll();
        model.addAttribute("bands", bands);
        return "band/bands";

    }

    @RequestMapping("/{id}")
    public String viewBand(@PathVariable String id, Model model) {

        Band band = bandRepository.get(id);
        model.addAttribute("band", band);
        return "band/viewBand";

    }

}

The displaying of a list of bands works fine but passing through an id brings up a Whitelabel Error Page saying "There was an unexpected error (type=Not Found, status=404)."

My app is using Spring Boot with JDBCTemplate and Thymeleaf. I have a templates folder with a band folder inside containing the bands.html and viewBand.html.

I've only been using Annotations thus far.

EDIT: URL I'm using: http://localhost:8080/band/viewBand/2

This doesn't work because in your request mapping, you are mapping it to http://localhost:8080/band/{id} . Try changing the @RequestMapping("/{id}") to @RequestMapping("/viewBand/{id}")

Method names do not affect the URL patterns that Spring uses.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM