在调用Spring RestController上获得404

时间:2018-09-27 06:06:49

标签: java spring rest

我有一个包含两个模块的mvn项目:

  • 服务器:提供多个REST端点的spring应用程序。
  • 客户:angularJS。

构建war.file,将npm生成的所有资源复制到服务器部分的WebContent文件夹中。在tomcat中手动安装war文件无效。该应用程序的安装没有任何例外,但是当我调用其中的一个休息服务时得到了404。 AngularJs启动良好。

WEB-INF / web

中声明使用Spring
    <servlet>
         <servlet-name>dispatcher</servlet-name>
         <servlet-class>
             org.springframework.web.servlet.DispatcherServlet
         </servlet-class>
         <init-param>
             <param-name>contextConfigLocation</param-name>
             <param-value>classpath*:spring.xml</param-value>
         </init-param>
         <load-on-startup>1</load-on-startup>
    </servlet>

     <servlet-mapping>
         <servlet-name>dispatcher</servlet-name>
         <url-pattern>/pmt/*</url-pattern>
     </servlet-mapping>

spring.xml配置文件:

<mvc:annotation-driven />
<context:component-scan base-package="de.pmt.server" />

<!--Services-->
<bean name="releaseService" 
    class="de.pmt.server.release.ReleaseService" p:releaseDAO-ref="releaseDAO"/>
<bean name="estimateService"
    class="de.pmt.server.strategy.EstimateService" />
<bean name="holidayService" 
    class="de.pmt.server.holiday.HolidayService" 
    p:restTemplate-ref="restTemplate" p:holidayDAO-ref="holidayDAO"/>

<!--RestController-->
<bean name="employeeController" 
    class="de.pmt.server.rest.controller.EmployeeController" 
    p:employeeDAO-ref="employeeDAO" />
<bean name="holidayController" 
    class="de.pmt.server.rest.controller.HolidayController" 
    p:holidayService-ref="holidayService"/>
<bean name="vacationController" 
    class="de.pmt.server.rest.controller.VacationController" 
    p:employeeDAO-ref="employeeDAO" p:vacationDAO-ref="vacationDAO" />
<bean name="releaseController" 
    class="de.pmt.server.rest.controller.ReleaseController" 
    p:releaseService-ref="releaseService"/>
<bean name="strategyController" 
    class="de.pmt.server.rest.controller.StrategyController" 
    p:strategyDAO-ref="strategyDAO" />

以EmployeeController为例-

package de.pmt.server.rest.controller;

@RestController
@RequestMapping("/employee")
public class EmployeeController {

    GenericDAO<Employee> employeeDAO;

    @RequestMapping("/getAll")
    @ResponseBody
    public List<Employee> getAllEmployee() {
        return employeeDAO.getAll();
    }

}

我打电话时得到404 本地主机:8080 / pmt / employee / getAll

2 个答案:

答案 0 :(得分:0)

您应该定义控制器使用的HTTP方法类型(GET,PUT,POST,DELETE)

@RequestMapping("/getAll", method = RequestMethod.GET)
@ResponseBody
public List<Employee> getAllEmployee() {
    return employeeDAO.getAll();
}

答案 1 :(得分:0)

从您的休息控制器中删除 @ResponseBody

@RequestMapping("/getAll" , method = RequestMethod.GET)   
    public List<Employee> getAllEmployee() {
        return employeeDAO.getAll();
    }