我正面临着我工作的一个项目的问题。这是一个场景。
我有一个Angular Springboot项目,从一个超文本标记语言页面,数据必须发送到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 .w.s.m.s.DefaultHandlerExceptionResolver :处理程序执行导致的已解决异常: org.springframework.web.bind.MissingServletRequestParameterException:必需的请求参数'request‘不存在2021-11-17 13:29:14.447 WARN 24572 - nio-8080-exec-5 .w.s.m.s.DefaultHandlerExceptionResolver :处理程序执行导致的已解决异常:nio必需的TradeAnnotationRequest参数'request’不存在
使用@RequestBody,我会得到空值。由于某些原因,不传递JSON数据。有谁能帮帮忙吗?我看了很多帖子。
发布于 2021-11-18 10:11:36
尝试将postExample ( json主体)编辑为:
{
"acctNo": $scope.tradeAnnotationDto.acctNo,
"tradeComment": $scope.tradeAnnotationDto.tradeComment
}
注意引号。
发布于 2021-11-18 02:23:44
我有一些进展。然而,在我的restcontroller中,我的数据仍然是空的。进行了以下更改
service.postExample = function(medicoRicercato) {
alert("Post Example Click Value is " + medicoRicercato.acctNo + " " + medicoRicercato.tradeComment);
return $http({
url: CONSTANTS.postExample,
contentType: "application/json;charset=UTF-8",
method: "POST",
data: {medicoRicercato},
dataType: 'json'
});
//return $http.post(CONSTANTS.postExample, medicoRicercato);
}
在我的restController中
@RequestMapping(value = "/postExample", method = RequestMethod.POST, consumes = "application/json",
headers = "content-type=application/json")
public void postExample(@RequestBody TradeAnnotationRequest request){
System.out.println("Before Request");
System.out.println(request);
System.out.println("Post Example ac " + request.getAcctNo());
System.out.println("Post Example co " + request.getTradeComment());
}
在我的pom.xml中添加了以下依赖项
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</dependency>
然而,我仍然得到null。知道的人能帮帮忙吗?
https://stackoverflow.com/questions/70011368
复制相似问题