为什么我的 Angular 服务请求无法到达 Java servlet?

问题描述 投票:0回答:1

各位:

我是 Angular 新手。我们的团队正在使用 Angular 16.1.8 重写前端,并保留 Java(开放 JDK 1.8)作为后端编程语言。我需要在 Angular 中实现一个日志类,以便我们可以将 GUI 日志信息保存到后端文件中。我们计划从 GUI 发送日志服务调用,并使用 Java servlet 接受请求并使用现有 Java log4j 写入日志文件。但是,我无法通过日志服务调用来访问 servlet;在调试模式下,servlet 中根本不会执行任何语句。另一方面,在我的代码进行服务调用之前一切都很顺利。类似地,还有其他服务调用,其他 servlet 确实会获取请求。有人可以给我提示吗?

我附上Angular代码如下:

import { Injectable } from '@angular/core';
import { WebServiceClient } from './web.service.client';

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

    constructor(private departureService : WebServiceClient) { }

    public logToServer(severity : string, message : string) {
    // We are using POST instead of GET for security reasons. GET is flagged by 
    // Software Security testing tools like WebInspect
        return this.departureService.post(
          "http://localhost:8080/departures"
          , '/application-log'
          , {
             "applicationName" :  "Departure-ui",
             "dateTime" : new Date(),
             "severity" : severity,
              "message" : message
         }
         , "json");
    }

  }

我将附上Java代码如下:

package gov.faa.infra.ifp.web.portlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;

import gov.faa.infra.ifp.bo.IfpAccessBo.ModeType;
import gov.faa.infra.ifp.domain.ResponseMessage.Importance;
import gov.faa.infra.ifp.util.IfpSession;
import gov.faa.infra.ifp.util.JsonUtils;

@WebServlet("/application-log")
public class LogServlet extends BaseServiceServlet {
    private static final long serialVersionUID = 1L;
    private static final Logger logger = null;

    @Override
    protected void doGet(
            HttpServletRequest request, HttpServletResponse response) 
                    throws ServletException, IOException {
        response.setStatus(HttpServletResponse.SC_METHOD_NOT_ALLOWED);
        throw new ServletException("GET method is not supported");
    }
    
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        long start = System.currentTimeMillis();
        int transactionHashCode = -1;
        String parameter = null;
        Boolean serviceIsValid = null;
        int status = HttpServletResponse.SC_OK;
        String statusMessage = null;

        Log logInput = null;
        String severity = null;
        String logMessage = null;

        try {
            transactionHashCode = 
                    IfpSession.getSession().beginReadOnlyTransaction();
            
            if(this.verifyAccess(ModeType.QueryFlightProcedure, request)) {
                logInput = JsonUtils.hydrateObject(
                            (parameter = this.readInputAsString(request))
                            , Log.class);

                
                if (StringUtils.isNotEmpty(logInput.getMessage())) {
                    logMessage = "";
                    
                    if (StringUtils.isNotEmpty(logInput.getDateTime()))
                        logMessage = logMessage + logInput.getDateTime();
                    
                    if (StringUtils.isNotEmpty(logInput.getApplicationName()))
                        logMessage = logMessage + logInput.getApplicationName();                
                    
                    if (StringUtils.isNotEmpty(logInput.getSeverity()))
                        logMessage = logMessage + logInput.getSeverity();
                    
                    if (StringUtils.isNotEmpty(logInput.getMessage()))
                        logMessage = logMessage + logInput.getMessage();
                    
                    severity = logInput.getSeverity();
                    
                    switch (severity) {
                        case "success": 
                            logger.trace(logMessage);
                            break;
                        case "info": 
                            logger.info(logMessage);
                            break;
                        case "warning": 
                            logger.warn(logMessage);
                            break;
                        case "error": 
                            logger.error(logMessage);
                            break;
                    }
                    
                } else {
                    IfpSession.getSession().addMessage(Importance.Failure, "empty log message", null);
                    status = HttpServletResponse.SC_BAD_REQUEST;
                }
                
            }
            else
                status = HttpServletResponse.SC_UNAUTHORIZED;       
                IfpSession.getSession().addMessage(Importance.Failure, "unauthorized access", null);
            
        }
        catch(Exception exception) {
            this.ifpSessionResponseWithExceptionAndRollback(
                    response, null, transactionHashCode
                    , "unable to write log" 
                            + (statusMessage == null 
                                    ? "" : " while " + statusMessage)
                    , exception
                    , new Object[] { 
                            serviceIsValid, parameter 
                    });
        }
        finally {
            IfpSession.getSession().rollbackTransaction(transactionHashCode);
            this.logTiming("completed LogServlet.doPost", start);
        }
    }

}
java angular servlets service
1个回答
0
投票

也许您可以检查 CORS 策略是否禁止调用您的后端。在开发时使用代理可能是个好主意。您能在控制台中看到一些错误消息吗?

© www.soinside.com 2019 - 2024. All rights reserved.