【发布时间】:2021-12-28 20:13:05
【问题描述】:
我的一个项目遇到了问题。这是场景。
我有 Angular Springboot 项目,从一个 HTML 页面,数据必须发送到 Springboot 控制器 (restController) 并进行处理。 当我将单个字符串作为输入发送到端点时,它可以工作,但是当我必须发送 JSON 时,它就不起作用了。
这里是示例代码。
AnnotationController.js
$scope.postExample = function() {
var annotationjson = {
acctNo: $scope.tradeAnnotationDto.acctNo,
tradeComment: $scope.tradeAnnotationDto.tradeComment
};
AnnotationService.postExample(annotationjson).then(function() {
}, function(reason) {
console.log("error occured");
}, function(value) {
console.log("no callback");
});
}
AnnotationService.js
service.postExample = function(annotationjson) {
alert("Post Example Click Value is " + annotationjson.acctNo + " " + annotationjson.tradeComment); -- I see the data here.
return $http.post(“/annotatetrade/postExample“, annotationjson);
}
AnnotationController.java (restcontroller)
@RequestMapping(value= "annotatetrade/postExample", method= RequestMethod.POST,consumes = "application/json")
public void postExample(@RequestParam TradeAnnotationRequest request){
System.out.println("Post Example account " + request.getAcctNo());
System.out.println("Post Example comment " + request.getTradeComment());
}
TradeAnnotationRequest.java
@Data
@NoArgsConstructor
@AllArgsConstructor
@ApiModel(value="TradeAnnotationRequest")
public class TradeAnnotationRequest {
String acctNo ;
String tradeComment ;
}
使用@RequestParam,这就是我得到的。 2021-11-17 13:28:55.996 WARN 24572 --- [nio-8080-exec-2] .wsmsDefaultHandlerExceptionResolver:已解决由处理程序执行引起的异常:org.springframework.web.bind.MissingServletRequestParameterException:必需的 TradeAnnotationRequest 参数“请求” ' 不存在 2021-11-17 13:29:14.447 WARN 24572 --- [nio-8080-exec-5] .wsmsDefaultHandlerExceptionResolver:已解决由处理程序执行引起的异常:org.springframework.web.bind.MissingServletRequestParameterException:必需的 TradeAnnotationRequest 参数“请求” ' 不存在
使用@RequestBody,我得到空值。 由于某种原因,未传递 JSON 数据。有人可以帮忙吗? 我浏览了很多帖子。
【问题讨论】:
标签: json angularjs spring-restcontroller