根上下文路径

时间:2017-11-13 14:02:33

标签: java tomcat spring-boot jersey

我正在努力使用jersey和spring boot为与我定义的前缀不匹配的url创建错误处理。请参阅此设置:

application.properties:

server.contextPath=/mainstay

Starter:

@SpringBootApplication
public class DemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }
}

资源:

@Component
public class ResourceConfig extends org.glassfish.jersey.server.ResourceConfig {

    public ResourceConfig() {
        register(HW.class);
    }


    @Path("test")
    public static class HW {

        @GET
        public String test() { 
            return "Hello test";
        }
    }
}

这将为我映射以下网址:

curl localhost:8080/mainstay/test -v
*   Trying 127.0.0.1...
* Connected to localhost (127.0.0.1) port 8080 (#0)
> GET /mainstay/test HTTP/1.1
> Host: localhost:8080
> User-Agent: curl/7.47.0
> Accept: */*
> 
< HTTP/1.1 200 
< Content-Type: text/plain
< Content-Length: 10
< Date: Mon, 13 Nov 2017 13:57:45 GMT
< 
* Connection #0 to host localhost left intact
Hello test

这很好,它正确映射了我的资源。现在,错误案例。提出此请求:

curl localhost:8080/mainstay/test/does_not_exist -v
*   Trying 127.0.0.1...
* Connected to localhost (127.0.0.1) port 8080 (#0)
> GET /mainstay/test/does_not_exist HTTP/1.1
> Host: localhost:8080
> User-Agent: curl/7.47.0
> Accept: */*
> 
< HTTP/1.1 404 
< Content-Type: text/html;charset=utf-8
< Content-Language: en
< Content-Length: 1078
< Date: Mon, 13 Nov 2017 13:58:21 GMT
< 
<!doctype html><html lang="en"><head><title>HTTP Status 404 – Not Found</title><style type="text/css">h1 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:22px;} h2 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:16px;} h3 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:14px;} body {font-family:Tahoma,Arial,sans-serif;color:black;background-color:white;} b {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;} p {font-family:Tahoma,Arial,sans-serif;background:white;color:black;font-size:12px;} a {color:black;} a.name {color:black;} .line {height:1px;background-color:#525D76;border:none;}</style></head><body><h1>HTTP Status 404 – Not Found</h1><hr class="line" /><p><b>Type</b> Status Report</p><p><b>Message</b> Not Found</p><p><b>Description</b> The origin server did not find a current representation for the target resource or is not willing to disclose that one exists.</p><hr clas* Connection #0 to host localhost left intact
s="line" /><h3>Apache Tomcat/8.5.23</h3></body></html>

这将按预期打印默认的嵌入式tomcat错误页面,但现在发出此请求:

curl localhost:8080/test/does_not_exist -v
*   Trying 127.0.0.1...
* Connected to localhost (127.0.0.1) port 8080 (#0)
> GET /test/does_not_exist HTTP/1.1
> Host: localhost:8080
> User-Agent: curl/7.47.0
> Accept: */*
> 
< HTTP/1.1 404 
< Content-Length: 0
< Date: Mon, 13 Nov 2017 13:58:54 GMT
< 
* Connection #0 to host localhost left intact

这不会映射到正确的错误页面。坦率地说,到目前为止,我还没有找到一种方法来获取断点来停止非前缀请求。这是因为所有的servlet(我相信)都记在前缀中,因此没有映射到tomcat站点上的任何内容。

我想知道是否有一种方法可以捕获URL未使用spring引导映射的事实,以便正确地对错误做出反应。在我的实际应用程序中,我使用的是jersey的异常映射器,但它们也有同样的问题。因此,即使只是抛出一个我能捕获的异常也会解决这个问题

我知道我可以删除上下文路径,这解决了我的问题,但是必须通过所有控制器类并手动管理前缀有点烦人。

我可以重新设计我的代码以使用子资源,但对于像这样的简单情况,这也感觉相当费力。

谢谢!

0 个答案:

没有答案