角度的HttpClient请求返回404。链接为actif

时间:2019-05-24 22:19:46

标签: angular typescript angular-httpclient

我正在尝试从tomcat服务器获取数据到有角度的应用程序。 在我的tomcat服务器上,我有一个返回json的链接 在我的角度应用程序中,我尝试使用httpclient和httpheader获取json。

当我将链接从角度代码复制到浏览器时,浏览器会接受它。然后在tomcat服务器的终端中显示系统消息。 但是,当我运行角度代码时,没有显示终端消息,并且在角度应用程序的控制台日志中,我看到了如下所示的错误。

下面是用于生成json响应的java方法。系统输出语句按预期显示json。

    public String toJSON(Object object) throws JsonProcessingException
    {
        ObjectMapper mapper = new ObjectMapper();
        return mapper.writeValueAsString(object);
    }

    private void getAllUser(HttpServletRequest request, HttpServletResponse response) throws IOException
    {
        System.out.println("en deze request is GET-ALL-USERS ");
        String userJSON = this.toJSON(model.getPersons());
        System.out.println("en deze request is GET-ALL-USERS  = " + userJSON);
        response.setContentType("application/json");
        response.getWriter().write(userJSON);
    }

接收json的角度代码:

import { Injectable } from '@angular/core';
import { HttpClient, HttpHeaders } from '@angular/common/http';
import { Gebruiker } from './gebruiker';
import { MessageService } from './message.service';
import { Observable, of } from 'rxjs';
import { catchError, map, tap } from 'rxjs/operators';

const httpOptions = {
  headers: new HttpHeaders({ 'Content-Type': 'application/json' })
};

@Injectable({
  providedIn: 'root'
})

export class GebruikerService {
  private urlGetAllUsers = 'http://localhost:8081/Controller?action=angular&asyncAction=getAllUsers';

  constructor(
    private http: HttpClient,
    private messageService: MessageService) {

  }

  /** GET gebruikers from the server */
  getGebruikers (): Observable<Gebruiker[]> {
    const url = `${this.urlGetAllUsers}`;
    return this.http.get<Gebruiker[]>(url)
      .pipe(
        tap(_ => this.log('fetched gebruikers')),
        catchError(this.handleError('getGebruikers', []))
      );
  }
  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

      // TODO: better job of transforming error for user consumption
      this.log(`${operation} failed: ${error.message}`);

      // Let the app keep running by returning an empty result.
      return of(result as T);
    };
  }
}

我在控制台中收到的错误消息:

{body: {…}, url: "http://localhost:8081/Controller?action=angular&asyncAction=getAllUsers", headers: HttpHeaders, status: 404, statusText: "Not Found"}
body: {error: "Collection 'undefined' not found"}
headers: HttpHeaders
lazyInit: ƒ ()
lazyUpdate: null
normalizedNames: Map(0) {}
__proto__: Object
status: 404
statusText: "Not Found"
url: "http://localhost:8081/Controller?action=angular&asyncAction=getAllUsers"
__proto__: Object

我希望有人能帮助我。 谢谢

1 个答案:

答案 0 :(得分:0)

当您从浏览器运行GET方法时,浏览器在请求中传递了什么?您能否对有效的获取请求的网络标签进行屏幕截图?您可以使用Google Chrome浏览器的开发人员工具捕获浏览器发送的get请求,然后将其粘贴为文本或图像。

我在想,浏览器没有传递“ Content-Type”:“ application / json”,而是其他某种类型(也许默认为文本)。同样,tomcat服务器(我假设它是基于Java EE的应用程序)也可能没有将请求路由到适当的请求处理程序。

您还可以提供请求映射设置吗,我想您可能在servlet应用程序的web.xml配置文件中(已部署到Tomcat服务器)。