我有以下控制器
@RestController("/person")
public class PersonController {
@Autowired
PersonService personService;
@RequestMapping(value = "/list",
method = RequestMethod.GET,
produces = MediaType.APPLICATION_JSON_VALUE
)
public List<PersonNode> getPersons(){
return personService.getList();
}
}
现在春天neo4j配置:
<?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: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">
<!-- Scan the JavaConfig -->
<context:annotation-config/>
<context:component-scan base-package="server.infrastructure.repositories.neo4j.config" />
</beans>
和spring mvc rest config:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
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
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!--Used for Spring MVC configuration - must have-->
<mvc:annotation-driven />
<!-- This shows where to scan for rest controller -->
<context:component-scan base-package="server.api.rest.controller" />
</beans>
和web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
<display-name>Restful Web Application with Spring MVC 4.0.5x</display-name>
<!-- Root App Context -->
<!-- The definition of the Root Spring Context Container shared by all Servlets and Filters -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/ApplicationContext.xml</param-value>
</context-param>
<!-- Bootstrap the root application context.Creates the Spring Container shared by all Servlets and Filters -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- Web App Settings -->
<!-- Processes application requests -->
<servlet>
<servlet-name>MR.rest.api</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet </servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/SpringMVC/SpringMVC-RESTContext.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>MR.rest.api</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
</web-app>
然而,当我提到http://localhost:8080/rest/person/list
时,我一直得到:
[http-bio-8080-exec-2] WARN osweb.servlet.PageNotFound - 在名为&#39; MR.rest的DispatcherServlet中找不到带有URI [/ rest / person / list]的HTTP请求的映射。 API&#39;
使用我的Tomcat启动日志:
...
INFO: Initializing Spring root WebApplicationContext
01:50:55.703 [localhost-startStop-1] INFO o.s.web.context.ContextLoader - Root WebApplicationContext: initialization started
01:50:55.774 [localhost-startStop-1] INFO o.s.w.c.s.XmlWebApplicationContext - Refreshing Root WebApplicationContext: startup date [Wed Dec 16 01:50:55 CET 2015]; root of context hierarchy
01:50:55.805 [localhost-startStop-1] INFO o.s.b.f.xml.XmlBeanDefinitionReader - Loading XML bean definitions from class path resource [spring/ApplicationContext.xml]
01:50:55.879 [localhost-startStop-1] INFO o.s.b.f.xml.XmlBeanDefinitionReader - Loading XML bean definitions from class path resource [spring/Neo4j-DataSources.xml]
01:50:56.254 [localhost-startStop-1] INFO o.s.c.s.PostProcessorRegistrationDelegate$BeanPostProcessorChecker - Bean 'myNeo4jConfiguration' of type [class server.infrastructure.repositories.neo4j.config.MyNeo4jConfiguration$$EnhancerBySpringCGLIB$$7212f882] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
01:50:56.257 [localhost-startStop-1] INFO o.s.d.n.config.Neo4jConfiguration - Initialising PersistenceExceptionTranslationPostProcessor
01:50:56.683 [localhost-startStop-1] INFO o.n.o.m.info.ClassFileProcessor - Starting Post-processing phase
01:50:56.684 [localhost-startStop-1] INFO o.n.o.m.info.ClassFileProcessor - Building annotation class map
01:50:56.684 [localhost-startStop-1] INFO o.n.o.m.info.ClassFileProcessor - Building interface class map for 6 classes
01:50:56.684 [localhost-startStop-1] INFO o.n.o.m.info.ClassFileProcessor - Registering default type converters...
01:50:56.692 [localhost-startStop-1] INFO o.n.o.m.info.ClassFileProcessor - Post-processing complete
01:50:56.692 [localhost-startStop-1] INFO o.n.o.m.info.ClassFileProcessor - 6 classes loaded in 22 milliseconds
01:50:56.974 [localhost-startStop-1] INFO o.s.d.n.mapping.Neo4jMappingContext - Neo4jMappingContext initialisation completed
01:50:57.088 [localhost-startStop-1] INFO o.s.d.n.config.Neo4jConfiguration - Initialising PersistenceExceptionTranslator
01:50:57.092 [localhost-startStop-1] INFO o.s.d.n.config.Neo4jConfiguration - Initialising PersistenceExceptionTranslationInterceptor
01:50:57.095 [localhost-startStop-1] INFO o.s.d.n.config.Neo4jConfiguration - Initialising Neo4jTransactionManager
01:50:57.130 [localhost-startStop-1] INFO o.s.web.context.ContextLoader - Root WebApplicationContext: initialization completed in 1424 ms
01:50:57.161 [localhost-startStop-1] INFO o.s.web.servlet.DispatcherServlet - FrameworkServlet 'MR.rest.api': initialization started
gru 16, 2015 1:50:57 AM org.apache.catalina.core.ApplicationContext log
INFO: Initializing Spring FrameworkServlet 'MR.rest.api'
01:50:57.165 [localhost-startStop-1] INFO o.s.w.c.s.XmlWebApplicationContext - Refreshing WebApplicationContext for namespace 'MR.rest.api-servlet': startup date [Wed Dec 16 01:50:57 CET 2015]; parent: Root WebApplicationContext
01:50:57.166 [localhost-startStop-1] INFO o.s.b.f.xml.XmlBeanDefinitionReader - Loading XML bean definitions from ServletContext resource [/WEB-INF/SpringMVC/SpringMVC-RESTContext.xml]
01:50:57.399 [localhost-startStop-1] INFO o.s.w.s.m.m.a.RequestMappingHandlerMapping - Mapped "{[/list],methods=[GET],produces=[application/json]}" onto public java.util.List<server.infrastructure.persistence.neo4j.nodes.PersonNode> server.api.rest.controller.PersonController.getPersons()
01:50:57.626 [localhost-startStop-1] INFO o.s.w.s.m.m.a.RequestMappingHandlerAdapter - Looking for @ControllerAdvice: WebApplicationContext for namespace 'MR.rest.api-servlet': startup date [Wed Dec 16 01:50:57 CET 2015]; parent: Root WebApplicationContext
01:50:57.694 [localhost-startStop-1] INFO o.s.w.s.m.m.a.RequestMappingHandlerAdapter - Looking for @ControllerAdvice: WebApplicationContext for namespace 'MR.rest.api-servlet': startup date [Wed Dec 16 01:50:57 CET 2015]; parent: Root WebApplicationContext
01:50:57.758 [localhost-startStop-1] INFO o.s.w.s.h.BeanNameUrlHandlerMapping - Mapped URL path [/person] onto handler '/person'
01:50:57.826 [localhost-startStop-1] INFO o.s.web.servlet.DispatcherServlet - FrameworkServlet 'MR.rest.api': initialization completed in 665 ms
gru 16, 2015 1:50:57 AM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["http-bio-8080"]
但是,当我提到http://localhost:8080/rest/list
时,我会得到预期的人员名单。
我希望在课堂级别的人员使用http://localhost:8080/rest/person/list
时引用@RestController
和`list&#39;在方法层面。如何做到这一点?
答案 0 :(得分:1)
您应该将@RequestMapping添加到Controller类
@RestController
@RequestMapping("/person")
public class PersonController {
@Autowired
PersonService personService;
@RequestMapping(value = "/list", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
public List<PersonNode> getPersons(){
return personService.getList();
}
}