Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/378.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 根上下文路径的Spring引导错误处理_Java_Tomcat_Spring Boot_Jersey - Fatal编程技术网

Java 根上下文路径的Spring引导错误处理

Java 根上下文路径的Spring引导错误处理,java,tomcat,spring-boot,jersey,Java,Tomcat,Spring Boot,Jersey,我正在努力为与我定义的前缀不匹配的URL使用jersey和spring引导创建错误处理。请参阅此设置: application.properties: server.contextPath=/mainstay 起动器: @SpringBootApplication public class DemoApplication { public static void main(String[] args) { SpringApplication.run(DemoAppli

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

application.properties:

server.contextPath=/mainstay
起动器:

@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";
        }
    }
}
这将为我映射以下URL:

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-v
*正在尝试127.0.0.1。。。
*已连接到本地主机(127.0.0.1)端口8080(#0)
>GET/mainstay/test HTTP/1.1
>主机:本地主机:8080
>用户代理:curl/7.47.0
>接受:*/*
> 
这很好,它正确地映射了我的资源。现在,错误案例。提出以下请求:

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>
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
curl localhost:8080/mainstay/test/不存在-v
*正在尝试127.0.0.1。。。
*已连接到本地主机(127.0.0.1)端口8080(#0)
>GET/mainstay/test/不存在HTTP/1.1
>主机:本地主机:8080
>用户代理:curl/7.47.0
>接受:*/*
> 
类型状态报告

消息未找到

说明源服务器未找到目标资源的当前表示形式,或不愿意透露存在该表示形式。


ApacheTomcat/8.5.23
这将按预期打印默认的嵌入式tomcat错误页面,但是,现在发出以下请求:

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>
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
curl localhost:8080/test/不存在-v
*正在尝试127.0.0.1。。。
*已连接到本地主机(127.0.0.1)端口8080(#0)
>获取/测试/不存在HTTP/1.1
>主机:本地主机:8080
>用户代理:curl/7.47.0
>接受:*/*
> 
这不会映射到正确的错误页面。坦率地说,到目前为止,我还没有找到一种方法来为非前缀请求获取断点停止。这是因为所有servlet(我相信)都注册了前缀,因此在tomcat站点上根本没有映射到任何东西

我想知道是否有一种方法可以捕捉到这样一个事实,即没有使用spring boot映射URL以正确响应错误。在我的实际应用程序中,我使用jersey的异常映射器,但它们有相同的问题。因此,即使只是抛出一个我可以捕获的异常,也可以解决这个问题

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

我可以重新设计我的代码来使用子资源,但对于这样一个简单的案例来说,这也确实需要付出相当大的努力


谢谢!

如果这是Tomcat,我会建议您配置一个全局404错误。我真的很想听听您如何在Jersey中执行同样的操作。这也不起作用,因为使用上下文路径不会路由任何与之不匹配的内容。如果您以前点击了请求,它看起来就像什么也没做一样手动。如果删除上下文路径,则可以访问全局404处理程序。如果将服务器配置为在实际错误上发送错误代码,则可以使用jersey映射器捕获错误,与全局处理程序相同。问题是,我似乎无法匹配任何与我的上下文路径不匹配的内容。这似乎是这将是一个突出的tomcat问题:如果这是tomcat,我会建议您配置一个全局404错误。我真的很想听听您如何在Jersey中执行同样的操作。这也不起作用,因为使用上下文路径不会路由任何不匹配的内容。如果您事先点击请求。如果删除上下文路径,则可以访问全局404处理程序。如果将服务器配置为在实际错误上发送错误代码,则可以使用jersey映射器捕获错误,与全局处理程序相同。问题是,我似乎无法匹配任何与我的错误无关的内容上下文路径这似乎是一个突出的tomcat问题: