尝试通过有角度的前端发出http api请求

时间:2019-09-30 21:12:09

标签: angular docker nginx docker-compose

我在java / spring中有一个rest API容器,并将其容器化为docker服务。 前端以角度编写,并通过NGINX服务器提供服务。 不幸的是,对http://service-rest:8080/api/call的调用失败了。 关于这个的任何想法

events {
  worker_connections  4096;  ## Default: 1024
}
http {
    server {
        listen 80;
        server_name  service-frontend;

        root   /usr/share/nginx/html;
        index  index.html index.htm;
        include /etc/nginx/mime.types;

        gzip on;
        gzip_min_length 1000;
        gzip_proxied expired no-cache no-store private auth;
        gzip_types text/plain text/css application/json application/javascript application/x-javascript text/xml application/xml application/xml+rss text/javascript;

        location / {
           root   /usr/share/nginx/html;
           index  index.html index.htm;
        }

        location /api {
           proxy_pass http://service-rest:8080;
        } 
    }
}

后端控制器

@RestController
@RequestMapping("/api")
@CrossOrigin
public class EntityController {

    @Autowired
    private EntityRepository entityRepository;

    @GetMapping("/entities")
    public ResponseEntity<Page<NumberArray>> getEntities() {
        return ResponseEntity.status(HttpStatus.OK).body(entityRepository.findAll(Pageable.unpaged()));
    }

    @PostMapping("/entities")
    public ResponseEntity<Entity> createEntity(@Valid @RequestBody Entity entity) throws JsonProcessingException {

        return ResponseEntity.status(HttpStatus.OK).body(entityRepository.save(entity));
    }

}

docker-compose.yaml

version: "2"
services:
  service-db:
    image: postgres:9.4.5
    container_name: service-db
    hostname: service-db
    network_mode: bridge
    volumes:
      - service-db-data:/var/lib/postgresql/data
    expose:
      - 5432
    ports:
      - 5433:5432
    environment:
         - POSTGRES_PASSWORD=postgres
         - POSTGRES_USER=postgres
         - POSTGRES_DB=postgres
    restart: unless-stopped
  service-server:
    build: serviceserver/.
    container_name: service-server
    hostname: service-server
    network_mode: bridge
    expose:
      - 1234
    ports:
      - 1234:1234
    restart: unless-stopped
  service-rest:
    build: servicerest/.
    container_name: service-rest
    hostname: service-rest

    network_mode: bridge

    expose:
      - 8080
    ports:
      - 8080:8080
    restart: unless-stopped
    depends_on:
      - service-db
    links:
      - service-db
      - service-server
  service-frontend:
    build: servicefrontend/.
    container_name: service-frontend
    hostname: service-frontend
    network_mode: bridge

    restart: unless-stopped
    ports:
      - "81:80"
    depends_on:
      - service-rest
    links:
      - service-rest



volumes:
    service-db-data:

浏览器消息 跨域请求被阻止:同源策略禁止读取http://service-rest:8080/api/call处的远程资源。 (原因:CORS请求未成功)。 错误错误:“未捕获(在承诺中):SF:{” headers“:{” normalizedNames“:{},” lazyUpdate“:null,” headers“:{}},” status“:0,” statusText“:”未知错误“,” url“:” http://service-rest:8080/api/call“,” ok“:false,”名称“:” HttpErrorResponse“,”消息“:”对http://service-rest:8080/api/numberarrays的Http错误响应:0未知错误“, “错误”:{“ isTrusted”:true}}“

1 个答案:

答案 0 :(得分:0)

调用精确的http请求,如

  client.get("http//service-rest:8080/api/call")

错了

正确的方法是致电

   client.get("/api/call")
相关问题