发布http请求仅适用于邮递员

时间:2020-05-15 22:30:04

标签: spring-mvc servlets xmlhttprequest http-post postman

我已经开发了一个API服务,其中Spring MVC作为后端,而Ionic 3作为FrontEnd。

我开发了许多post和get方法,但是仅在使用邮递员的情况下,无法在浏览器中向现有@controller添加新方法。

java error server

控制器为:

package com.noecar.car;

import java.util.List;

import javax.management.OperationsException;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;

import com.noecar.appointment.Appointment;
import com.noecar.appointment.AppointmentService;

@Controller
public class CarController{

    @Autowired
    CarService carService;

    @Autowired
    AppointmentService appointmentService;

    @PostMapping("car/{email}")
    @ResponseBody
    public Boolean create(@RequestBody Car car, @PathVariable("email") String email ) throws OperationsException {
        return carService.create(car, email);
    }

    @PostMapping("appointment") // THIS IS NOT WORKING
    @ResponseBody
    public Boolean createappointment(@RequestBody Appointment appointment ) throws OperationsException {
        return appointmentService.create(appointment, (long) 1, "email_example");
    }


    @DeleteMapping("deleteCar/{carId}")
    @ResponseBody
    public Boolean delete(@PathVariable("carId") Long carId ) throws OperationsException {
        return carService.delete(carId);
    }

    @GetMapping("car")
    @ResponseBody
    public List<Car> findAll() {
        return carService.findAll();
    }

}

IONIC应用程序通过请求而未在服务中找到它的映射: HTTP request error

这是发出请求的Ionic TypeScript代码,并且我检查了URL是否正确组成:

setAppointment(descripcion: string, hours: number, date: Date, tipo:string) {
  let appointment = {
    date:date,
    descripcion:descripcion,
    hours: hours,
    tipo:tipo
  }

  let headers = new Headers({
    'Content-Type':'application/json'
  });

  let options = new RequestOptions({
    headers: headers
  });

  let url = URL_SERVICIOS + "/appointment";

  let json = JSON.stringify(appointment); //the JSON in string format

    return new Promise(resolve => {
      this.http.post(url, json, options).subscribe(data => {
        if (data.json()) {
          resolve(true);
        } 
        else {
          resolve(false);
        }
      });

0 个答案:

没有答案