如何在Spring框架中提供robots.txt?

时间:2018-05-03 23:37:52

标签: java spring spring-boot

我看到了this,但它已经过时了。我尝试了以下方法:

创建src/main/resources/static/robots.txt

@Configuration
public class WebConfig extends WebMvcConfigurerAdapter{

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
//        registry.addResourceHandler("/robots.txt").addResourceLocations("classpath:/static/robots.txt");
//        registry.addResourceHandler("/robots.txt").addResourceLocations("/static/robots.txt");
//        registry.addResourceHandler("/robots.txt").addResourceLocations("/static/");
        registry.addResourceHandler("/robots.txt").addResourceLocations("classpath:/static/");

每一次,

$ curl -i http://localhost:8080/robots.txt
HTTP/1.1 302
Set-Cookie: JSESSIONID=D1E5304FE8E693115A1FFB0F76786ABD; Path=/; HttpOnly
Location: http://localhost:8080/pageNotFound
Content-Length: 0

然而静态CSS有效。

$ curl -iq http://localhost:8080/css/style.css | head
HTTP/1.1 200

我每次更改代码时都杀了服务器并运行mvn spring-boot:run

当我启动服务器时它会这样说:

2018-05-03 17:22:18.639  INFO 25148 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/robots.txt] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
...
2018-05-03 17:22:39.139  INFO 25148 --- [nio-8080-exec-1] c.s.shorturl.apis.ShortUrlApiController  : Method name: doUrlRequest() request http method is GET

正在执行此操作。

@Controller
@Scope("session")
@ApiIgnore
public class ProjectShortUrlController implements ErrorController{
    @RequestMapping(value = "/*")
    public void doUrlRequest(HttpServletRequest request, HttpServletResponse response) {
      CustomLogger.info(TAG, "rediection: ", "Method name: doUrlRequest() request http method is "+request.getMethod());

文档:https://docs.spring.io/spring/docs/5.1.0.BUILD-SNAPSHOT/spring-framework-reference/web.html#mvc-config-static-resources

春季4.3.10

我发现如果我这样做了

@Configuration
public class WebConfig extends WebMvcConfigurationSupport {
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/robots.txt").addResourceLocations("/static/robots.txt");

然后robots.txt正常工作

$ curl -i http://localhost:8080/robots.txt
HTTP/1.1 200
Content-Type: text/plain
Content-Length: 28

User-agent: *
Disallow: /

但所有其他网址开始提供" Whitelabel错误页面"!

2 个答案:

答案 0 :(得分:1)

我放弃了WebMvcConfigurerAdapter。我认为控制器请求的优先级高于资源处理程序。我只是在控制器中使用了它。

@RequestMapping(value = "/robots.txt")
public void robots(HttpServletRequest request, HttpServletResponse response) {
    try {
        response.getWriter().write("User-agent: *\nDisallow: /\n");
    } catch (IOException e) {
        CustomLogger.info(TAG, "robots", "robots(): "+e.getMessage());
    }
}

我可能遇到了麻烦,因为有一个@RequestMapping(value = "/*")路径。

答案 1 :(得分:0)

application-context.xml 中,我厌倦了以下方式,并且工作正常。

<mvc:resources mapping="/robots.txt" location="/custom/bots/robots.txt" />
<mvc:resources mapping="/favicon.ico" location="/custom/images/main_logo_pink.png" />

与项目中的其他静态文件一样,我也放在下面。

<mvc:annotation-driven />

<mvc:resources mapping="/custom/styles/**" location="/custom/styles/" />
<mvc:resources mapping="/custom/scripts/**" location="/custom/scripts/" />
<mvc:resources mapping="/custom/images/**" location="/custom/images/" />