java - How do I serve robots.txt in Spring framework?
问题描述
I saw this but it's obsolete. I tried the following:
Created 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/");
Each time,
$ 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
However static CSS works.
$ curl -iq http://localhost:8080/css/style.css | head
HTTP/1.1 200
I killed the server and ran mvn spring-boot:run
each time I changed the code.
It says this when I start the server:
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
It is executing this instead.
@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());
Documentation: https://docs.spring.io/spring/docs/5.1.0.BUILD-SNAPSHOT/spring-framework-reference/web.html#mvc-config-static-resources
Spring 4.3.10
I found if I do this
@Configuration
public class WebConfig extends WebMvcConfigurationSupport {
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/robots.txt").addResourceLocations("/static/robots.txt");
Then robots.txt
works
$ curl -i http://localhost:8080/robots.txt
HTTP/1.1 200
Content-Type: text/plain
Content-Length: 28
User-agent: *
Disallow: /
But all other URLs start to give "Whitelabel Error Page"!
解决方案
我放弃了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 = "/*")
路。
推荐阅读
- arrays - 在 N 个数组中找到共同的值
- python - 获得 x 值不相同的几条曲线的平均曲线
- typescript - 使元素的类型为接口 A 或接口 B
- javascript - 基本的“if”语句在车把 JS 中不起作用
- java - Websphere 在 EAR 中更改 JNDI 名称
- netbeans - 像 Netbeans 一样在 VS Code 中的新行周围自动添加引号
- python - 检测网站是否限制导航其网站,直到您接受警报或弹出窗口 - 在 python 中使用 selenium webdriver
- r - 子集调查设计的问题
- php - 即使使用 ajaxComplete() 函数,jQuery 代码也不会应用于动态内容。有什么建议么?
- onelogin - 在数据库 onelogin 中添加 Secret