Search code examples
spring-bootmicroservicesjhipster

JHipster spring controller with microservices


I have a JHipster gateway+microservice application. I have added a spring service with jhipster spring-controller and then edited the code like this:

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

For completeness, this is the 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;
    }
}

Then I have dockerized gateway and microservice, jhipster docker-compose and started all. Everything works but when the Angular frontent asks for /api/data/vin I get:

  • if not logged in: 401 (which is fine)

  • if logged in: the JHipster page 'an error has occurred', instead of returning the JSON of the DTO

What did I miss?

Also, it doesn't appear listed on the Jhipster registry API

2ND EDIT: Added client angular code

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

And

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

Solution

  • Your angular client sends a GET request on /api/data/vin with query parameters while your REST controller expects a request body, this can't work.

    Your controller must expect a @RequestParam

    Also, as the request goes through a gateway, it must be prefixed by /services and your service name, so in your case the URL is /services/graph/api/data/vin.

    Also using @Valid on a String does not do anything unless you add some other validation annotations like @NotBlank or @Size(max=30)

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

    @RequestBody must be used only for POST or PUT.