Getting 404 requested resource not available in spring restful web services
Solution 1
If you want return body then use special annotation @ResponseBody
. By default controller methods annotated with @RequestMapping
must return view name.
@RequestMapping(method = RequestMethod.GET)
@ResponseBody
public String getHello() {
return "hello world";
}
EDIT:
Add <mvc:annotation-driven />
to your sample-servlet.xml.
Replace root context configuration file:
<context-param>
<param-name>contextConfiguration</param-name>
<param-value>/WEB-INF/sample-servlet.xml</param-value>
</context-param>
to
<context-param>
<param-name>contextConfiguration</param-name>
<param-value>/WEB-INF/context.xml</param-value>
</context-param>
Move all spring configuration to root context /WEB-INF/context.xml
and leave /WEB-INF/sample-servlet.xml
empty:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
">
</beans>
Solution 2
I guess you are missing the context path of your project. If you are using eclipse you can find the context path in the project properties --> Web Project settings.
Also, make sure there no errors on your console.
Solution 3
I beleive you need to put the RequestMapping for your method with a /. Something like this:
@RequestMapping(value = "/", method = RequestMethod.GET)
public String getHello() {
return "hello world";
}
coder
Updated on June 04, 2022Comments
-
coder about 2 years
I'm using spring 3.2.3.RELEASE version and tomcat 7. I wanted to build a sample REST API using spring framework. My web.xml looks something like this:
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <context-param> <param-name>contextConfiguration</param-name> <param-value>/WEB-INF/sample-servlet.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <servlet> <servlet-name>sample-servlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>sample</servlet-name> <url-pattern>/myProjects</url-pattern> </servlet-mapping>
My sample-servlet.xml looks something like this:
<beans> <context:component-scan base-package="com.myprojects.sampleproject" /> </beans>
Basically I do not want any JSP files since I just want to return string(JSON string) from controller. My controller looks something like this:
package com.myprojects.sampleproject; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; /** * * @author author */ @Controller @RequestMapping("/user") public class sampleprojectController { @RequestMapping(method = RequestMethod.GET) public String getHello() { return "hello world"; } }
I was able to successfully create a war file and deploy it on tomcat. But when I hit localhost:8080/myProjects/user, I get 404. I'm using maven to build and deploy the package.
I've worked on JAX-RS before where in we could configure in netbeans to create a project to write web services and everything came in handy that time. But here I'm actually trying to convert a maven based project to a web application running on tomcat. To do that, I tried this sample and I'm unable to get that up and running. Am I missing something?
-
coder almost 11 yearsI use netbeans and in project properties I dont see web project settings. Actually I had written a stand alone maven based application initially. Now I'm trying to add a wrapper to this to create REST web services. I'm not sure what I'm missing here.
-
coder almost 11 yearslogs will be available on TOMCAT_HOME/logs right? I dont see any logging happening when I hit the page. Is there any other location where I can check the logs?
-
coder almost 11 yearsI got the issue now. When I was deploying the war file to tomcat7, I'm getting this error: IOException parsing XML document from ServletContext resource [/WEB-INF/applicationContext.xml]; nested exception is java.io.FileNotFoundException: Could not open ServletContext resource [/WEB-INF/applicationContext.xml]. What exactly should be there in applicationContext.xml. I was assuming the sample-servlet.xml and web xml will suffice. Could you please explain what needs to be added in this xml?
-
coder almost 11 yearsI fixed this issue by adding applicationContext.xml. When deploying to tomcat I even got: Mapped "{[/user],methods=[GET],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public java.lang.String com.myprojects.sampleproject.sampleprojectController.getHello(). But when I hit localhost:8080/myProjects/user, I get resource not available. In web.xml, url-pattern is /myProjects and in controller, the RequestMapping is /user. Hence Im assuming /myProjects/user is the url. Please correct me if I'm wrong.
-
seralex.vi almost 11 years@coder: maybe replace servlet mapping /myProjects to /myProjects/*
-
coder almost 11 yearsFinally got the issue. Netbeans had set some default context path: /<myprojectname>. So my url is /<myProjectname>/myProjects/user. I guess this is set in META-INF/context.xml.