gpt4 book ai didi

javascript - AngularJs POST 方法到 springboot api

转载 作者:行者123 更新时间:2023-12-03 08:22:26 24 4
gpt4 key购买 nike

我正在用 Java (springboot) 运行一个应用程序作为后端,并使用 angularJs 作为前端。我在使 POST/PUT 方法正常工作时遇到一些问题。angularjs 部分的 post 方法没有错误,但我的 java 代码可能已损坏,它无法到达数据库,告诉我一切正常,但数据没有更新。

这是 angularjs 方法:

        .controller(
'TecnicoController',
[
'$scope',
'$http',
'$routeParams',
function($scope, $http, $routeParams) {

var onError = function(reason) {
$scope.error = "No se pudo encontrar";
};

var urlbase = "http://localhost:8080/";

var legajo = $routeParams.legajo;

console.log(legajo);

var onTecnicoComplete = function(response) {
$scope.tecnico = response.data;
console.log($scope.tecnico);
};

console.log($scope.tecnico);

$http.get(urlbase + "get/tecnico/" + legajo)
.then(onTecnicoComplete, onError);

$scope.saveTecnico = function(tecnico) {
console.log(tecnico);
return $http.post(urlbase + "set/tecnico/", tecnico)

};

} ])

get 方法就像一个魅力,在完成帖子 Console log 后,我在日志中得到了 OK。

这是我的 Controller JAVA 代码,如果需要,我将上传服务 serviceimpl 和存储库。

   @RestController
public class TecnicoController {

@Autowired
TecnicoRepository tecnicoRepository;

@Autowired
private final TecnicoService tecnicoService;

@Inject
public TecnicoController(final TecnicoService tecnicoService) {
this.tecnicoService = tecnicoService;
}

@RequestMapping(value = "/get/tecnico/{legajo}", method = RequestMethod.GET)
@ResponseBody
public Object queryTecnico(@PathVariable Integer legajo) {
Tecnico tecnico = tecnicoService.getTecnico(legajo);
if (tecnico == null)
return "No encontrado";
return tecnico;
}

@RequestMapping(value = "/get/tecnico/", method = RequestMethod.GET)
@ResponseBody
public Object queryTecnico() {
List<Tecnico> tecnico = tecnicoRepository.findAll();
return tecnico;
}

@RequestMapping(value = "/set/tecnico/", method = RequestMethod.POST)
@Transactional
public Tecnico editTecnico(final Tecnico tecnico) {
return tecnicoRepository.save(tecnico);
}

**编辑:方法运行时的调试屏幕截图:

IDE DEBUG

不知道为什么它不起作用......我有点浏览 Spring 手册,看看是否可以修复它,但这让我发疯,时间至关重要。

下面是 HTML,以防万一:

       <form role="form">
<div class="panel-body">

<div class="panel-body">
<img src="/assets/doge.jpg" alt="Doge">
</div>

<div class="container">
<div class="input-group">
<span class="input-group-addon" id="tec-nombre">Nombre del
Tecnico:</span><input type="text" class="form-control"
data-ng-model="tecnico.nombre" aria-describedby="tec-nombre">
</div>
<div class="input-group">
<span class="input-group-addon" id="tec-legajo">Legajo del
Tecnico:</span><input type="number" class="form-control"
data-ng-model="tecnico.legajo" aria-describedby="tec-legajo">
<div role="alert">
<span class="error"
data-ng-show="myForm.legajoTecnico.$error.required">
Required!</span>
</div>
</div>
<div class="input-group">
<span class="input-group-addon" id="tec-email">Email del
Tecnico:</span><input type="email" class="form-control"
data-ng-model="tecnico.email" aria-describedby="tec-email">
<div role="alert">
<span class="error"
data-ng-show="myForm.emailTecnico.$error.required">
Required!</span>
</div>
</div>
<div class="input-group">
<span class="input-group-addon" id="tec-interno">Interno del
Tecnico:</span><input type="text" class="form-control"
data-ng-model="tecnico.interno" aria-describedby="tec-interno">
<div role="alert">
<span class="error"
data-ng-show="myForm.nombreTecnico.$error.required">
Required!</span>
</div>
</div>
</div>
</div>
<div class= "form-group">
<label class= "col-md-2"></label>
<div class="col-md-4">
<a href="#/" class="btn">Cancel</a>
<button data-ng-click="saveTecnico(tecnico);"
class="btn btn-primary">Actualizar {{tecnico.legajo}}</button>
<button data-ng-click="deleteCustomer(customer)"
data-ng-show="customer._id" class="btn btn-warning">Delete</button>
</div>

最佳答案

需要用@RequestBody注释参数:

public Tecnico editTecnico(final @RequestBody Tecnico tecnico) {
return tecnicoRepository.save(tecnico);
}

关于javascript - AngularJs POST 方法到 springboot api,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33696008/

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