在尝试向服务器发送HTTP响应后,为什么我的参数保持为空

时间:2019-02-20 13:20:29

标签: java rest

我已经搜索了3天,以获取有关此主题的答案。 我有一个我想做的项目,但卡在了登录部分。 我正在尝试将参数从我的角度应用程序发送到后端Java项目。

这是Java登录算法:

public CouponClientFacade login(String name, String password, ClientType 
clientType) throws Exception {

    CompanyDBDAO companyDBDAO = new CompanyDBDAO();
    CustomerDBDAO customerDBDAO = new CustomerDBDAO();

    switch (clientType) {
    case ADMIN:
        if (name.equalsIgnoreCase("admin") & password.equalsIgnoreCase("1234")) {
            System.out.println("Logged In");
            return (CouponClientFacade) new AdminFacade();
        } else {
            throw new Exception("Login to Admin Failed!");
        }
    case COMPANY:
        if (companyDBDAO.login(name, password, clientType))
            ;
        return new CompanyFacade(0);

    case CUSTOMER:
        if (customerDBDAO.login(name, password, clientType))
            ;
        return new CustomerFacade(0);
    default:
        return null;

    }
}

这是具有响应算法的登录名的其余Web服务:

package couponSystemServices;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status;

import system.core.MainUse.CouponSystem;
import system.core.facade.AdminFacade;
import system.core.facade.CompanyFacade;
import system.core.facade.CouponClientFacade;
import system.core.facade.CustomerFacade;
import system.core.javabeans.ClientType;
import system.core.javabeans.Company;

@Path("loginpage")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public class LoginService {
    @Context
    private HttpServletRequest request;
    private CouponClientFacade couponFacade;

    @POST
    @Path("login")
    public Response login(@QueryParam("name") String name, @QueryParam("password") String password,
            @QueryParam("clientType") String clientType) throws Exception {
        // ClientType type = ClientType.valueOf(clientType);
        HttpSession session = request.getSession();
        if (!session.isNew()) {
            session.invalidate();
            session = request.getSession();
        }
        CouponSystem cs = CouponSystem.getInstance();
        switch (clientType) {
        case "COMPANY":
            couponFacade = (CompanyFacade) cs.login(name, password, ClientType.COMPANY);
        case "CUSTOMER":
            couponFacade = (CustomerFacade) cs.login(name, password, ClientType.CUSTOMER);
        case "ADMIN":
            couponFacade = (AdminFacade) cs.login("admin", "1234", ClientType.ADMIN);
        }
        session.setAttribute("clientFacade", couponFacade);
        return Response.status(Status.OK).build();
    }
}

我尝试使用邮递员检查我正在使用的参数,即使在邮递员中,我在所有3个参数中都得到Null。

从AngularApp / Postman发送了参数后,添加了登录方法的Stacktrace:

   SEVERE: Servlet.service() for servlet [Jersey REST Service] in context with path [/CouponSystemWeb] threw exception [java.lang.NullPointerException] with root cause
java.lang.NullPointerException
    at couponSystemServices.LoginService.login(LoginService.java:42)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.glassfish.jersey.server.model.internal.ResourceMethodInvocationHandlerFactory.lambda$static$0(ResourceMethodInvocationHandlerFactory.java:74)
    at org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher$1.run(AbstractJavaResourceMethodDispatcher.java:144)
    at org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher.invoke(AbstractJavaResourceMethodDispatcher.java:161)
    at org.glassfish.jersey.server.model.internal.JavaResourceMethodDispatcherProvider$ResponseOutInvoker.doDispatch(JavaResourceMethodDispatcherProvider.java:202)
    at org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher.dispatch(AbstractJavaResourceMethodDispatcher.java:99)
    at org.glassfish.jersey.server.model.ResourceMethodInvoker.invoke(ResourceMethodInvoker.java:388)
    at org.glassfish.jersey.server.model.ResourceMethodInvoker.apply(ResourceMethodInvoker.java:346)
    at org.glassfish.jersey.server.model.ResourceMethodInvoker.apply(ResourceMethodInvoker.java:102)
    at org.glassfish.jersey.server.ServerRuntime$2.run(ServerRuntime.java:337)
    at org.glassfish.jersey.internal.Errors$1.call(Errors.java:271)
    at org.glassfish.jersey.internal.Errors$1.call(Errors.java:267)
    at org.glassfish.jersey.internal.Errors.process(Errors.java:315)
    at org.glassfish.jersey.internal.Errors.process(Errors.java:297)
    at org.glassfish.jersey.internal.Errors.process(Errors.java:267)
    at org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:280)
    at org.glassfish.jersey.server.ServerRuntime.process(ServerRuntime.java:316)
    at org.glassfish.jersey.server.ApplicationHandler.handle(ApplicationHandler.java:1084)
    at org.glassfish.jersey.servlet.WebComponent.serviceImpl(WebComponent.java:418)
    at org.glassfish.jersey.servlet.WebComponent.service(WebComponent.java:372)
    at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:389)
    at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:342)
    at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:229)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:292)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:240)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207)
    at org.apache.catalina.filters.CorsFilter.handleSimpleCORS(CorsFilter.java:301)
    at org.apache.catalina.filters.CorsFilter.doFilter(CorsFilter.java:169)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:240)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:212)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:141)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
    at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:616)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:522)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1095)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:672)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1500)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1456)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.lang.Thread.run(Unknown Source)

0 个答案:

没有答案