Custom 404 using Spring DispatcherServlet - spring

I've set up web.xml as below. I also have an annotation-based controller, which takes in any URL pattern and then goes to the corresponding jsp (I've set that up in the -servlet.xml). However, If I go to a page that ends in .html (and whose jsp doesn't exist), I don't see the custom 404 page (and see the below error in the log). Any page that doesn't end in .html, I can see the custom 404 page.
How can I configure to have a custom 404 page for any page that goes through the DispatcherServlet?
Also want to add that if I set my error page to a static page (ie. error.htm) it works, but if I change it to a jsp (ie. error.jsp), I get the IllegalStateException. Any help would be appreciated.
log error
Caused by: java.lang.IllegalStateException: getOutputStream() has already been called for this response
at org.apache.catalina.connector.Response.getWriter(Response.java:606)
at org.apache.catalina.connector.ResponseFacade.getWriter(ResponseFacade.java:195)
at org.apache.jasper.runtime.JspWriterImpl.initOut(JspWriterImpl.java:124)
controller
#RequestMapping(value = {"/**"})
public ModelAndView test() {
ModelAndView modelAndView = new ModelAndView();
return modelAndView;
}
web.xml
<servlet>
<servlet-name>my_servlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
</servlet>
...
<servlet-mapping>
<servlet-name>my_servlet</servlet-name>
<url-pattern>*.html</url-pattern>
</servlet-mapping>
...
<error-page>
<error-code>404</error-code>
<location>/error.html</location>
</error-page>

One option is to map all your error pages through your dispatcher servlet.
Create a new HTTP error controller:
#Controller
public class HTTPErrorController {
#RequestMapping(value="/errors/404.html")
public String handle404() {
return "errorPageTemplate";
}
#RequestMapping(value="/errors/403.html")
...
}
Map the error pages in web.xml
<error-page>
<error-code>404</error-code>
<location>/errors/404.html</location>
</error-page>

Related

Spring MVC: calls from JSP not going to the controller

I am trying to pass parameters from JSP to the Spring MVC controller. However the controller doesn't get called. I have read many related posts regarding this and have tried various solutions provided but it's not working for me.
My web.xml looks like this:
<display-name>MyList</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>mylist</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>mylist</servlet-name>
<url-pattern>/welcome.jsp</url-pattern>
<url-pattern>/welcome.html</url-pattern>
<url-pattern>*.html</url-pattern>
</servlet-mapping>
My Controller:
#RequestMapping(path = "/subcategory/{id}", method = RequestMethod.GET)
public String findSubcategory(#PathVariable int id,Model model) {
List<Category> subCategoryList = this.myListDao.getSubCategories(id);
model.addAttribute("subcategories", subCategoryList);
return "searchCategory" ;
}
The relevant code from My JSP is as follows
<spring:url value="/subcategory/1" var="formUrl"/>
<c:forEach var="category" varStatus="status" items="${categories}">
<li>${category.description}</li>
</c:forEach>
It works when I have the url as /subcategory.html and have #RequestMapping("/subcategory") in my controller. When I use /subcategory/1 and change the corresponding request mapping, it does not work. I have tried various url patterns such as <url-pattern>/mylist/*</url-pattern><url-pattern>/MyList/*</url-pattern>etc. but nothing is working. I would really appreciate any help in solving this. Thanks a lot in advance.
Your current servlet mapping is overriding the default or root servlet mapping,
<servlet-mapping>
<servlet-name>mylist</servlet-name>
<url-pattern>/welcome.jsp</url-pattern>
<url-pattern>/welcome.html</url-pattern>
<url-pattern>*.html</url-pattern>
</servlet-mapping>
Your following mapping allows any request with *.html to be accepted hence your invocation to /subcategory.html worked without any issues.
<url-pattern>*.html</url-pattern>
Instead it should be changed to,
<servlet-mapping>
<servlet-name>mylist</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
Some points to remember,
<url-pattern>/*</url-pattern>
This pattern is usually recommended with Filter to continue with filter chaining. Using this pattern otherwise will make you to take care of all responsibilities like serving static resources etc to be handled explicitly.
#RequestMapping(path = "/subcategory/{id}", method = RequestMethod.GET)
instead of path, you should use value, like this:
#RequestMapping(value = "/subcategory/{id}", method = RequestMethod.GET)
Your call cannot reach the controller because your request mapping "/subcategory/{id}" does not match any url-patterns defined with your dispatcher servlet. You can try replacing these lines:
<url-pattern>/welcome.jsp</url-pattern>
<url-pattern>/welcome.html</url-pattern>
<url-pattern>*.html</url-pattern>
with
<url-pattern>/*</url-pattern>

404 Error from REST Endpoint in SpringMVC for DispatcherServlet

RESOLVED PER THIS ANSWER
Spring invokes wrong controller mapping
Spring #Controllers URLs are always interpreted relative the the Spring Dispatcher Servlet that handles them. So if you map the dispatcher servlet to /api/ in web.xml then the URL to your controller above is /api/api/choice
The double string service/service/1234 was working.
ORIGINAL POST
Accessing a REST resource endpoint gives me a 404 error although everything seems to be defined correctly:
Log output:
DEBUG DispatcherServlet with name 'mvc-dispatcher' processing GET request for [/myapp/service/1234]
DEBUG Looking up handler method for path /1234
DEBUG Did not find handler method for [/1234]
WARN No mapping found for HTTP request with URI [/myapp/service/1234] in DispatcherServlet with name 'mvc-dispatcher'
web.xml
<servlet>
<servlet-name>mvc-dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextClass</param-name>
<param-value>org.springframework.web.context.support.AnnotationConfigWebApplicationContext</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>mvc-dispatcher</servlet-name>
<url-pattern>/service/*</url-pattern>
</servlet-mapping>
SpringMVC Controller
#RestController
#RequestMapping("/service")
public class RESTController {
#RequestMapping(value="/{id}", method=RequestMethod.GET)
public String getResult ( #PathVariable String id )
{
//... get JSON result
}
}
Expected invocation: myapp/service/1234
Also tried these options:
1) Don't define a class RequestMapping, just do a Method Request Mapping
#RequestMapping("/service/{id}")
2) as well as
#RequestMapping("/service*/{id}")
#RequestMapping("/service**/{id}")
Keep getting a 404 with the log above.
update your web.xml file :
<servlet-mapping>
<servlet-name>mvc-dispatcher</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>

Spring unit test 404\unknown url in mock mvc

I would like to test that when an unknown url is requested and a 404 error is generated that my web app actually redirects to the right place.
I havent been able to get this working, I think because tomcat is handling the 404 errors so the forwardedUrl is always null for the tests. I know this works in reality because if I enter some rubbish into the url my app does redirect to my custom page.
My unit test looks like:
#Test
public void testUnknownUrl() throws Exception {
mockMvc.perform(get("/url_doesnt_exist"))
.andExpect(status().isNotFound())
.andExpect(forwardedUrl("/static/error/Sorry.html"));
}
My web.xml configuration is:
<servlet>
<servlet-name>appServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>appServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<error-page>
<error-code>404</error-code>
<location>/static/error/Sorry.html</location>
</error-page>
The mapping for /static is defined in my spring config like:
<resources mapping="/static/**" location="/resources/" />
Ultimately I would like to mock a request to an unknown url and then check that the page being returned is /static/error/Sorry.html.
Am I doing something wrong or is this not the way to handle 404 etc in spring? The check of the forwarded url in the unit test is always null.
A slightly different question but related all the same is, at what point does the tomcat error handling get invoked over and above the spring controller advice handling?
I'm not sure about the configuration with the /static path in your web.xml, it shouldn't be like that depending on your dispatcher-servlet (default name) configuration; but as far as I can tell you are in the right path.
This is what I have for mine:
#WebAppConfiguration
#ActiveProfiles("development")
#RunWith(SpringJUnit4ClassRunner.class)
#ContextConfiguration(classes = AppConfig.class)
public class ErrorControllerTest {
#Autowired
private WebApplicationContext context;
private MockMvc mockMvc;
#Before
public void setUp() {
mockMvc = MockMvcBuilders.webAppContextSetup(context)/*.alwaysExpect(status().isOk())*/.build();
}
#Test
public void testError() throws Exception {
mockMvc.perform(get("/error").contentType(MediaType.TEXT_HTML))
.andExpect(status().isOk()) // See "alwaysExpect" above
.andExpect(view().name("error"))
.andExpect(forwardedUrl("/WEB-INF/views/error.jsp"));
}
#Test
public void testResourceNotFound() throws Exception {
mockMvc.perform(get("/resource-not-found").contentType(MediaType.TEXT_HTML))
.andExpect(status().isNotFound())
.andExpect(view().name("resource-not-found"))
.andExpect(forwardedUrl("/WEB-INF/views/resource-not-found.jsp"));
}
}
Sample project is here. I'm using JSPs but you can switch to .html just by changing the InternalResourceViewResolver configuration.
I am using spring 3 and after some reading around I have found out the dispatcher servlet just returns a response code without throwing an exception which I guess is why tomcat always handles this.
Removing the error page tags from web.xml results in a tomcat generic 404 page, so I think the answer to this is to upgrade to spring 4 where I can then pass an init param to the dispatcher servlet requesting it throw an error for a page not found.
I am happy to be corrected on this though as it may help my understanding.

Different 404 error page depending on the user role

I'm developing a web application using spring mvc, and spring-security for authentication.
So I have three roles: ROLE_ADMIN, ROLE_CONS and ROLE_CP
And I have three 404 pages 404CP.jsp, 404Cons.jsp and 404Admin.jsp
But the problem is that I only know how to specify the error page for the whole application, in web.xml file:
<error-page>
<error-code>404</error-code>
<location>/presentation/404CP.jsp</location>
</error-page>
So I want to know how to specify a different 404 page for each role
Thanks in advance
Try this:
web.xml:
<error-page>
<error-code>404</error-code>
<location>/notFound</location>
</error-page>
ErrorController:
#Controller
public class ErrorController {
#RequestMapping("/notFound")
public String notFound(HttpServletRequest request) {
if (request.isUserInRole("ROLE_ADMIN")) {
return "404Admin";
} else if (request.isUserInRole("ROLE_CONS")) {
return "404Cons";
}
// ...
}
}
The error pages must be in the directory where your views are located (so that they will be correctly resolved by the view resolver).

Servlet Mapping Help - Possible to Avoid Referencing Context Name?

I am working on a Spring application using Tomcat 6 and Spring 2.5. I'm trying to get my URL mapping correct. What I would like to have work is the following:
http://localhost:8080/idptest -> doesn't work
But instead, I have to reference the context name in my URL in order to resolve the mapping:
http://localhost:8080/<context_name>/idptest -> works
How can I avoid the requirement of referencing the context name in my URL without using a rewrite/proxy engine e.g. Apache?
Here is the servlet definition and mapping from my web.xml:
<servlet>
<servlet-name>idptest</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/conf/idptest.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>idptest</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
Here's the outline of my controller (showing annotations for request mappings):
#Controller
#RequestMapping("/idptest")
public class MyController {
#RequestMapping(method=RequestMethod.GET)
public String setupForm(Model model){
MyObject someObject = new MyObject();
model.addAttribute("someObject", someObject);
return "myform";
}
#RequestMapping(method = RequestMethod.POST)
public String processSubmit(#ModelAttribute("someObject") MyObject someObject) throws Exception {
// POST logic...
}
}
Thanks!
That's going to depend on your servlet container, for Tomcat - you pretty much have to deploy your webapp as the ROOT webapp, that is, under $CATALINA_HOME/webapps/ROOT/
More info here
Just rename your war file to ROOT.war, then the application runs in root context (i.e. with empty context name)

Resources