AngularJs控制器获取请求没有达到Spring MVC Rest Service

时间:2014-11-17 23:11:31

标签: javascript angularjs spring rest

所以我的角度javascript为

    var app = angular.module('app', []);
    app.controller('controller', function($scope, $http) {
        $http.get('http://localhost:8080/core/students.json')
                .success(function(data) {
                    $scope.user = data;
                });
    });

和我的休息控制器

@RestController
public class StudentRestController {

@RequestMapping(value = "/students", produces = { "application/json" }, method =      RequestMethod.GET)
@ResponseStatus(HttpStatus.OK)
public Student getStudent() {
    // return studentService.getStudentByUserName(userName);
    Student s = new Student();
    s.setUserName("userName");
    s.setEmailAddress("email");
    return s;
}
}

但由于某种原因,javascript ajax请求没有达到方法getStudent()。为什么是这样?我收到了控制台错误

"GET http://localhost:8080/core/students.json 404 (Not Found)"

普通按钮网址调用按预期工作

2 个答案:

答案 0 :(得分:1)

将angularjs app更改为

var app = angular.module('app', []);
app.controller('controller', [ "$scope", "$http", function($scope, $http) {
    $http.get("http://localhost:8080/students").success(function(data) {
        $scope.user = data;
    });
} ]);

如果你的web.xml

,则成立
<servlet-mapping>
        <servlet-name> dispatcherServlet </servlet-name>
        <url-pattern> * </url-pattern>
    </servlet-mapping>

答案 1 :(得分:0)

您正在定义&#34; /学生&#34;在@RequestMapping上,您的网址应为&#34; ... / core / students&#34;。