我在我作業的一個專案中遇到了問題。這是場景。
我有 Angular Springboot 專案,從 HTML 頁面,資料必須發送到 Springboot 控制器(restController)并進行處理。當我將單個字串作為輸入發送到端點時,它可以作業,但是當我必須發送 JSON 時,它不起作用。
這是示例代碼。
注釋控制器.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");
});
}
注釋服務.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(休息控制器)
@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.MissingServletRequestParameterRequestException:需要引數 TradeAnnotation不存在 2021-11-17 13:29:14.447 WARN 24572 --- [nio-8080-exec-5] .wsmsDefaultHandlerExceptionResolver:由處理程式執行引起的已解決例外:org.springframework.web.bind.MissingAnnotRequestParameterException 引數:必需“請求”不存在
使用@RequestBody,我得到空值。出于某種原因,未傳遞 JSON 資料。有人可以幫忙嗎?我瀏覽了很多帖子。
uj5u.com熱心網友回復:
我有一些進步。但是,我的 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>
但是我仍然為空。知道的人可以幫忙嗎
uj5u.com熱心網友回復:
嘗試將您的 postExample(json 正文)編輯為:
{
"acctNo": $scope.tradeAnnotationDto.acctNo,
"tradeComment": $scope.tradeAnnotationDto.tradeComment
}
注意引號。
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/359689.html
