0

I'm trying to connect my angular app to my backend spring-boot app and supposedly I need to make a get request from angular to spring-boot, or at least that's what I've seen people do. I'm assuming this error has something to do with returning JSON when it is an observable thats returned but I don't know how to fix that. But I don't want JSON returned I want the html page from my spring boot app returned.

SecurityConfig:

package com.prototype.prototype.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;

@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
    @Override
    public void configure(HttpSecurity http) throws Exception {

        http
                .antMatcher("/**").authorizeRequests()   
                .antMatchers(new String[]{"/home", "/not-restricted", "/css/**"}).permitAll() 
                .anyRequest().authenticated()
                .and()
                .oauth2Login();
    }
}

HomeController:

package com.prototype.prototype.Controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;

@Controller
public class HomeController
{
    @CrossOrigin("http://localhost:4200")
    @GetMapping("/home")
    public String Login() {
        return "index";
    }
   
    @GetMapping("/restricted")
    public String restricted() {
        return "final";
    }
}

rest.sevice.ts:

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

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

  private baseURL = 'http://localhost:8080/home';

  constructor(private http: HttpClient) { }

  checkUsers(): Observable<any> 
  {

  return this.http.get(this.baseURL);
  }
}

servicecomponent.ts:

constructor(private rest: RESTService) { }
ngOnInit(): void 
  {
   
    this.rest.checkUsers().subscribe();
    
  }

image of error

nate
  • 15
  • 1
  • 7
  • Have you tried this? https://stackoverflow.com/questions/46408537/angular-httpclient-http-failure-during-parsing – Tanimak Jun 23 '20 at 18:14

1 Answers1

1

Try to add return type as : responseType: 'text' in
return this.http.get(this.baseURL,{responseType: 'text'});

Similar issue : Angular HttpClient "Http failure during parsing"

Angular 6: How to set response type as text while making http call

Shradha
  • 59
  • 3
  • worked thanks. but shouldn't the html page display in my angular app? Its called in my ngOnIt method – nate Jun 23 '20 at 18:43
  • Will depend on your index.html as well. Is it blank. Your backend returns String "index" but observable is not observing in anywhere. .subscribe is blank. Check above links how to pass and fetch data through subscribe. – Shradha Jun 23 '20 at 18:50