简体   繁体   English

springboot和ionic如何通信?

[英]How communicate with springboot and ionic?

i am using ionic 4 with spring boot web service, the problem is after installation application in my phone the web service is not run?我正在使用带有 spring 启动 web 服务的 ionic 4,问题是在我的手机中安装应用程序后 web 服务没有运行?

i use in Controller.java我在 Controller.java 中使用

@CrossOrigin(origins = "*")
@RestController
public class CarController {
    @Autowired
    CarRepository carRepository;
    
    @GetMapping(path = "/cars")
    public ResponseEntity<List<Car>> retriveCars() {
        try {
            List<Car> cars = new ArrayList<>();
            carRepository.findAll().forEach(cars::add);
            
            
            if (cars.isEmpty()) {
                return new ResponseEntity<>(null, HttpStatus.NO_CONTENT);
            }
            return new ResponseEntity<>(cars, HttpStatus.OK);
        }catch(Exception e) {
            return new ResponseEntity<>(null, HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }
    
    @PostMapping(path = "/car")
    public ResponseEntity<Car> saveCar (@RequestBody Car car) {
        try {
            Car _car= carRepository.save(new Car(UUID.randomUUID(), car.getMake(), car.getModel()));
            return new ResponseEntity<>(_car, HttpStatus.CREATED);
        }
        catch(Exception e) {
            System.out.println(e);
            return new ResponseEntity<>(null, HttpStatus.EXPECTATION_FAILED);
            
        }
    }

and in api.service.ts并在 api.service.ts

 import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http'
import { Car } from './car';

@Injectable({
  providedIn: 'root'
})
export class ApiService {
  constructor(private http: HttpClient) { }

  baseUrl='http://localhost:8081';

  retrieveCars(){
    return this.http.get<Car[]>(this.baseUrl+ '/cars');
  }

  saveCar(car: Car) {
    return this.http.post<Car>(this.baseUrl+'/car', car);
  }

My question is how can i communicate in ionic and springboot?我的问题是我如何在 ionic 和 springboot 中进行交流? i need help please我需要帮助

yes it will not work because http gives mixed data in mobiles and emulators.... it will work perfectly with browsers but not with emulators and devices是的,它不会工作,因为 http 在手机和模拟器中提供混合数据....它将与浏览器完美配合,但不适用于模拟器和设备

Solution is simple just use https to get data in api.service.ts in baseUrl everything else is fine解决方案很简单,只需使用https在 baseUrl 中的 api.service.ts 中获取数据,其他一切都很好

https ://localhost:8081 https ://localhost:8081

baseUrl='http://localhost:8081'; baseUrl='http://localhost:8081';

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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