尝试访问spring启动应用程序中的jpa响应时,java.lang.nullpointerexception出现间隔服务器错误

2vuwiymt  于 2021-07-06  发布在  Java
关注(0)|答案(0)|浏览(163)

我对一个问题摸不着头脑,找不到任何解决办法。我对springboot还比较陌生,正在开发一个关于它的小项目。我的应用程序只有两个表。我有一张table叫 product_metrics 表的jpa实体如下所示-

@Entity
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@Table(name = "product_metrics")
public class ProductMetric extends Auditable<String> {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

@NotNull
@Column(name = "product_id", unique = true)
private Long productId;

@Column(name = "bundle_quantity")
private BigDecimal bundleQuantity;

@Column(name = "length_in_cm")
private BigDecimal lengthInCm;

@Column(name = "breadth_in_cm")
private BigDecimal breadthInCm;

@Column(name = "height_in_cm")
private BigDecimal heightInCm;

}
现在的问题是我想获取数组中给定产品id的所有产品。所以我决定使用post-api调用,因为如果使用get-api发送大量产品标识,我不想突破url限制。我所面临的问题是,一旦我的jpa存储库被击中,我就会得到内部服务器错误。
我的控制器是-

@RestController
@RequestMapping("/")
public class ProductMetricController extends BaseController {

@Autowired
private ProductDetailService productDetailService;

@PostMapping("/product-details")
public ResponseEntity<Map<String, Object>> getProductDetails(@RequestBody Map<String, List> requestParam) {
    Map<String, Object> responseMapper = new HashMap<>();

    try {
        if (requestParam.containsKey("product_ids")) {
            responseMapper = productDetailService.fetchProductList(requestParam);
        }

        return new ResponseEntity<>(responseMapper, HttpStatus.OK);
    } catch (Exception e) {
        responseMapper.put("message", e.getMessage());
        return new ResponseEntity<>(responseMapper, e.getClass().getAnnotation(ResponseStatus.class).value());
    }

}
}

我的服务是-

@Service
public class ProductDetailServiceImpl implements ProductDetailService{

@Autowired
private ProductMetricRepository productMetricRepository;

public  Map<String, Object> fetchProductList(Map<String, List>  requestBody) {

    Map<String, Object> responseMapper = new HashMap<>();
    List<Long> productIds = requestBody.get("product_ids");

    Optional<List<ProductMetric>> productList = productMetricRepository.findAllByProductIdIn(productIds);
    if ( productList.isPresent() ) {
        List<ProductMetric> productMetricsList =  productList.get();
        for (ProductMetric metrics : productMetricsList) {
            responseMapper.put(metrics.getId().toString(), metrics);
        }
        return responseMapper;
    } throw new ResourceNotFoundException(ExceptionMsgs.PRODUCT_LIST_NOT_FOUND);
}
}

我的jpa报告是-

@Repository
   public interface ProductMetricRepository extends JpaRepository<ProductMetric, Long> {

   Optional<List<ProductMetric>> findAllByProductIdIn(List<Long> productIds);
}

我正在用请求体点击post请求-

{
   "product_ids": [
       12345,
       66052
   ]
}

我所面临的问题是,我在java.lang.nullpointerexception中遇到了内部服务器错误-

java.lang.NullPointerException\n\tat
    com.bizongo.logistics.controllers.ProductMetricController.getProductDetails(ProductMetricController.java:32)\n\tat
    java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native
    Method)\n\tat
    java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)\n\tat
    java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)\n\tat
    java.base/java.lang.reflect.Method.invoke(Method.java:566)\n\tat
    org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:190)\n\tat
    org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:138)\n\tat
    org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:106)\n\tat
    org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:888)\n\tat
    org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:793)\n\tat
    org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87)\n\tat
    org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1040)\n\tat
    org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:943)\n\tat
    org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1006)\n\tat
    org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:909)\n\tat
    javax.servlet.http.HttpServlet.service(HttpServlet.java:660)\n\tat
    org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:883)\n\tat
    javax.servlet.http.HttpServlet.service(HttpServlet.java:741)\n\tat
    org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)\n\tat
    org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)\n\tat
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)\n\tat
    org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)\n\tat
    org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)\n\tat
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:320)\n\tat
    org.springframework.security.web.access.ExceptionTranslationFilter.doFilter(ExceptionTranslationFilter.java:118)\n\tat
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)\n\tat
    org.springframework.security.web.session.SessionManagementFilter.doFilter(SessionManagementFilter.java:137)\n\tat
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)\n\tat
    org.springframework.security.web.authentication.AnonymousAuthenticationFilter.doFilter(AnonymousAuthenticationFilter.java:111)\n\tat
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)\n\tat
    org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter.doFilter(SecurityContextHolderAwareRequestFilter.java:158)\n\tat
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)\n\tat
    org.springframework.security.web.savedrequest.RequestCacheAwareFilter.doFilter(RequestCacheAwareFilter.java:63)\n\tat
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)\n\tat
    org.springframework.security.web.authentication.www.BasicAuthenticationFilter.doFilterInternal(BasicAuthenticationFilter.java:154)\n\tat
    org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119)\n\tat
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)\n\tat
    org.springframework.security.web.authentication.logout.LogoutFilter.doFilter(LogoutFilter.java:116)\n\tat
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)\n\tat
    org.springframework.security.web.header.HeaderWriterFilter.doHeadersAfter(HeaderWriterFilter.java:92)\n\tat
    org.springframework.security.web.header.HeaderWriterFilter.doFilterInternal(HeaderWriterFilter.java:77)\n\tat
    org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119)\n\tat
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)\n\tat
    org.springframework.security.web.context.SecurityContextPersistenceFilter.doFilter(SecurityContextPersistenceFilter.java:105)\n\tat
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)\n\tat
    org.springframework.security.web.context.request.async.WebAsyncManagerIntegrationFilter.doFilterInternal(WebAsyncManagerIntegrationFilter.java:56)\n\tat
    org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119)\n\tat
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)\n\tat
    org.springframework.security.web.FilterChainProxy.doFilterInternal(FilterChainProxy.java:215)\n\tat
    org.springframework.security.web.FilterChainProxy.doFilter(FilterChainProxy.java:178)\n\tat
    org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:358)\n\tat
    org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:271)\n\tat
    org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)\n\tat
    org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)\n\tat
    org.springframework.boot.actuate.metrics.web.servlet.WebMvcMetricsFilter.doFilterInternal(WebMvcMetricsFilter.java:108)\n\tat
    org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119)\n\tat
    org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)\n\tat
    org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)\n\tat
    org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:201)\n\tat
    org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119)\n\tat
    org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)\n\tat
    org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)\n\tat
    org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:202)\n\tat
    org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)\n\tat
    org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:541)\n\tat
    org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:139)\n\tat
    org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92)\n\tat
    org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:74)\n\tat
    org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343)\n\tat
    org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:367)\n\tat
    org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:65)\n\tat
    org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:860)\n\tat
    org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1598)\n\tat
    org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)\n\tat
    java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)\n\tat
    java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)\n\tat
    org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)\n\tat
    java.base/java.lang.Thread.run(Thread.java:834)\n"

如果你们能帮我,那就太好了。对于我本该犯的任何愚蠢的错误,我深表歉意,并提前表示感谢:)

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题