使用Spring Restful Web服务时获取org.springframework.web.servlet.PageNotFound.noHandlerFound错误

时间:2017-02-01 04:54:07

标签: java angularjs spring

我正在使用spring restful webservice和angular JS。我试图连接我的后端代码,但无法这样做。我无法弄清楚到底哪里出错了。

这是我的角度代码:

app.constant('REST_URI', 'http://localhost:8181/rest/');

app.factory('FeedbackService',['$http', 'REST_URI', function ($http, REST_URI) {

    const addFeedbackAPI = REST_URI + 'feedback/addFeedback';

    var addFeedback = function(feedbackData){
        return $http.post(addFeedbackAPI, feedbackData);
    };

   return {
       addFeedback : addFeedback
   };

}]);

app.controller('feedbackController', ['$scope', 'FeedbackService', function ($scope, FeedbackService) {

    $scope.feedbackData = {};

    $scope.addFeedback = function () {

        $scope.feedbackData.registerDate = new Date();

        FeedbackService.addFeedback($scope.feedbackData).then(
            function (successResponse) {
                $scope.feedbackResponse = successResponse;
            },
            function (errorResponse) {
                $scope.feedbackResponse = errorResponse;
            }
        );
    };
}]);

的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">


    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/config/rootApplicationContext.xml</param-value>
    </context-param>
    <listener>
        <listener-class> org.springframework.web.context.ContextLoaderListener </listener-class>
    </listener>

    <servlet>
        <servlet-name>springrest</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/config/webApplicationContext.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>springrest</servlet-name>
        <url-pattern>/rest/*</url-pattern>
    </servlet-mapping>

</web-app>

控制器

@RestController(value = "/rest/feedback/")
public class FeedbackController {

    @Autowired
    private FeedbackService feedbackService;

    @RequestMapping(value = "addFeedback", method = RequestMethod.POST)
    public long addFeedback(@RequestBody Feedback feedback) {
        return feedbackService.addFeedback(feedback);
    }
}

但我在日志中收到404错误:

console:

http://localhost:8181/rest/feedback/addFeedback 404 ()

服务器:     01-Feb-2017 10:05:49.339警告[http-nio-8181-exec-9] org.springframework.web.servlet.PageNotFound.noHandlerFound找不到带有URI [/ rest / feedback / addFeedback]的HTTP请求的映射DispatcherServlet,名称为“springrest”

1 个答案:

答案 0 :(得分:0)

使用@RestController(value = "/feedback/")

而不是@RestController(value = "/rest/feedback/")

因为你已经在/rest/*中使用了web.xml,所以你不需要在java控制器中给出它。