JHipster spring 微服务控制器

JHipster spring controller with microservices

我有一个 JHipster 网关+微服务应用程序。我用 jhipster spring-controller 添加了 spring 服务,然后像这样编辑代码:

@RestController
@RequestMapping("/api/data")
public class DataResource {

    /**
    * GET vin
    */
    @GetMapping("/vin")
    public ResponseEntity<Object> vin(@Valid @RequestBody String address) {
        Chart3DataDTO[] data=new Chart3DataDTO[15];
        for (int i=0;i<15;i++){
            data[i]=new Chart3DataDTO(System.currentTimeMillis()+i, 200+i, 201+i, 202+i);
        }
        return ResponseEntity.ok(data);
    }

为完整起见,这是 DTO

public class Chart3DataDTO {

    private Long xAxis;
    private Integer[] yAxis=new Integer[3];

    public Chart3DataDTO(Long xAxis, Integer yAxis1, Integer yAxis2, Integer yAxis3) {
        this.xAxis = xAxis;
        this.yAxis = new Integer[]{yAxis1, yAxis2, yAxis3};
    }

    public Long getxAxis() {
        return xAxis;
    }

    public Integer[] getyAxis() {
        return yAxis;
    }
}

然后我对网关和微服务进行了 docker 化,jhipster docker-compose 并开始了。一切正常,但是当 Angular 前端要求 /api/data/vin 我得到:

我错过了什么?

此外,它没有出现在 Jhipster 注册表中API

第二次编辑:添加了客户端 angular 代码

import { Injectable } from '@angular/core';
import { Observable, of, throwError } from 'rxjs';
import { HttpClient, HttpHeaders, HttpErrorResponse } from '@angular/common/http';
import { catchError, tap, map } from 'rxjs/operators';

const httpOptions = {
  headers: new HttpHeaders({ 'Content-Type': 'application/json',
    'Access-Control-Allow-Origin':'*'

  })
};
//const apiUrl = 'api/vin';
const apiUrl = '/api/data/vin';

@Injectable({
  providedIn: 'root'
})
export class ApiService {

  constructor(private http: HttpClient) {}
  /*
  private handleError<T> (operation = 'operation', result?: T) {
    return (error: any): Observable<T> => {

      // TODO: send the error to remote logging infrastructure
      console.error(error); // log to console instead

      // Let the app keep running by returning an empty result.
      return of(result as T);
    };
  }
*/
  getInputVoltage(address: String): Observable<[]> {
    return this.http.get<[]>(`${apiUrl}` + '?address=' + address,httpOptions);
  }
}

import { Component, OnInit } from '@angular/core';
import * as Highcharts from 'highcharts';
import { ApiService } from '../api.service';

@Component({
  selector: 'jhi-device-graph',
  templateUrl: './device-graph.component.html',
  styleUrls: ['./device-graph.component.scss']
})
export class DeviceGraphComponent implements OnInit {
  Highcharts: typeof Highcharts = Highcharts;
  chartOptions: Highcharts.Options = {
    xAxis: {
      type: 'datetime'
    },
    yAxis: {
      title: {
        text: 'Voltage'
      }
    },
    title: {
      text: 'Input voltage'
    },
    series: [
      {
        data: [
          [Date.UTC(2010, 0, 1), 29.9],
          [Date.UTC(2010, 2, 1), 71.5],
          [Date.UTC(2010, 3, 1), 106.4]
        ],
        type: 'line',
        name: 'Vin1'
      },
      {
        data: [
          [Date.UTC(2010, 0, 1), 39.9],
          [Date.UTC(2010, 2, 1), 91.5],
          [Date.UTC(2010, 3, 1), 96.4]
        ],
        type: 'line',
        name: 'Vin2'
      }
    ]
  };

  data: String[] = [];
  isLoadingResults = true;

  constructor(private api: ApiService) {}

  ngOnInit(): void {
    this.api.getInputVoltage('10.1.30.1').subscribe(
      (res: any) => {
        this.data = res;
        //console.log(this.data);
        this.isLoadingResults = false;
      },
      err => {
        //console.log(err);
        this.isLoadingResults = false;
      }
    );
  }
}

假设您在调用 get 时发送参数(查看 @RequestBody 地址)并且没有看到日志,您可以尝试将 ResponseEntity<Object> 更改为 ResponseEntity<Chart3DataDTO[]>

您的 angular 客户端在 /api/data/vin 上发送带有查询参数的 GET 请求,而您的 REST 控制器需要请求正文,这是行不通的。

您的控制器必须期待 @RequestParam

此外,当请求通过网关时,它必须以 /services 和您的服务名称为前缀,因此在您的情况下 URL 是 /services/graph/api/data/vin.

此外,在字符串上使用 @Valid 不会执行任何操作,除非您添加一些其他验证注释,例如 @NotBlank@Size(max=30)

@GetMapping("/vin")
public ResponseEntity<Object> vin(@Valid @RequestParam String address) {

@RequestBody 只能用于 POST 或 PUT。