【问题标题】:Passing JSON from AngularJS to Springboot RestController is not working将 JSON 从 AngularJS 传递到 Springboot RestController 不起作用
【发布时间】: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


    【解决方案1】:

    我有一些进展。但是,我的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>
    

    但是我仍然是空的。有知道的朋友帮忙看看

    【讨论】:

      【解决方案2】:

      尝试将您的 postExample(json 正文)编辑为:

      {
              "acctNo": $scope.tradeAnnotationDto.acctNo,
              "tradeComment": $scope.tradeAnnotationDto.tradeComment
      }
      

      注意引号。

      【讨论】:

      • 谢谢谢伊。在以下添加的引号中 ``` { "acctNo": $scope.tradeAnnotationDto.acctNo, "tradeComment": $scope.tradeAnnotationDto.tradeComment }; ``` 我的 RestController 有 ``` @RequestMapping(value = "/postExample", method = RequestMethod.POST, consumes = "application/json", headers = "content-type=application/json") public void postExample( @RequestBody TradeAnnotationRequest request){ ``` 当我检查 service.js 中的值时,值仍然为 null,因为 TradeAnnotationRequest(acctNo=null, tradeComment=null) 就在那里。
      猜你喜欢
      • 2018-04-17
      • 1970-01-01
      • 2021-06-18
      • 2017-09-29
      • 1970-01-01
      • 2015-10-30
      • 2016-11-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多