'Passing JSON from AngularJS to Springboot RestController is not working
I am facing a problem with one of the project that I work. Here is the scenario.
I have Angular Springboot project, where from a HTML page, data has to be sent to Springboot Controller (restController) and does the processing. When I send a single String as input to an endpoint, it works, but when I have to send a JSON, it does not work.
Here is the sample code.
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 ;
}
With @RequestParam, this is what I get. 2021-11-17 13:28:55.996 WARN 24572 --- [nio-8080-exec-2] .w.s.m.s.DefaultHandlerExceptionResolver : Resolved exception caused by Handler execution: org.springframework.web.bind.MissingServletRequestParameterException: Required TradeAnnotationRequest parameter 'request' is not present 2021-11-17 13:29:14.447 WARN 24572 --- [nio-8080-exec-5] .w.s.m.s.DefaultHandlerExceptionResolver : Resolved exception caused by Handler execution: org.springframework.web.bind.MissingServletRequestParameterException: Required TradeAnnotationRequest parameter 'request' is not present
With @RequestBody, I get nulls. For some reason, JSON data is not passed. Can anyone please help? I went through lots of posts.
I have some progress. However I am still getting data null in my restcontroller. Following changes are made
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);
}
and in my 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());
}
added following dependency in my pom.xml
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</dependency>
However I am still getting null. Could anyone who knows please help
Solution 1:[1]
Try to edit your postExample (the json body) as:
{
"acctNo": $scope.tradeAnnotationDto.acctNo,
"tradeComment": $scope.tradeAnnotationDto.tradeComment
}
Pay attention the quotation mark.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Shay Zambrovski |