question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Error Handling pre contextPath does not work as expected

See original GitHub issue

Statement:

The error handling for 404 errors (or general error pages) seems to not be working. If a contextPath is set, all servlets are initialised with this context path and there is nothing handling URL requests for URLs that do not match the context path.

Bug Report:

I created a demo project using the spring boot intialiser with jersey:

Versions:

spring-boot-starter-jersey:1.5.8.RELEASE spring-boot-starter-test:1.5.8.RELEASE

I created a simple setup that creates a REST Application such as this:

@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";
        }
    }
}

I set the context path to:

server.contextPath=/mainstay

The server starts and it will now use the default error page registered with tomcat for any 404 after `localhost:8080/mainstay’ and it will respond with a random 404 error for all other requests that do not match the prefix.

On curl:

Success:

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

Successful error case:

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>

Faulty error case that does not match the servlet contextpath:

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

If I refactor my code and annotate the entire prefix into my jersey resource all works as expected, all requests get routed and the error handler’s are called. It enables me to also send the exception and catch it with jersey’s exception mappers to handle it.

However I have not found a way to register a custom error handle for this case.

I hope this makes sense. Please let me know if I can do anything else to add value to this issue.

Regards,

Artur

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:11 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
markt-asfcommented, Nov 17, 2017

Tomcat expects that a ROOT context is configured. It doesn’t configure one automatically. The ability to configure global error pages (for this case and 500 errors that don’t get as far as a Context) is a long-standing enhancement request. See https://bz.apache.org/bugzilla/show_bug.cgi?id=41007 There isn’t an obvious implementation option for this enhancement which, combined with little user interest in the feature, is why, I suspect, it remains unimplemented.

0reactions
philwebbcommented, Jul 4, 2019

@jzoe If you’re seeing a similar problem with the latest Spring Boot version please open a new issue and attach a small sample that we can run. Perhaps there’s a regression that we’ve missed.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Add context path to Spring Boot application - Stack Overflow
The new root of the application should be at localhost:12378/mainstay , but it is still located at localhost:12378 . What am I missing...
Read more >
Configure the WebView Application Context Path - TechDocs
You can optionally configure the application context path for the WebView installation. This path will be appended to the fully qualified ...
Read more >
Apache Tomcat 9 (9.0.70) - Manager App How-To
The context path is not used when deploying a web application ... Otherwise, the response will start with FAIL and include an error...
Read more >
Release Notes Red Hat JBoss Fuse Service Works 6.0
This document contains late-breaking news and known issues affecting the release ... This is due to a bug in Red Hat JBoss Enterprise...
Read more >
Spring Boot Reference Documentation
Also, there is nothing special about a Spring Boot application, so you can ... This naming structure is intended to help when you...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found