skip to Main Content

I have been trying to connect my backend (spring boot/java) with my frontend (Angular) but I keep getting this error in the console:

error: SyntaxError: Unexpected token 'S', "[Services {"... is not valid JSON at JSON.parse,
message: "Http failure during parsing for http://localhost:8080/providers/getServices"

My service looks like this:

import { Injectable } from '@angular/core';
import { HttpClient, HttpHeaders } from '@angular/common/http';
import { Observable } from 'rxjs';

import { ServiceSection } from 'src/app/models/services/service-section';

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

  private servicesUrl: string;
  private projectsUrl: string;
  // services: Observable<ServiceSection[]>;

  constructor(private http: HttpClient) {
    this.servicesUrl = 'http://localhost:8080/providers/getServices';
    this.projectsUrl = 'http://localhost:8080/projects/getAllProjects'
  }

  public getAll(): Observable<ServiceSection[]> {
    console.log(this);
    return this.http.get<ServiceSection[]>(this.servicesUrl);
  }
}

And my controller looks like this:

package hibera.web.api.controllers;

import com.google.api.core.ApiFuture;
import com.google.cloud.firestore.CollectionReference;
import com.google.cloud.firestore.DocumentSnapshot;
import com.google.cloud.firestore.QuerySnapshot;
import hibera.web.api.domain.Services;
import hibera.web.api.service.FirebaseInit;
import hibera.web.api.service.ProvidingsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutionException;

@RestController
@RequestMapping("/providers")
@CrossOrigin(origins = "http://localhost:4200")
public class ProvidersController {
    @Autowired
    FirebaseInit db;
    @Autowired
    public ProvidingsService service;

    @GetMapping("/getServices")
    public String getServices() throws InterruptedException, ExecutionException {
        List<Services> services = new ArrayList<Services>();

        CollectionReference service = db.getFirebase().collection("services_section");
        ApiFuture<QuerySnapshot> querySnapshot = service.get();

        for (DocumentSnapshot doc:querySnapshot.get().getDocuments()){
            Services serv = doc.toObject(Services.class);
            services.add(serv);
        }

        return services.toString();
    }
}

I understand that it is not being parsed as a json object but when I try to add {responseType: 'text'} it gives me a bunch of errors in the console. In postman all works fine, but trying to loop it the data from the database to the client gives me an actual headache. I honestly dont think it has anything to do with the API but rather the client.

I someone could have an answer for me or atleast help me.

Thanks in advance 🙂

2

Answers


  1. You need to convert your object to valid json with a Jackson ObjectMapper rather than do a toString()

    getServices should return as follows:

    return objectMapper.writeValueAsString(services);
    

    You will need to autowire an ObjectMapper into ProvidersController as follows. ObjectMapper is dependent on jackson-databind but you probably have it via a dependency to spring-boot-starter-web

    @Autowired
    ObjectMapper objectMapper; // com.fasterxml.jackson.databind.ObjectMapper
    
    Login or Signup to reply.
  2. The .toString() method does not convert to proper JSON format.
    As mentioned in the other answer, you can use any Json library to convert it into a JSON string.

    But instead of returning String, you can return the object. Since it’s a rest controller, it will convert the object into a JSON by default.

    return services;
    

    And in Angular, you can parse it normally.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search