当前位置: 首页 > 知识库问答 >
问题:

servlet.service()用于包含路径的上下文中的servlet[dispatcherServlet]

微生恩
2023-03-14

在创建spring maven rest WebService时,我得到了以下错误。当我使用postman测试OCBC api上的post请求时,它工作得很好。但是当我试图为citi执行post请求时,我得到了以下错误:servlet.service()for servlet[dispatcherServlet]上下文中的path[]抛出异常[request processing Failed;嵌套异常是java.lang.NullPointerException]。在我的邮递员中,它显示错误:“内部服务器错误”,状态:“500”

错误:

Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is java.lang.NullPointerException] with root cause

java.lang.NullPointerException: null
        at com.example.service.impl.PaymentServiceImpl.addCitiPayment(PaymentServiceImpl.java:22) ~[classes/:na]
        at com.example.controller.PaymentController.addCitiPayment(PaymentController.java:25) ~[classes/:na]
        at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:na]
        at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:na]
        at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:na]
        at java.base/java.lang.reflect.Method.invoke(Method.java:564) ~[na:na]
        at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:197) ~[spring-web-5.3.4.jar:5.3.4]
        at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:141) ~[spring-web-5.3.4.jar:5.3.4]
        at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:106) ~[spring-webmvc-5.3.4.jar:5.3.4]
        at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:894) ~[spring-webmvc-5.3.4.jar:5.3.4]
        at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:808) ~[spring-webmvc-5.3.4.jar:5.3.4]
        at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87) ~[spring-webmvc-5.3.4.jar:5.3.4]    
        at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1060) ~[spring-webmvc-5.3.4.jar:5.3.4]
        at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:962) ~[spring-webmvc-5.3.4.jar:5.3.4]
        at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1006) ~[spring-webmvc-5.3.4.jar:5.3.4]
        at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:909) ~[spring-webmvc-5.3.4.jar:5.3.4]
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:652) ~[tomcat-embed-core-9.0.43.jar:4.0.FR]
        at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:883) ~[spring-webmvc-5.3.4.jar:5.3.4]
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:733) ~[tomcat-embed-core-9.0.43.jar:4.0.FR]
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:227) ~[tomcat-embed-core-9.0.43.jar:9.0.43]
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162) ~[tomcat-embed-core-9.0.43.jar:9.0.43]
        at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53) ~[tomcat-embed-websocket-9.0.43.jar:9.0.43]
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:189) ~[tomcat-embed-core-9.0.43.jar:9.0.43]
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162) ~[tomcat-embed-core-9.0.43.jar:9.0.43]
        at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:100) ~[spring-web-5.3.4.jar:5.3.4]
        at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119) ~[spring-web-5.3.4.jar:5.3.4]
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:189) ~[tomcat-embed-core-9.0.43.jar:9.0.43]
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162) ~[tomcat-embed-core-9.0.43.jar:9.0.43]
        at org.springframework.web.filter.FormContentFilter.doFilterInternal(FormContentFilter.java:93) ~[spring-web-5.3.4.jar:5.3.4]
        at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119) ~[spring-web-5.3.4.jar:5.3.4]
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:189) ~[tomcat-embed-core-9.0.43.jar:9.0.43]
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162) ~[tomcat-embed-core-9.0.43.jar:9.0.43]
        at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:201) ~[spring-web-5.3.4.jar:5.3.4]
        at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119) ~[spring-web-5.3.4.jar:5.3.4]
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:189) ~[tomcat-embed-core-9.0.43.jar:9.0.43]
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162) ~[tomcat-embed-core-9.0.43.jar:9.0.43]
        at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:202) ~[tomcat-embed-core-9.0.43.jar:9.0.43]
        at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:97) ~[tomcat-embed-core-9.0.43.jar:9.0.43]
        at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:542) ~[tomcat-embed-core-9.0.43.jar:9.0.43]
        at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:143) ~[tomcat-embed-core-9.0.43.jar:9.0.43]
        at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92) ~[tomcat-embed-core-9.0.43.jar:9.0.43]
        at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:78) ~[tomcat-embed-core-9.0.43.jar:9.0.43]
        at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:346) ~[tomcat-embed-core-9.0.43.jar:9.0.43]
        at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:374) ~[tomcat-embed-core-9.0.43.jar:9.0.43]
        at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:65) ~[tomcat-embed-core-9.0.43.jar:9.0.43]
        at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:887) ~[tomcat-embed-core-9.0.43.jar:9.0.43]
        at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1684) ~[tomcat-embed-core-9.0.43.jar:9.0.43]
        at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49) ~[tomcat-embed-core-9.0.43.jar:9.0.43]
        at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1130) ~[na:na]
        at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:630) ~[na:na]
        at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) ~[tomcat-embed-core-9.0.43.jar:9.0.43]
        at java.base/java.lang.Thread.run(Thread.java:832) ~[na:na]

下面是我的代码:

班克科布卡道

package com.example.repository;

import com.example.model.BankOCBC;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface BankOCBCDAO extends JpaRepository<BankOCBC, Integer>{

}
package com.example.repository;

import com.example.model.BankCitiBank;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface BankCitiBankDAO extends JpaRepository<BankCitiBank, Integer>{

}
package com.example.controller;

