我正在从我的有角度的UI调用spring boot REST服务。只要将Spring Boot Rest服务作为Spring Boot应用程序执行,它就可以很好地工作。但是,一旦将其转换为WAR文件并部署到Jboss 6.2.4服务器上,我就会得到404。我看到从UI进行的REST服务调用成功了,但是请求JSON没有被传递。在请求JSON上,我传递了2个字符串和一个上载的excel文件。
这是我的角度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应用程序,但是所有请求参数都为null。
这是请求有效负载
------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"}
终于想通了。
我必须添加一个Multipartresolver bean。
@Configuration
public class WebConfig extends WebMvcAutoConfiguration {
@Bean
public MultipartResolver multipartResolver() {
return new CommonsMultipartResolver();
}
}
这解决了问题。好像当我将其作为spring boot应用程序运行时,spring已经照顾好了它,但是当部署为WAR文件时,应配置此bean
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句