使用构造函数注入的弹簧@RestController中的空异常@Service
Posted
技术标签:
【中文标题】使用构造函数注入的弹簧@RestController中的空异常@Service【英文标题】:Null exception @Service in a spring @RestController using constructor injection 【发布时间】:2022-01-05 14:42:03 【问题描述】:这是我的第三个个人 spring-boot 项目,我的配置与以前相比 99% 相同,所以不知道为什么会在这个项目上出现此错误。
项目启动得很好,但是一旦我对我拥有的唯一 @RestController 进行任何请愿,它会立即收到 null 错误。这个端点接收一个 alpha 2 国家代码 + 城市名称 + 所需的 IP 地址,如果没有提供 IP 地址,它会使用客户端 IP 地址,该地址用作 @Service 我从中获取 null 的参数。之后,控制器应该在将预期输出发送到客户端之前执行许多其他操作,但由于此 @Service 上的 null 异常,它从未到达该点。
这是我正在尝试的请愿书:
http://localhost:8080/apiv1/get-distance?country=af&city=Qalat&ip=150.135.184.38
There was an unexpected error (type=Internal Server Error, status=500).
Cannot invoke "me.givo.distancebetweenus.getdistance.services.CurrentLocationService.getCurrentLocation(String)" because "this.currentLocationService" is null
java.lang.NullPointerException: Cannot invoke "me.givo.distancebetweenus.getdistance.services.CurrentLocationService.getCurrentLocation(String)" because "this.currentLocationService" is null
at me.givo.distancebetweenus.getdistance.controllers.DistanceController.getDistance(DistanceController.java:53)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:568)
at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:205)
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:150)
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:117)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:895)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:808)
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1067)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:963)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1006)
at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:898)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:655)
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:883)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:764)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:227)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:189)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162)
at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:100)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:189)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162)
at org.springframework.web.filter.FormContentFilter.doFilterInternal(FormContentFilter.java:93)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:189)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162)
at org.springframework.boot.actuate.metrics.web.servlet.WebMvcMetricsFilter.doFilterInternal(WebMvcMetricsFilter.java:96)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:189)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162)
at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:201)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:189)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:197)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:97)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:540)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:135)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:78)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:357)
at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:382)
at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:65)
at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:895)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1722)
at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
at org.apache.tomcat.util.threads.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1191)
at org.apache.tomcat.util.threads.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:659)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.base/java.lang.Thread.run(Thread.java:833)
这是我的控制器:
package me.givo.distancebetweenus.getdistance.controllers;
import javax.servlet.http.HttpServletRequest;
import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import me.givo.distancebetweenus.getdistance.dtos.CityDto;
import me.givo.distancebetweenus.getdistance.dtos.CountryDto;
import me.givo.distancebetweenus.getdistance.entities.CityEntity;
import me.givo.distancebetweenus.getdistance.entities.CountryEntity;
import me.givo.distancebetweenus.getdistance.models.ApiCurrentLocationResponse;
import me.givo.distancebetweenus.getdistance.models.Coordinate;
import me.givo.distancebetweenus.getdistance.models.DistanceResponse;
import me.givo.distancebetweenus.getdistance.repositories.ICitiesRepository;
import me.givo.distancebetweenus.getdistance.repositories.ICountriesRepository;
import me.givo.distancebetweenus.getdistance.services.CurrentLocationService;
import org.modelmapper.ModelMapper;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
@RequestMapping("/apiv1")
@Validated
@RestController
public class DistanceController
private final CurrentLocationService currentLocationService;
private final ICitiesRepository citiesRepository;
private final ICountriesRepository countriesRepository;
private final ModelMapper modelMapper;
public DistanceController(CurrentLocationService currentLocationService, ICitiesRepository citiesRepository,
ICountriesRepository countryRepository, ModelMapper modelMapper)
this.currentLocationService = currentLocationService;
this.citiesRepository = citiesRepository;
this.countriesRepository = countryRepository;
this.modelMapper = modelMapper;
@GetMapping("/get-distance")
@ResponseBody
private ResponseEntity<Object> getDistance(
@RequestParam(required = true, name = "country") @Min(2) @Max(2) String country,
@RequestParam(required = true, name = "city") String city,
@RequestParam(required = false, name = "ip") @Min(7) @Max(15) String ip,
HttpServletRequest request)
ApiCurrentLocationResponse currentLocation;
if (ip == null || ip.isEmpty())
String requestIP = request.getRemoteAddr();
System.out.println(requestIP);
try
currentLocation = currentLocationService.getCurrentLocation(requestIP);
catch (Exception e)
System.out.println(e.getMessage());
else
try
currentLocation = currentLocationService.getCurrentLocation(ip);
catch (Exception e)
System.out.println(e.getMessage());
CountryEntity countryEntity = countriesRepository.getById(country);
CityEntity cityEntity = citiesRepository.getByNameAndCode(city, country);
CountryDto countryDto = convertToCountryDto(countryEntity);
CityDto cityDto = convertToCityDto(cityEntity);
Coordinate currentLocationCoordinate = new Coordinate(currentLocation);
Coordinate destinationCoordinate = new Coordinate(cityDto);
Double distance = currentLocationCoordinate.distanceBetweenUsKm(destinationCoordinate);
DistanceResponse response = new DistanceResponse(countryDto, cityDto, distance);
return ResponseEntity.status(HttpStatus.OK).body(response);
private CityDto convertToCityDto(CityEntity cityEntity)
return modelMapper.map(cityEntity, CityDto.class);
private CountryDto convertToCountryDto(CountryEntity countryEntity)
return modelMapper.map(countryEntity, CountryDto.class);
这是“空”@Service:
package me.givo.distancebetweenus.getdistance.services;
import me.givo.distancebetweenus.getdistance.models.ApiCurrentLocationResponse;
import org.springframework.stereotype.Service;
import org.springframework.web.reactive.function.client.WebClient;
@Service
public class CurrentLocationService
private final WebClient.Builder webClient;
private final String key = "fakekey347834834892923fake";
public CurrentLocationService(WebClient.Builder webClient)
this.webClient = webClient;
public ApiCurrentLocationResponse getCurrentLocation(String requestIP)
String url = "https://api.fakeapi.app/json/";
return webClient.baseUrl(url).build().get()
.uri(uriBuilder -> uriBuilder
.path(requestIP)
.queryParam("apikey", key)
.build())
.retrieve()
.bodyToMono(ApiCurrentLocationResponse.class)
.block();
非常感谢您的帮助。如果需要任何其他信息,请告诉我。谢谢! - - - - - - - - 编辑 - - - - - - - - - - shivam 的评论让我想到了用 @Autowired 我的注射进行标记的想法,所以我做到了,我不再在 @Service 中得到空异常,它在 ICountriesRepository 而不是我的 @RestController 中的下一行代码.. 这些是我所做的更改:
public class DistanceController
private final CurrentLocationService currentLocationService;
private final ICitiesRepository citiesRepository;
private final ICountriesRepository countriesRepository;
private final ModelMapper modelMapper;
@Autowired
public DistanceController(CurrentLocationService currentLocationService, ICitiesRepository citiesRepository,
ICountriesRepository countriesRepository, ModelMapper modelMapper)
this.currentLocationService = currentLocationService;
this.citiesRepository = citiesRepository;
this.countriesRepository = countriesRepository;
this.modelMapper = modelMapper;
这是“新”错误日志:
There was an unexpected error (type=Internal Server Error, status=500).
Cannot invoke "me.givo.distancebetweenus.getdistance.repositories.ICountriesRepository.getById(Object)" because "this.countriesRepository" is null
java.lang.NullPointerException: Cannot invoke "me.givo.distancebetweenus.getdistance.repositories.ICountriesRepository.getById(Object)" because "this.countriesRepository" is null
at me.givo.distancebetweenus.getdistance.controllers.DistanceController.getDistance(DistanceController.java:72)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:568)
at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:205)
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:150)
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:117)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:895)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:808)
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1067)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:963)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1006)
at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:898)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:655)
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:883)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:764)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:227)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:189)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162)
at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:100)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:189)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162)
at org.springframework.web.filter.FormContentFilter.doFilterInternal(FormContentFilter.java:93)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:189)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162)
at org.springframework.boot.actuate.metrics.web.servlet.WebMvcMetricsFilter.doFilterInternal(WebMvcMetricsFilter.java:96)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:189)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162)
at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:201)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:189)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:197)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:97)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:540)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:135)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:78)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:357)
at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:382)
at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:65)
at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:895)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1722)
at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
at org.apache.tomcat.util.threads.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1191)
at org.apache.tomcat.util.threads.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:659)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.base/java.lang.Thread.run(Thread.java:833)
有ICountriesRepository的代码:
package me.givo.distancebetweenus.getdistance.repositories;
import me.givo.distancebetweenus.getdistance.entities.CountryEntity;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface ICountriesRepository extends JpaRepository<CountryEntity, String>
另外,我正在添加我为 ModelMapper 制作的 @Configuration 代码:
package me.givo.distancebetweenus.getdistance.config;
import org.modelmapper.ModelMapper;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class Config
@Bean
public ModelMapper modelMapper()
return new ModelMapper();
【问题讨论】:
您是否将 CurrentLocationService 自动连接到 DistanceController 中? 你好,希瓦姆。我没有,因为我在控制器构造函数中注入,但我也尝试添加 Autowired,现在我在控制器使用的下一个存储库中得到相同的空错误。我将更新最初的问题。谢谢。 依赖注入发生在你启动应用程序时。那时你有什么例外吗? 尝试使用@Autowired 注释构造函数。 Spring(从 4.3 开始)默认自动装配 ONLY 构造函数,有没有你没有向我们展示的构造函数? 现在你已经把那里弄得一团糟,请把@Autowire
留在构造函数上。请显示使用此类/接口ICountriesRepository
。
【参考方案1】:
我在工作日有点忙,所以直到现在才能够查看我的代码,我发现我的控制器唯一拥有的 GetMapping 方法是 private。我刚刚将其 公开 并且所有操作都已按预期运行。感谢大家的宝贵时间。
【讨论】:
以上是关于使用构造函数注入的弹簧@RestController中的空异常@Service的主要内容,如果未能解决你的问题,请参考以下文章
在身份验证过滤器中添加自定义声明。在过滤器中获取用户 ID。弹簧靴