import com.example.model.BankCitiBank;
import com.example.model.BankOCBC;
import com.example.service.PaymentService;
import com.example.dto.ApiResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


@RestController
@RequestMapping(path="/payment")
public class PaymentController {
    
    @Autowired
    private PaymentService paymentService;

    
    @PostMapping("/citi")
    public ApiResponse addCitiPayment(@RequestBody BankCitiBank bankCitiBank) {
        return paymentService.addCitiPayment(bankCitiBank);
    }

    @PostMapping("/ocbc")
    public ApiResponse addOCBCPayment(@RequestBody BankOCBC bankOCBC) {
        return paymentService.addOCBCPayment(bankOCBC);
    }
}
package com.example.service;

import com.example.model.BankCitiBank;
import com.example.model.BankOCBC;
import com.example.dto.ApiResponse;

public interface PaymentService {
    ApiResponse addCitiPayment(BankCitiBank bankCitiBank);
    ApiResponse addOCBCPayment(BankOCBC bankOCBC);
}
package com.example.service.impl;

import com.example.dto.ApiResponse;
import com.example.model.BankCitiBank;
import com.example.model.BankOCBC;
import com.example.service.PaymentService;
import com.example.repository.BankCitiBankDAO;
import com.example.repository.BankOCBCDAO;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


@Service
public class PaymentServiceImpl implements PaymentService{
    @Autowired
    private BankOCBCDAO bankOCBCDAO;
    private BankCitiBankDAO bankCitiBankDAO;
    
    @Override
    public ApiResponse addCitiPayment(BankCitiBank bankCitiBank) {
        bankCitiBankDAO.save(bankCitiBank);
        return new ApiResponse(200, "CitiBank Payment Successful", null); 
    }

    @Override
    public ApiResponse addOCBCPayment(BankOCBC bankOCBC) {
        bankOCBCDAO.save(bankOCBC);
        return new ApiResponse(200, "OCBC Payment Successful", null); 
        
    }
    
}

请告知我的代码中有什么错误。提前谢了。

我还可以问一下,是否有任何推荐方法来加密从api获得的数据值,然后添加到数据库中。

共有1个答案

徐凌
2023-03-14

您还需要将@autowired添加到bankcitibankao:

    @Autowired
    private BankOCBCDAO bankOCBCDAO;
    @Autowired
    private BankCitiBankDAO bankCitiBankDAO;

但是,由于字段注入并不是最好的方法(https://www.vojtechruzicka.com/field-dependency-injection-brincession-harbabily/),所以应该使用构造函数注入:

@Service
public class PaymentServiceImpl implements PaymentService{

    private BankOCBCDAO bankOCBCDAO;
    private BankCitiBankDAO bankCitiBankDAO;

    public PaymentServiceImpl(BankOCBCDAO bankOCBCDAO, BankCitiBankDAO bankCitiBankDAO){
       this.bankOCBCDAO = bankOCBCDAO;
       this.bankCitiBankDAO = bankCitiBankDAO;
    }
...
}
 类似资料:
  • 我将Spring Boot与Jetty一起使用,当我从servletContext获取路径时。getRealPath(“/”)结果如下: 但应该像那样或至少类似于: 还有一个有趣的事实是,如果我有webapp文件夹,jetty会在其中使用这个文件夹,比如servlet context和servlet context。getRealPath(“/”)将返回正常路径。 为什么Jetty对servlet

  • 我需要设置Servlet(由于某些原因,只有Servlet而不是handler)来处理WAR以外的文件。在这里https://stackoverflow.com/a/28735121/5057736我找到了以下解决方案: 这个解决方案的工作,这是我需要的。但是,当我将第N行更改为时,它就停止工作。我尝试了“/foo/”和“/foo”,但是结果是一样的--我得到了。为什么?我怎样才能使它在这种特定的

  • 问题内容: 我需要设置Servlet(由于某些原因,只有Servlet而不是处理程序)才能在war之外使用文件。在这里我找到了以下解决方案: 此解决方案有效,这就是我需要的。但是,一旦将LINE N更改为,它就会停止工作。我尝试了“ / foo /”,“ / foo”,但是结果是相同的-我得到了。为什么?如何在特定背景下使用它?由于相同的原因,我使用码头9.2.15。 问题答案: 该设计看后请求U

  • 我正在使用Jetty的Spring靴。我配置上下文路径: 访问时http://localhost:8080/test,它不起作用。但是去http://localhost:8080/test/作品 是/测试和/测试/不同吗?我如何访问http://localhost:8080/test

  • 本文向大家介绍java下载url路径包含中文需要转义的操作,包括了java下载url路径包含中文需要转义的操作的使用技巧和注意事项,需要的朋友参考一下 大家看这个url: http://127.0.0.1:9997/file/江苏省地震应急预案(二级).docx 应为url路径中有中文没有转义,输入流报错 The valid characters are defined in RFC 7230 a

  • 我正在尝试使用sping-boot-starter-webflow和reactive Netty创建sping-boot-2 REST api。我正在尝试根据要在Spring-Boot-2中定义的application.yml中定义的新属性设置上下文路径。 然而,它看起来像网络流量,Netty不使用/识别application.yml.中定义的这个属性 如果我使用spring boot start