从java spring方法管理http.get响应

时间:2016-05-05 15:32:59

标签: javascript angularjs spring spring-mvc get

我必须从java spring get request管理http get响应。 从服务器下载的文件是一个zip(里面是.pdf和.png)。 这是java代码:

@RequestMapping(value="/data/{meetingId}", method = RequestMethod.GET)
public byte[] getMeetingRest(@PathVariable(value="meetingId") String meetingId) {
    byte[] file = new byte[1024];
    Buffer buf =null;
    try {

        String url = getContentServer()+"/data?username="+getUsername()+"&password="+getPassword()+"&id="+meetingId;
        RestTemplate templ = new RestTemplate();
        file = templ.getForObject(url, byte[].class);

    } catch (Exception e) {
        String msg = "Error retrieving meeting list";
        logger.error(msg, e);
    }
    logger.info("file zip  --> "+file.length);
    return file;
}

这是agular http.get:

 appControllers.controller('FolderListController', ['$scope', '$routeParams', '$http', '$timeout', '$q', 'global',
                                                   function($scope, $routeParams, $http, $timeout, $q, global){
    console.log("FolderListController");
    var meetingId = $routeParams.meetingId;

    $http.get(CONTEXT_PATH+"/services/data/"+meetingId, {responseType: 'arraybuffer'})
    .success(function(data){
        console.log(data.length);
        console.log(data);
    })
    .error(function(e){
        console.log("Errore chiamate rest data zip   e = "+e);
    });

    //visualizzo i corretti items.
    for(var i=0;i<global.meetings.length;i++){
        console.log("meetingList["+i+"] = "+global.meetings[i]);
        if(global.meetings[i].id == meetingId){
            console.log("trovato id :  id="+global.meetings[i].id+"  --  meetingId  = "+meetingId);
            $scope.items = global.meetings[i];
            break;
        }
    }
}]);

这是输出:

  

undefined controllers.js:108 ArrayBuffer {}。

有些建议吗?

感谢。

1 个答案:

答案 0 :(得分:0)

尝试添加@ResponseBody注释或将@Controller注释替换为@RestController。

@Controller
....
@RequestMapping(value="/data/{meetingId}", method = RequestMethod.GET)
public @ResponseBody byte[] getMeetingRest(@PathVariable String meetingId) {
    ...
}

或:

@RestController
...
@GetMapping("/data/{meetingId}")
public byte[] getMeetingRest(@PathVariable String meetingId) {
    ...
}