Java 获取 404 请求的资源在 Spring Restful Web 服务中不可用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18045871/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Getting 404 requested resource not available in spring restful web services
提问by coder
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:
我正在使用 spring 3.2.3.RELEASE 版本和 tomcat 7。我想使用 spring 框架构建一个示例 REST API。我的 web.xml 看起来像这样:
<?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:
我的 sample-servlet.xml 看起来像这样:
<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:
基本上我不想要任何 JSP 文件,因为我只想从控制器返回字符串(JSON 字符串)。我的控制器看起来像这样:
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.
我能够成功创建一个war文件并将其部署在tomcat上。但是当我点击 localhost:8080/myProjects/user 时,我得到 404。我使用 maven 来构建和部署包。
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?
我之前在 JAX-RS 上工作过,我们可以在 netbeans 中进行配置以创建一个项目来编写 Web 服务,那时一切都派上了用场。但在这里,我实际上是在尝试将基于 maven 的项目转换为在 tomcat 上运行的 Web 应用程序。为此,我尝试了此示例,但无法启动并运行它。我错过了什么吗?
采纳答案by seralex.vi
If you want return body then use special annotation @ResponseBody
. By default controller methods annotated with @RequestMapping
must return view name.
如果您想返回正文,请使用特殊注释@ResponseBody
。默认情况下,带有注释的控制器方法@RequestMapping
必须返回视图名称。
@RequestMapping(method = RequestMethod.GET)
@ResponseBody
public String getHello() {
return "hello world";
}
EDIT:
编辑:
Add <mvc:annotation-driven />
to your sample-servlet.xml.
添加<mvc:annotation-driven />
到您的 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:
将所有 spring 配置移动到根上下文/WEB-INF/context.xml
并/WEB-INF/sample-servlet.xml
留空:
<?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>
回答by Juned Ahsan
I beleive you need to put the RequestMapping for your method with a /. Something like this:
我相信你需要用 / 为你的方法放置 RequestMapping。像这样的东西:
@RequestMapping(value = "/", method = RequestMethod.GET)
public String getHello() {
return "hello world";
}
回答by Raunak Agarwal
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.
我猜您缺少项目的上下文路径。如果您使用的是 eclipse,您可以在项目属性 --> Web 项目设置中找到上下文路径。
Also, make sure there no errors on your console.
另外,请确保您的控制台上没有错误。