How can I serve static html from spring boot?

前端 未结 6 919
感情败类
感情败类 2020-12-05 06:18

I ran the spring-boot-sample-web-static project from here, made this alteration to the pom


    org.springframework.boot&l         


        
6条回答
  •  没有蜡笔的小新
    2020-12-05 07:17

    As it is written before, some folders (/META-INF/resources/, /resources/, /static/, /public/) serve static content by default, conroller misconfiguration can break this behaviour.

    It is a common pitfall that people define the base url of a controller in the @RestController annotation, instead of the @RequestMapping annotation on the top of the controllers.

    This is wrong:

    @RestController("/api/base")
    public class MyController {
    
        @PostMapping
        public String myPostMethod( ...) {
    

    The above example will prevent you from opening the index.html. The Spring expects a POST method at the root, because the myPostMethod is mapped to the "/" path.

    You have to use this instead:

    @RestController
    @RequestMapping("/api/base")
    public class MyController {
    
        @PostMapping
        public String myPostMethod( ...) {
    

提交回复
热议问题