gpt4 book ai didi

java - 在 post 方法中从 Angular UI 发送两个参数并由 java API 处理

转载 作者:行者123 更新时间:2023-12-02 12:24:18 28 4
gpt4 key购买 nike

只是需要指导...从 Controller 到服务,变量都是可访问的。但当它们发送到 api 端时不可用。 (只要有一个变量就可以正常工作)变量值是经过一些计算后得出的。

Service.js代码...

function MySvcFunction(value1, value2) {
console.log('value1 : ', value1); //printing value1
console.log('value2 : ', value2); //printing value2
return $http.post('http://' + api_url + value1, value2).then(
function (response) {
//manage success
},
function (error) {
//manage error;
});
}

Controller.js 代码...

$scope.someFunction = function(){
console.log('value1 : ', value1); //printing value1
console.log('value2 : ', value2); //printing value2
MySvcController.MySvcFunction($scope.value1, $scope.value2).then(
function (response) {
//display on screen
});

现在java中的api代码...

场景1异常(有两个@RequestBody)

@PostMapping(value = "/api_url")
public ResponseEntity<Object> MyFunction(@RequestBody Integer value1, @RequestBody Integer value2) {
System.out.println("value1 : "+ value1);
System.out.println("value2 : "+ value2);
}
//Exception:
Failed to read HTTP message: org.springframework.http.converter.HttpMessageNotReadableException: Could not read document: Stream closed; nested exception is java.io.IOException*/

场景2异常(有一个@RequestBody)

@PostMapping(value = "/api_url")
public ResponseEntity<Object> MyFunction(@RequestBody Integer value1, Integer value2) {
System.out.println("value1 : "+ value1); //value1 : int val
System.out.println("value2 : "+ value2); //value2 : null
}
//Exception:
nested NullPointerException with root cause.

最佳答案

我不确定它是否正确?

Controller .js

$scope.someFunction = function(){
var bothVar = {'value1': $scope.value1, 'value2': $scope.value2};
MySvcController.MySvcFunction(bothVar).then(
function (response) {
//display on screen
});

服务.js

function MySvcFunction(bothVar) {
return $http.post('http://' + api_url + bothVar).then(
function (response) {
//manage success
},
function (error) {
//manage error;
});
}

API端java代码

@PostMapping(value = "/api_url")
public ResponseEntity<Object> suggestBreakfast(@RequestBody Map bothVar){
System.out.println("value1 is : "+ bothVar.get("value1"));
System.out.println("value2 is : "+ bothVar.get("value2"));
}
// And i am getting those two values here successfully

关于java - 在 post 方法中从 Angular UI 发送两个参数并由 java API 处理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45561403/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com