Spring Rest Controller POST请求不起作用

时间:2016-06-06 00:48:00

标签: java angularjs spring rest spring-mvc

我有一个休息控制器:

@RestController
@RequestMapping("/query")
public class QueryController {

    @Autowired
    private QueryService queryService;

    @RequestMapping(value = "/select", method = RequestMethod.POST)
    public @ResponseBody QueryResultDTO executeQuery(@RequestBody QueryDTO queryDTO) {
        try {
            QueryResultDTO queryResultDTO = queryService.executeQuery("select * from employees");
            queryResultDTO.setSuccessful(true);
            return queryResultDTO;
        } catch (SQLException e) {
            QueryResultDTO queryResultDTO = new QueryResultDTO();
            queryResultDTO.setSuccessful(false);
            queryResultDTO.setErrorMessage(e.getMessage());
            return queryResultDTO;
        }
    }
}

我尝试从AngularJS控制器发送POST请求:

app.controller("AppCtrl",function($scope,$http) {
    var app = this;

    $scope.execute= function () {
        $http({
            url: '../query/select',
            method: "POST",
            data: { 'message' : $scope.queryText }
        })
            .then(function(response) {
                    $scope.queryResult = response.data;
                    console.log($scope.queryResult);
                    console.log($scope.queryText)
                },
                function(response) {
                    console.log(response);
                });
    }

});

但它不起作用。我甚至没有调用Spring Controller中的executeQuery函数。

但是当我将RequestMethod更改为GET时,它可以正常工作。

@RestController
@RequestMapping("/query")
public class QueryController {

    @Autowired
    private QueryService queryService;

    @RequestMapping(value = "/select", method = RequestMethod.GET)
    public @ResponseBody QueryResultDTO executeQuery() {
        try {
            QueryResultDTO queryResultDTO = queryService.executeQuery("INSERT INTO employee VALUES (7,'dupa')");
            queryResultDTO.setSuccessful(true);
            return queryResultDTO;
        } catch (SQLException e) {
            QueryResultDTO queryResultDTO = new QueryResultDTO();
            queryResultDTO.setSuccessful(false);
            queryResultDTO.setErrorMessage(e.getMessage());
            return queryResultDTO;
        }
    }
}

并在Angular控制器中:

app.controller("AppCtrl",function($scope,$http) {
    var app = this;

    $scope.execute= function () {
        $http({
            url: '../query/select',
            method: "GET",
            data: { 'message' : $scope.queryText }
        })
            .then(function(response) {
                    $scope.queryResult = response.data;
                    console.log($scope.queryResult);
                    console.log($scope.queryText)
                },
                function(response) {
                    console.log(response);
                });
    }

});

我的主要问题是我想将一些数据发送到我的Spring控制器,然后发送JSON以响应我的Angular控制器。 Whith GET方法响应完美无缺,但当我使用POST时,甚至不调用控制器方法。

编辑:

我的QueryDTO课很简单:

public class QueryDTO {

    private String message;

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }
}

一些DEBUG级别的日志:

2016-06-06 09:28:23.697 DEBUG 7504 --- [nio-8090-exec-2] o.s.web.servlet.DispatcherServlet        : Null ModelAndView returned to DispatcherServlet with name 'dispatcherServlet': assuming HandlerAdapter completed request handling
2016-06-06 09:28:23.698 DEBUG 7504 --- [nio-8090-exec-2] o.s.web.servlet.DispatcherServlet        : Successfully completed request

2 个答案:

答案 0 :(得分:0)

尝试在方法中添加consumes=MediaType.APPLICATION_JSON_VALUE

@Transactional
@RequestMapping(value = "/userlogincheck", method = RequestMethod.POST,   consumes=MediaType.APPLICATION_JSON_VALUE)
public @ResponseBody void userLoginCheck(@RequestBody UserImpl user, HttpServletRequest request, HttpServletResponse response) throws JSONException, IOException {
JSONObject json = new JSONObject();
    try {
        String email=user.getEmail();
        Long userId=user.getId();
        User loginData = accountService.userLoginCheck(email,userId);
        if(loginData==null)
        {
            json.put("status", "FAILURE");
            json.put("message", "user does not exist");
            json.put("nextPage", "signIn");
        }
        else
        {
            json.put("status", "SUCCESS");
            json.put("nextPage", updateState);
        }
    }
    catch(Exception e) {
        logger.info(e.getMessage());
    }
    response.setContentType("application/json;charset=UTF-8");
    logger.info("response======" + json.toString());
    PrintWriter out = response.getWriter();
    out.write(json.toString());
}

答案 1 :(得分:0)

我遇到了同样的问题,并且能够通过向我的请求添加CSRF令牌来解决这个问题(如果您使用WebSecurity,这只是一个问题)。 https://docs.spring.io/spring-security/site/docs/current/reference/html/csrf.html

此链接描述了以下步骤:

1)将标记添加到标题中,使用thymeleaf,如下所示(我认为您也可以从cookie中获取标记):

<head>
   <meta name="_csrf" th:content="${_csrf.token}"/>
.....
</head>

2)更改您的请求以包含CSRF令牌,如下所示(我不熟悉angular,但我想您可以像使用Jquery一样设置标题):

var token = $("meta[name='_csrf']").attr("content");
  $.ajax({
          type: 'POST',
          url: url,
          data: JSON.stringify(newTodo),
          headers: {
             'X-CSRF-TOKEN': token
          },
          contentType: 'application/json',
          dataType: 'json',
          success: function(){
            alert('callback ');
         }
      });