angularjs $ http.get和java api调用

时间:2017-05-03 03:59:52

标签: javascript java angularjs rest

我正在尝试使用参数创建一个angularjs $ http.get请求但由于syntaxt可能无法正常工作。请你 确认我做错了,也许在angularjs调用或java api方法中语法错误

$http.get(document.requestPathPrefix + "/home/my-api",
   $scope.requestParametersObject
   ).then(
        function(response) {
           $scope.output = response.data;
        },
        function(response) {
           $scope.retrieveErrorMssg = "Failed to retrieve required data.";
   });

我的参数与此图像类似

parameter object for api call

在这样的java api调用中

@RequestMapping(value = "/my-api", method = RequestMethod.GET)
public ResponseEntity<Collection<MyObjectResponse>> getMyObjResponse(@RequestBody final MyObjectRequest request)
{
    Map<Integer,MyObjectResponse> oResponse = new HashMap<>();
    try {

        //Manipulation and db calls

    } catch (Exception e) {
        log.error(e.getMessage(), e);
    }
    return new ResponseEntity<Collection<MyObjectResponse>>(oResponse.values(), HttpStatus.OK);
}

2 个答案:

答案 0 :(得分:1)

试试,

.catch()

答案 1 :(得分:0)

如果您担心语法,请查看此简单示例并根据需要进行修改。

在JS中,您的http.get调用应包含URL及其参数(如果有)

$http.get('getUserInfo',
{
   params : {
          'userEmail' : 'userEmail'
   }
}).then(function(response) {
        console.log(response);
});

如果要将参数传递给API调用,请确保Java控制器或服务已定义参数以接收该参数。理想情况下,参数应在呼叫和提供商之间匹配

例如,对于上面的API调用,弹簧控制器应该如下所示,

@RequestMapping(value = "/getUserInfo", method = RequestMethod.GET)
public @ResponseBody User getUserInfo(
        @RequestParam("userEmail") String userEmail) {
// Do Something
}