DTO for Spring和AngularJS

时间:2014-10-20 10:22:51

标签: angularjs spring controller dto

我想从AngularJS处理请求。所以我有DTO级:

public class LoginDTO {
    private String login;
    private RoleEnum role;

    //setters and getters
}

后端控制器:

@Controller
@RequestMapping("/EmployeeService/user")
public class UserController {

    @RequestMapping(method = RequestMethod.GET, value = "/userInfo")
    @ResponseBody
    public LoginDTO currentUserInfo(){
        LoginDTO loginDTO = new LoginDTO(RoleEnum.ROLE_ADMIN, "test");
        return loginDTO;
    }
}

前端的控制器:

app.controller("test", function($scope, $http){
    var response = $http({
        method: "get",
        url: "/EmployeeService/user/userInfo"
    });
    response.success(function (data) {
        alert("ok");
    });
    response.error(function(data){
        alert("failed");
    });
});

在前端,我看到failed。如果我在后端更改控制器:

@Controller
@RequestMapping("/EmployeeService/user")
public class UserController {

    @RequestMapping(method = RequestMethod.GET, value = "/userInfo")
    @ResponseBody
    public String currentUserInfo(){
        return "test";
    }
}

我看到OK。如何使用DTO课程?

关于争斗错误the server responded with a status of 406 (Not Acceptable)

The resource identified by this request is only capable of generating responses with 
characteristics not acceptable according to the request "accept" headers.

1 个答案:

答案 0 :(得分:0)

让我们添加:

app.controller("test", function($scope, $http){
    var response = $http({
        method: "get",
        url: "/EmployeeService/user/userInfo",
<!--ADD--!>
        dataType: 'json',
        contentType: 'application/json',
        mimeType: 'application/json'
<!--END--!>
    });
    response.success(function (data) {
        alert("ok");
    });
    response.error(function(data){
        alert("failed");
    });
});