简体   繁体   中英

AngularJs POST method to springboot api

Im running an app in Java (springboot) for the backend and angularJs for the front end. I´m having some trouble making the POST/PUT methods work. There are no errors on the post method in the angularjs part but my java code might be broken, it can´t reach the DB, tells me everything is okey but the data doesnt update.

Heres the angularjs method:

        .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)

                        };

                    } ])

The get methods work like a charm and I get the OK in the log after doing the post 控制台日志

Heres my JAVA code for the Controller I´ll upload the service serviceimpl and repository if needed.

   @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);
}

**EDIT: Screenshot of the debug when the method runs:

IDE调试

Not sure why it´s not working... I kinda going through the spring manual to see if I can fix it but it´s making me crazy and time is of the essence.

Heres the HTML just in case:

       <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>

You need to annotate the parameter with @RequestBody :

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM