简体   繁体   English

静态html春季启动时出现405错误代码

[英]405 error code on static html spring boot

I am trying to return a static html( index.html ) page using spring boot but I am alway getting a 405 error whem I trying ( http://localhost:8080/ ). 我正在尝试使用Spring Boot返回一个静态的html( index.html )页面,但是我总是在尝试( http:// localhost:8080 / )时收到405错误。 Strange fact is that debugger enters index() method. 奇怪的事实是调试器进入index()方法。

HomeController:

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
public class HomeController {

    @RequestMapping(value = "/", method = RequestMethod.GET)
    public String index() {
        return "index.html";
    }
}

I have tried to return "index.html" and "index" strings. 我试图返回"index.html"和“ index”字符串。

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;

@SpringBootApplication
public class Application {

    public static void main(String[] args) throws InterruptedException {
        ConfigurableApplicationContext applicationContext
                = SpringApplication.run(Application.class, args);
    }
}

location of html file is: src\\main\\resources\\public\\index.html html文件的位置是: src\\main\\resources\\public\\index.html

here goes a part of startup logger output: 这是启动记录器输出的一部分:

INFO 8284 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/],methods=[GET]}" onto public java.lang.String com.acs.map.controller.HomeController.index()

Screanshot of an error 错误的画面 在此处输入图片说明

and i am running project with gradle: gradle bootRun 我正在使用gradle运行项目: gradle bootRun

Logger message after request: 请求后的记录器消息:

WARN 3988 --- [nio-8080-exec-6] o.s.web.servlet.PageNotFound             : Request method 'GET' not supported

Also I have tried with and without this configuration: 我也尝试过使用和不使用此配置:

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.view.InternalResourceView;
import org.springframework.web.servlet.view.UrlBasedViewResolver;

@Configuration
@EnableWebMvc
public class WebConfig extends WebMvcConfigurerAdapter {

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/public/**").addResourceLocations("classpath:/public/");
        registry.addResourceHandler("/resources/public/**").addResourceLocations("classpath:/resources/public/");
        super.addResourceHandlers(registry);
    }

    @Bean
    public ViewResolver viewResolver() {
        UrlBasedViewResolver viewResolver = new UrlBasedViewResolver();
        viewResolver.setViewClass(InternalResourceView.class);
        return viewResolver;
    }
}

By default Spring Boot will serve static content from a directory called /static (or /public or /resources or /META-INF/resources). 默认情况下,Spring Boot将提供来自名为/ static(或/ public或/ resources或/ META-INF / resources)的目录中的静态内容。 I did a quick check by following the below structure and was successful. 我按照以下结构进行了快速检查,并成功。 在此处输入图片说明

So I believe by extending WebMvcConfigurerAdapter class like below and it should return static content using your current controller code (without the WebConfig class).You can use viewResolver as well to map between view names and actual views as well by further modifying your code. 因此,我相信通过扩展如下所示的WebMvcConfigurerAdapter类,它应该使用当前的控制器代码(不带WebConfig类)返回静态内容。您还可以使用viewResolver在视图名称和实际视图之间进行映射,还可以通过进一步修改代码来实现。

    @SpringBootApplication
public class Application extends WebMvcConfigurerAdapter{
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

Here are my project dependencies 这是我的项目依赖项 在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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