在 Spring 中调用 rest api 时获取 404
Getting 404 while calling rest api developed in Spring
控制器
package com.example;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/ws")
public class Controller {
@RequestMapping(value="/req", method = RequestMethod.GET,produces="application/json")
TestBean readBookmarks() {
TestBean bean = new TestBean("Test", 25);
return bean;
}
}
Web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:web="http://xmlns.jcp.org/xml/ns/javaee"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd
http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" id="WebApp_ID" version="2.4">
<display-name>Spring MVC Application</display-name>
<servlet>
<servlet-name>App</servlet-name>
<servlet-class> org.springframework.web.servlet.DispatcherServlet</servlet-class
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>App</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
应用-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"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-
context.xsd">
<context:component-scan base-package="com.example"/>
</beans>
当我尝试在浏览器上点击 http://localhost:8080/ws/req 时,它显示 HTTP 状态 404。
URL 已按照日志中的观察进行注册:
Jun 07, 2017 10:35:39 AM org.apache.tomcat.util.digester.SetPropertiesRule begin
WARNING: [SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting property 'source' to 'org.eclipse.jst.j2ee.server:MathWebApp' did not find a matching property.
Jun 07, 2017 10:35:39 AM org.apache.catalina.startup.VersionLoggerListener log
INFO: Server version: Apache Tomcat/8.0.44
Jun 07, 2017 10:35:39 AM org.apache.catalina.startup.VersionLoggerListener log
INFO: Server built: May 10 2017 17:21:09 UTC
Jun 07, 2017 10:35:39 AM org.apache.catalina.startup.VersionLoggerListener log
INFO: Server number: 8.0.44.0
Jun 07, 2017 10:35:39 AM org.apache.catalina.startup.VersionLoggerListener log
INFO: OS Name: Mac OS X
Jun 07, 2017 10:35:39 AM org.apache.catalina.startup.VersionLoggerListener log
INFO: OS Version: 10.12.4
Jun 07, 2017 10:35:39 AM org.apache.catalina.startup.VersionLoggerListener log
INFO: Architecture: x86_64
Jun 07, 2017 10:35:39 AM org.apache.catalina.startup.VersionLoggerListener log
INFO: Java Home: /Library/Java/JavaVirtualMachines/jdk1.8.0_111.jdk/Contents/Home/jre
Jun 07, 2017 10:35:39 AM org.apache.catalina.startup.VersionLoggerListener log
INFO: JVM Version: 1.8.0_111-b14
Jun 07, 2017 10:35:39 AM org.apache.catalina.startup.VersionLoggerListener log
INFO: JVM Vendor: Oracle Corporation
Jun 07, 2017 10:35:39 AM org.apache.catalina.startup.VersionLoggerListener log
INFO: CATALINA_BASE: /Users/kegupta/Documents/Framework/.metadata/.plugins/org.eclipse.wst.server.core/tmp0
Jun 07, 2017 10:35:39 AM org.apache.catalina.startup.VersionLoggerListener log
INFO: CATALINA_HOME: /Users/kegupta/Desktop/apache-tomcat-8.0.44
Jun 07, 2017 10:35:39 AM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Dcatalina.base=/Users/kegupta/Documents/Framework/.metadata/.plugins/org.eclipse.wst.server.core/tmp0
Jun 07, 2017 10:35:39 AM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Dcatalina.home=/Users/kegupta/Desktop/apache-tomcat-8.0.44
Jun 07, 2017 10:35:39 AM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Dwtp.deploy=/Users/kegupta/Documents/Framework/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps
Jun 07, 2017 10:35:39 AM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Djava.endorsed.dirs=/Users/kegupta/Desktop/apache-tomcat-8.0.44/endorsed
Jun 07, 2017 10:35:39 AM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Dfile.encoding=UTF-8
Jun 07, 2017 10:35:39 AM org.apache.catalina.core.AprLifecycleListener lifecycleEvent
INFO: The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: /Users/kegupta/Library/Java/Extensions:/Library/Java/Extensions:/Network/Library/Java/Extensions:/System/Library/Java/Extensions:/usr/lib/java:.
Jun 07, 2017 10:35:39 AM org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["http-nio-8080"]
Jun 07, 2017 10:35:39 AM org.apache.tomcat.util.net.NioSelectorPool getSharedSelector
INFO: Using a shared selector for servlet write/read
Jun 07, 2017 10:35:39 AM org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["ajp-nio-8009"]
Jun 07, 2017 10:35:39 AM org.apache.tomcat.util.net.NioSelectorPool getSharedSelector
INFO: Using a shared selector for servlet write/read
Jun 07, 2017 10:35:39 AM org.apache.catalina.startup.Catalina load
INFO: Initialization processed in 480 ms
Jun 07, 2017 10:35:39 AM org.apache.catalina.core.StandardService startInternal
INFO: Starting service Catalina
Jun 07, 2017 10:35:39 AM org.apache.catalina.core.StandardEngine startInternal
INFO: Starting Servlet Engine: Apache Tomcat/8.0.44
Jun 07, 2017 10:35:40 AM org.apache.jasper.servlet.TldScanner scanJars
INFO: At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time.
Jun 07, 2017 10:35:40 AM org.apache.catalina.core.ApplicationContext log
INFO: No Spring WebApplicationInitializer types detected on classpath
Jun 07, 2017 10:35:40 AM org.apache.catalina.core.ApplicationContext log
INFO: Initializing Spring FrameworkServlet 'App'
Jun 07, 2017 10:35:40 AM org.springframework.web.servlet.DispatcherServlet initServletBean
INFO: FrameworkServlet 'App': initialization started
Jun 07, 2017 10:35:40 AM org.springframework.web.context.support.XmlWebApplicationContext prepareRefresh
INFO: Refreshing WebApplicationContext for namespace 'App-servlet': startup date [Wed Jun 07 10:35:40 IST 2017]; root of context hierarchy
Jun 07, 2017 10:35:40 AM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from ServletContext resource [/WEB-INF/App-servlet.xml]
Jun 07, 2017 10:35:40 AM org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping registerHandler
INFO: Mapped URL path [/ws/req] onto handler 'controller'
Jun 07, 2017 10:35:40 AM org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping registerHandler
INFO: Mapped URL path [/ws/req.*] onto handler 'controller'
Jun 07, 2017 10:35:40 AM org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping registerHandler
INFO: Mapped URL path [/ws/req/] onto handler 'controller'
Jun 07, 2017 10:35:40 AM org.springframework.web.servlet.DispatcherServlet initServletBean
INFO: FrameworkServlet 'App': initialization completed in 617 ms
Jun 07, 2017 10:35:40 AM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["http-nio-8080"]
Jun 07, 2017 10:35:40 AM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["ajp-nio-8009"]
Jun 07, 2017 10:35:40 AM org.apache.catalina.startup.Catalina start
INFO: Server startup in 1514 ms
我卡了很久。任何帮助将不胜感激。
从日志中我可以看到正在部署一个名为 war 的应用程序。尝试使用这个 url :
http://localhost:8080/App/ws/req
Or
http://localhost:8080/<deployed tomcat war name>/ws/req
TestBean readBookmarks() {
TestBean bean = new TestBean("Test", 25);
return bean;
这个函数似乎缺少@ResponseBody 并且 TestBean(可能是自定义 class)不是可以被 Spring 序列化的东西。您能否尝试发送 String 而不是 TestBean —— 应该可以。
此外,部署目录可能与您的 web.xml 配置不同(即 App 以外的目录)。您可以尝试加载 http://localhost:8080/{Dir}/ws/req where Dir == deployment directory (!= 'App').
以上两点应该可以解决您的问题。
控制器
package com.example;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/ws")
public class Controller {
@RequestMapping(value="/req", method = RequestMethod.GET,produces="application/json")
TestBean readBookmarks() {
TestBean bean = new TestBean("Test", 25);
return bean;
}
}
Web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:web="http://xmlns.jcp.org/xml/ns/javaee"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd
http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" id="WebApp_ID" version="2.4">
<display-name>Spring MVC Application</display-name>
<servlet>
<servlet-name>App</servlet-name>
<servlet-class> org.springframework.web.servlet.DispatcherServlet</servlet-class
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>App</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
应用-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"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-
context.xsd">
<context:component-scan base-package="com.example"/>
</beans>
当我尝试在浏览器上点击 http://localhost:8080/ws/req 时,它显示 HTTP 状态 404。
URL 已按照日志中的观察进行注册:
Jun 07, 2017 10:35:39 AM org.apache.tomcat.util.digester.SetPropertiesRule begin
WARNING: [SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting property 'source' to 'org.eclipse.jst.j2ee.server:MathWebApp' did not find a matching property.
Jun 07, 2017 10:35:39 AM org.apache.catalina.startup.VersionLoggerListener log
INFO: Server version: Apache Tomcat/8.0.44
Jun 07, 2017 10:35:39 AM org.apache.catalina.startup.VersionLoggerListener log
INFO: Server built: May 10 2017 17:21:09 UTC
Jun 07, 2017 10:35:39 AM org.apache.catalina.startup.VersionLoggerListener log
INFO: Server number: 8.0.44.0
Jun 07, 2017 10:35:39 AM org.apache.catalina.startup.VersionLoggerListener log
INFO: OS Name: Mac OS X
Jun 07, 2017 10:35:39 AM org.apache.catalina.startup.VersionLoggerListener log
INFO: OS Version: 10.12.4
Jun 07, 2017 10:35:39 AM org.apache.catalina.startup.VersionLoggerListener log
INFO: Architecture: x86_64
Jun 07, 2017 10:35:39 AM org.apache.catalina.startup.VersionLoggerListener log
INFO: Java Home: /Library/Java/JavaVirtualMachines/jdk1.8.0_111.jdk/Contents/Home/jre
Jun 07, 2017 10:35:39 AM org.apache.catalina.startup.VersionLoggerListener log
INFO: JVM Version: 1.8.0_111-b14
Jun 07, 2017 10:35:39 AM org.apache.catalina.startup.VersionLoggerListener log
INFO: JVM Vendor: Oracle Corporation
Jun 07, 2017 10:35:39 AM org.apache.catalina.startup.VersionLoggerListener log
INFO: CATALINA_BASE: /Users/kegupta/Documents/Framework/.metadata/.plugins/org.eclipse.wst.server.core/tmp0
Jun 07, 2017 10:35:39 AM org.apache.catalina.startup.VersionLoggerListener log
INFO: CATALINA_HOME: /Users/kegupta/Desktop/apache-tomcat-8.0.44
Jun 07, 2017 10:35:39 AM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Dcatalina.base=/Users/kegupta/Documents/Framework/.metadata/.plugins/org.eclipse.wst.server.core/tmp0
Jun 07, 2017 10:35:39 AM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Dcatalina.home=/Users/kegupta/Desktop/apache-tomcat-8.0.44
Jun 07, 2017 10:35:39 AM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Dwtp.deploy=/Users/kegupta/Documents/Framework/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps
Jun 07, 2017 10:35:39 AM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Djava.endorsed.dirs=/Users/kegupta/Desktop/apache-tomcat-8.0.44/endorsed
Jun 07, 2017 10:35:39 AM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Dfile.encoding=UTF-8
Jun 07, 2017 10:35:39 AM org.apache.catalina.core.AprLifecycleListener lifecycleEvent
INFO: The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: /Users/kegupta/Library/Java/Extensions:/Library/Java/Extensions:/Network/Library/Java/Extensions:/System/Library/Java/Extensions:/usr/lib/java:.
Jun 07, 2017 10:35:39 AM org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["http-nio-8080"]
Jun 07, 2017 10:35:39 AM org.apache.tomcat.util.net.NioSelectorPool getSharedSelector
INFO: Using a shared selector for servlet write/read
Jun 07, 2017 10:35:39 AM org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["ajp-nio-8009"]
Jun 07, 2017 10:35:39 AM org.apache.tomcat.util.net.NioSelectorPool getSharedSelector
INFO: Using a shared selector for servlet write/read
Jun 07, 2017 10:35:39 AM org.apache.catalina.startup.Catalina load
INFO: Initialization processed in 480 ms
Jun 07, 2017 10:35:39 AM org.apache.catalina.core.StandardService startInternal
INFO: Starting service Catalina
Jun 07, 2017 10:35:39 AM org.apache.catalina.core.StandardEngine startInternal
INFO: Starting Servlet Engine: Apache Tomcat/8.0.44
Jun 07, 2017 10:35:40 AM org.apache.jasper.servlet.TldScanner scanJars
INFO: At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time.
Jun 07, 2017 10:35:40 AM org.apache.catalina.core.ApplicationContext log
INFO: No Spring WebApplicationInitializer types detected on classpath
Jun 07, 2017 10:35:40 AM org.apache.catalina.core.ApplicationContext log
INFO: Initializing Spring FrameworkServlet 'App'
Jun 07, 2017 10:35:40 AM org.springframework.web.servlet.DispatcherServlet initServletBean
INFO: FrameworkServlet 'App': initialization started
Jun 07, 2017 10:35:40 AM org.springframework.web.context.support.XmlWebApplicationContext prepareRefresh
INFO: Refreshing WebApplicationContext for namespace 'App-servlet': startup date [Wed Jun 07 10:35:40 IST 2017]; root of context hierarchy
Jun 07, 2017 10:35:40 AM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from ServletContext resource [/WEB-INF/App-servlet.xml]
Jun 07, 2017 10:35:40 AM org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping registerHandler
INFO: Mapped URL path [/ws/req] onto handler 'controller'
Jun 07, 2017 10:35:40 AM org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping registerHandler
INFO: Mapped URL path [/ws/req.*] onto handler 'controller'
Jun 07, 2017 10:35:40 AM org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping registerHandler
INFO: Mapped URL path [/ws/req/] onto handler 'controller'
Jun 07, 2017 10:35:40 AM org.springframework.web.servlet.DispatcherServlet initServletBean
INFO: FrameworkServlet 'App': initialization completed in 617 ms
Jun 07, 2017 10:35:40 AM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["http-nio-8080"]
Jun 07, 2017 10:35:40 AM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["ajp-nio-8009"]
Jun 07, 2017 10:35:40 AM org.apache.catalina.startup.Catalina start
INFO: Server startup in 1514 ms
我卡了很久。任何帮助将不胜感激。
从日志中我可以看到正在部署一个名为 war 的应用程序。尝试使用这个 url :
http://localhost:8080/App/ws/req
Or
http://localhost:8080/<deployed tomcat war name>/ws/req
TestBean readBookmarks() {
TestBean bean = new TestBean("Test", 25);
return bean;
这个函数似乎缺少@ResponseBody 并且 TestBean(可能是自定义 class)不是可以被 Spring 序列化的东西。您能否尝试发送 String 而不是 TestBean —— 应该可以。
此外,部署目录可能与您的 web.xml 配置不同(即 App 以外的目录)。您可以尝试加载 http://localhost:8080/{Dir}/ws/req where Dir == deployment directory (!= 'App').
以上两点应该可以解决您的问题。