RequestMapping for Spring API无法正确映射

时间:2016-03-31 04:12:11

标签: java spring web-services rest

我正在尝试在eclipse中创建一个简单的web项目,在启动tomcat后我可以导航到一个路径(在@RequestMapping中指定)并做一些工作。我已经验证maven构建正确编译我的源文件但是在我启动Tomcat之后,当我导航到指定路径时,我得到404。我在下面包含了我的控制器类以及我的WEB-INF /目录中的applicationContext和dispatcher-servlet。任何人都能说出映射的问题是什么吗?尝试导航到/ test

的所有变体时,我得到404
@Controller
public class IdGenerator {
@Autowired
private InpoweredIdDAO dao;

@RequestMapping(value = "/getId.json",  method = RequestMethod.GET)
@ResponseBody
public InpoweredId getId(@RequestParam String ipAddress, @RequestParam long ts,@RequestParam String agent, 
        @RequestParam String referrer) throws Exception {
    String inpoweredId = InpoweredId.getInpoweredIdFromIp(ipAddress);

    return dao.getImpoweredIdByIpMask(inpoweredId);
}

@RequestMapping(value="/test", method = RequestMethod.GET) 
public void testTomcat() {
    System.out.println("Connected to tomcat");
}

application-context.xml

  <beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    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-4.2.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-4.2.xsd">

    <context:component-scan base-package="inpowered.generator" />

    </beans>

调度-servlet.xml中

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    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-4.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.2.xsd">

    <context:component-scan base-package="inpowered.generator" />

</beans>

编辑: 这是我的web.xml,这是非常基本的,也许这是问题?我没有得到任何堆栈跟踪我只是得到404

<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
  <display-name>Archetype Created Web Application</display-name>
</web-app>

1 个答案:

答案 0 :(得分:0)

在web.xml中添加DispatcherServlet

<servlet> 
<servlet-name>Spring MVC Dispatcher Servlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/config/web-application-config.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>

因此春天可以收听您正在访问的网址