【发布时间】:2016-06-02 03:54:07
【问题描述】:
我正在从我的 Angular UI 调用 Spring Boot REST 服务。只要 Spring Boot Rest 服务作为 Spring Boot 应用程序执行,它就可以正常工作。但是一旦我将它转换为 WAR 文件并部署在 Jboss 6.2.4 服务器上,我得到 404。我看到来自 UI 的 REST 服务调用成功,但请求 JSON 没有通过。在请求 JSON 时,我传递了 2 个字符串和一个上传的 excel 文件。
这是我的 Angular UI http 调用
App.service('getHeatMapDataService', ['$http', '$q', function ($http, $q) {
this.getHeatMapData = function (scope) {
var url = 'http://localhost:8080/rest-services/fusiontables/upload';
var deferred = $q.defer();
$http({
method: 'POST',
url: url,
headers: {
'Content-Type': undefined
},
data: {
stateCd: scope.stateCd,
addressExtras: scope.addressExtras,
uploadFile: scope.upFile
},
transformRequest: function (data, headersGetter) {
var formData = new FormData();
angular.forEach(data, function (value, key) {
formData.append(key, value);
});
var headers = headersGetter();
delete headers['Content-Type'];
return formData;
}
})
.success(function (data) {
deferred.resolve(data);
console.log("Success");
console.log(data);
})
.error(function (data, status) {
deferred.reject(status);
console.log("Failed");
});
return deferred.promise;
}
}]);
这是我工作时的 Spring Boot Rest 控制器
@RequestMapping(value="/upload", method=RequestMethod.POST)
@ResponseBody
public String getBoundaries(HeatMapUploadCommand uploadCommand) {
logger.info("Heat Map Controller invoked " + uploadCommand);
return null;
}
这是我的上传命令
public class HeatMapUploadCommand {
private String stateCd;
private String addressExtras;
private MultipartFile uploadFile;
我在 Jboss 上部署后,请求仍然会到达 Spring Boot 应用程序,但随后它的所有请求参数都为空。
这是请求负载
------WebKitFormBoundaryvCCnl3nhIgoW1MwR
Content-Disposition: form-data; name="stateCd"
CA
------WebKitFormBoundaryvCCnl3nhIgoW1MwR
Content-Disposition: form-data; name="addressExtras"
1234
------WebKitFormBoundaryvCCnl3nhIgoW1MwR
Content-Disposition: form-data; name="uploadFile"; filename="CAdata.xlsx"
Content-Type: application/vnd.openxmlformats- officedocument.spreadsheetml.sheet
------WebKitFormBoundaryvCCnl3nhIgoW1MwR--
我尝试将控制器更改为
@RequestMapping(value="/upload", method=RequestMethod.POST)
@ResponseBody
public String getBoundaries(@RequestParam(value="stateCd") String stateCd,
@RequestParam(value="addressExtras") String addressExtras,
@RequestParam(value="uploadFile") MultipartFile file) {
System.out.println("Heat Map Controller invoked " + stateCd);
return null;
}
仍然没有运气。这是我得到的回应。
{"timestamp":1464840821648,"status":400,"error":"Bad Request","exception":"org.springframework.web.bind.MissingServletRequestParameterException","message":"Required String parameter 'stateCd' is not present","path":"/rest-services/fusiontables/upload"}
【问题讨论】:
标签: angularjs jboss spring-boot angular-http