Spring MVC 404 even though URL is mapped - spring

I've been trying to setup a Spring MVC controller but when I try to make a GET request, I get a 404 error.
I created a working test example here: https://github.com/Jardo-51/zk-spring-mvc-test
When I run the application on Tomcat and try to make a GET request to: http://localhost:8080/zk-spring-mvc-test/api/v0/foo, I get a 404 error and the logs say:
WARNING: No mapping found for HTTP request with URI [/zk-spring-mvc-test/api/v0/foo] in DispatcherServlet with name 'dispatcher-api'`
I've been trying to fix it according to this answer, and found out that the controller is mapped correctly because the logs on startup say:
INFO: Mapped "{[/zk-spring-mvc-test/api/v0/foo],methods=[GET]}" onto public org.springframework.http.ResponseEntity<java.lang.String> com.jardoapps.zkspringmvctest.controllers.FooController.method()
The app uses ZK framework which needs its own servlets so maybe there is a conflict with the DispatcherServlet. Please see my example app for more details (it contains only the necessary code).
Here is the web.xlm (Spring context and MVC config are at the top).
Here is the controller class.

Simply replace #RequestMapping("zk-spring-mvc-test/api/v0/foo") with #RequestMapping("/v0/foo") in your FooController class.
The reason is that the path that you specify into the #RequestMapping annotation is the part of the request's URL beyond the part that called the servlet.
You defined DispatcherServlet's mapping as:
<servlet-mapping>
<servlet-name>dispatcher-api</servlet-name>
<url-pattern>/api/*</url-pattern>
</servlet-mapping>
So we have zk-spring-mvc-test that is the context root (this is deploy dependent), /api/ that calls the Spring DispatcherServlet, and finally /v0/foo that should be mapped by your controller:
#RestController
#RequestMapping("/v0/foo")
public class FooController {
#RequestMapping(method = RequestMethod.GET)
public ResponseEntity<String> method() {
return ResponseEntity.ok().body("OK");
}
}
You can see Spring MVC configure url-pattern for further information.

Related

Would someone answer some questions about Spring and Request Mapping?

I just started studying Spring, and I'm so confused.
I just created a new 'Spring Legacy Project' at STS. HomeController and home.jsp are there.
When I run it on server, it comes through the HomeController first, and arrives to home.jsp.
#RequestMapping(value = "/", method = RequestMethod.GET)
public String home(Locale locale, Model model) {
return "home";
}
What makes my project go through the HomeController at the beginning? Should I look at any xml file?
What does value="/" mean in #RequestMapping?
At the home.jsp, I made a button to go 'result.jsp'. From the 'result.jsp' I want to go back to home.jsp. but it doesn't work. What should I do?
<input type="button" value="뒤로 " onclick="javascript:location.href='/views/home.jsp'">
Why isn't this button working? Those two JSP files are in same place.
Your app is deployed to some app server, such as tomcat. The request <app server>/<context root> is handled by the app server to the .war with the appropriate context root, e.g. to your app. Your app uses Spring MVC, so it is Spring's RequestMappingHandlerMapping bean from your .war file that initially handles the request and finds your method that will handle this request. It does so by comparing the path in the request with the value of each method annotated with #RequestMapping.
The annotation #RequestMapping(value="/") of your home() method means that request <app server>/<context root> will be handled by your 'home()' method.
Any request from your JSP will go back to the Spring MVC that will try to map it to the appropriate controller method, i.e. to the method annotated by #RequestMapping with the appropriate path relative to the context root. So if your result.jsp just links to "/", it should bring you to the home() method and then to the home.jsp.

Spring Boot + Jersey + view controller not working together [duplicate]

I am getting a HTTP 404 error when trying to serve index.html ( located under main/resources/static) from a spring boot app. However if I remove the Jersey based JAX-RS class from the project, then http://localhost:8080/index.html works fine.
The following is main class
#SpringBootApplication
public class BootWebApplication {
public static void main(String[] args) {
SpringApplication.run(BootWebApplication.class, args);
}
}
I am not sure if I am missing something here.
Thanks
The problem is the default setting of the Jersey servlet path, which defaults to /*. This hogs up all the requests, including request to the default servlet for static content. So the request is going to Jersey looking for the static content, and when it can't find the resource within the Jersey application, it will send out a 404.
You have a couple options around this:
Configure Jerse runtime as a filter (instead of as a servlet by default). See this post for how you can do that. Also with this option, you need to configure one of the ServletProperties to forward the 404s to the servlet container. You can use the property that configures Jersey to forward all request which results in a Jersey resource not being found, or the property that allows you to configure a regex pattern for requests to foward.
You can simply change the Jersey servlet pattern to something else other than the default. The easiest way to do that is to annotate your ResourceConfig subclass with #ApplicationPath("/root-path"). Or you can configure it in your application.properties - spring.jersey.applicationPath.

How can I tell if Spring has loaded my #Controller?

Is there a way to tell if Spring has loaded my #Controller?
I'm requesting a URL but I'm not hitting my controller and I can't figure out why
I'm loading controllers by doing a component scan
<context:component-scan base-package="com.example.app.web"/>
Other controllers in the same package as my failing controller are working fine.
My controller code is:
#Controller
#RequestMapping(value = "/app/administration/ecosystem")
public class AppEcosystemController {
#Autowired
EcosystemManagerService ecosystemManagerService;
#RequestMapping(value = "/Foo", method = RequestMethod.GET)
public String getEcosystem() {
/* Implementation */
}
The first thing I'd like to do is to be sure that this controller is getting picked up by the component scan.
Any suggestions?
Just enable logging for your application, you can find this information at INFO level
For example in my application I have a controller named UserController.
The following log4j.properties does the trick.
log4j.rootLogger=INFO, FILE
log4j.appender.FILE=org.apache.log4j.FileAppender
log4j.appender.FILE.File=../logs/rest-json.log
log4j.appender.FILE.layout=org.apache.log4j.PatternLayout
log4j.appender.FILE.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n
I can see in the log that RequestMappingHandlerMapping mapped my controller (scroll all the way to the right).
07:28:36,255 INFO RequestMappingHandlerMapping:182 - Mapped "{[/rest/**/users/{id}],methods=[GET],params=[],headers=[],consumes=[],produces=[text/xml || application/json],custom=[]}" onto public org..domain.User org.ramanh.controller.UserController.getUser(java.lang.String)
07:28:36,255 INFO RequestMappingHandlerMapping:182 - Mapped "{[/rest/**/users],methods=[POST],params=[],headers=[],consumes=[],produces=[text/xml || application/json],custom=[]}" onto public void org..controller.UserController.addUser(org...domain.User)
If you are still unsure I would suggest adding a method annotated with #PostConstruct.
You could easily look up the message in the log or place a break point in this method.
#PostConstruct
protected void iamAlive(){
log.info(“Hello AppEcosystemController”)
}
If you find that your controller is initialized correctly but still the url is not accessible.I would test the following
You are getting 404 error - maybe you are not pointing to the correct
url (do not forget to add the application as prefix to the url)
You are getting 404 error - Dispatcher servlet mapping in web.xml doesn't meet
the url above
You are getting 403/401 – maybe you are using
spring security and it’s blocking the url
You are getting 406 – your
content type definition is conflicting with your request
You are getting 50x – something is buggy in your code
I made an ApplicationContextDumper. Add it into application context, it will dump all beans and their dependencies in the current context and parent contexts (if any) into log file when the application context initialization finishes. It also lists the beans which aren’t referenced.
It was inspired by this answer.
You could start out with enabling debug logging for Spring as outlined here.
I'd also recommend leveraging the MVC testing support, which you'll find in the spring-test jar. Details on how to use it can be found here and here.

getting the HTTP URL from inside a JAX-RS resource

From inside my JAX-RS (Jersey) resource I need to get the base URL of the Jersey Servlet that's "publishing" that resource. I tried injecting ServletContext as described here, and then doing a:
return servletContext.getResource("/").toString();
to get the "base" URL of the Jersey Servlet for this resource.
However the above code returns a value like:
jndi:/localhost/jax-rs-tomcat-test/
where I was expecting something more like:
http://localhost:8080/jax-rs-tomcat-test/jax-rs
Where "jax-rs" is what I have in my web.xml:
<servlet-mapping>
<servlet-name>jersey-servlet</servlet-name>
<url-pattern>/jax-rs/*</url-pattern>
</servlet-mapping>
That is, there are four "differences": (a) protocol, (b) single instead of double slash after the protocol, (c) port number and (d) missing URL pattern for triggering the Jersey servlet. So, how do I get:
the base http:// URL of the Jersey servlet
the full URL that triggered a particular #GET or #POST annotated method ?
You're looking for UriInfo. Inject it into your resource using #Context:
#Context
private UriInfo uriInfo;
and then you can call getBaseUri() method:
uriInfo.getBaseUri();

No mapping found for HTTP request

I am back with working in Springs. I used to work in Springs but blindly, didn't understand much. I used to get a lot of errors, very basic ones, and I am getting them again.
My problem is that, I don't know how the configuration of the Spring-MVC work.
What happens when I run the project from my STS?
I am working on the spring template project in STS.
I am getting this when I run the project.
WARN : org.springframework.web.servlet.PageNotFound - No mapping found for HTTP request with URI [/common/] in DispatcherServlet with name 'appServlet'
I am totally fed up and broken.
Just 2 months of break from work, I am back at the starting block.
I don't want to post my code and make the question specific.
I want an answer that explains the way in which the server executes a spring project. Right from the running of an application(basic hello world application) to the display of the home page.
This will be helpful for all the beginners.
I tried searching for such an explanation in the net but I didn't get any proper explanation, but got a lot of basic samples. Those samples are easy to understand but are not explaining the way in which the server goes about.
Note: I am looking for an answer that explains the Springs concept. From the running of an application to the display of a home page. What all happens in this process? Where does the server start with? How does it go about?
Here is the flow initially servlet container loads the web.xml file.In web.xml we will specify that all the requests are handled by the spring FrontController that is DispatcherServlet.
We include it by adding the following code
<servlet>
<servlet-name>dispatcher</servlet-name>
<servletclass>org.springframework.web.servlet.DispatcherServlet</servletclass>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>*.htm</url-pattern>
</servlet-mapping>
Here it indicate if the url request is of *.htm it is handled by dispatcherServlet then dispatcherServlet load dispatcher-servlet.xml . Where we need to mention the mapping to controller by writing the specific url request such as
<bean name="/insert.htm" class="com.controller.MyController"></bean>
So in bean we mention that for request of /insert.htm it tells the servlet to look in the mentioned class.You need use the Annotation of #RequestMapping above the method for ex
#RequestMapping("/insert.htm")
public ModelAndView insert(HttpServletRequest req,Student student)
{
String name=req.getParameter("name");
int id=Integer.parseInt(req.getParameter("id"));
student.setId(id);
return new ModelAndView("display","Student",student);//It returns a view named display with modelclass name as `Student` and model object student
}
So when a Request url of /insert.htm appears it executes the above method it returns a ModelAndView object nothing but an view.It again goes to dispatcher-servlet.xml and looks for view Resolver the normal code that is to be added is
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver"
p:prefix="/WEB-INF/jsp/"
p:suffix=".jsp" />
So from this it gets the logical view name and appends the prefix and suffix to it .Finally it displays the content in the view.so it looks for display in view resolver prefixes and suffixes the things and finally returns /WEB-INF/jsp/display.jsp .Which displays the jsp content
You are mapping your Spring servlet only for requests that end with .htm. The request for the root of your application does not end with .htm and so, it does not get picked up by Spring. Edit your web.xml as follows, in order to use Spring for all requests:
<servlet-mapping>
<servlet-name>appServlet</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
Then, use this as the controller:
package com.mkyong.common;
#Controller
public class HomeController {
#RequestMapping(value = "/", method = RequestMethod.GET)
public ModelAndView helloWorld() {
ModelAndView model = new ModelAndView("index");
model.addObject("msg", "hello world");
return model;
}
}
The controller intercepts the requests for the context root of the application, adds the msg attribute to the model and redirects to the index view.
So, you need to add the index.jsp file in the /WEB-INF/views/ directory. Inside your jsp, you will be able to use the value of the msg attribute.
From what every you have posted you do no have a request mapping for the url /common/.
You will have to create another request mapping function like the one below in your controller class and create a view file also.
#RequestMapping(value = "/common/", method = RequestMethod.GET)
public ModelAndView common(HttpServletRequest request,
HttpServletResponse response) {
ModelAndView model = new ModelAndView("common");
model.addObject("msg", "hello world");
return model;
}

Resources