我有一个由Gradle管理的多个Spring微服务应用程序。有一个常见的jar和特定于域的jar,其组织方式如下:
| common/
|-- common.jar
| p/
|-- p-ingest.jar
| g/
|-- g-ingest.jar我的普通jar有一个类BaseRESTService,它自动生成了HttpServletRequest (我已经知道这是个坏主意,而不是我的代码)。p-ingest.jar和g-inest.jar一样,都是普通的。geoalloc运行没有问题。但是p-g,实际上是从g-ingest复制过来的,它没有运行.当上下文试图初始化时,P- gets将获得以下异常:
Field request in com.company.common.BaseRESTService required a bean of type 'javax.servlet.http.HttpServletRequest' that could not be found.我对Spring并不陌生,我理解组件扫描和自动装配等等,但是我已经花了两天的时间来完成这项工作,我不知道发生了什么。我在这两个项目上运行了gradle dependencies,而且树是相同的。下面是p-ingest的引导应用程序类:
@SpringBootApplication
@ComponentScan(
basePackageClasses = com.company.common.ConfigurationSearchCriteriaFactory.class,
basePackages = {"com.company.erd"})
@EnableJpaRepositories(basePackages = "com.company.erd")
@EntityScan(basePackages = {"com.company.erd"})
public class PortfolioRiskIngestApplication implements ApplicationRunner {
private static final Log logger = LogFactory.getLog(IngestApplication.class);
@Autowired
private IngestService ingestService;
public PIngestApplication(PIngestService ingestService) {this.ingestService = ingestService;}
public static void main(String[] args) {
SpringProfileHelper.setProfileFromAwsEnvironment();
SpringApplication app = new SpringApplication(PIngestApplication.class);
app.setWebEnvironment(false);
app.run(args);
}
@PostConstruct
void started() {
TimeZone.setDefault(TimeZone.getTimeZone("UTC"));
}
@Override
public void run(ApplicationArguments applicationArguments) {
// define the log info
LogInfo info = LogInfo.newInstance("run", new Object[] { StringUtility.arrayToString(applicationArguments.getSourceArgs()) });
// log entry
logger.info(info.entry());
ingestService.run(applicationArguments.getSourceArgs());
// log exit
logger.info(info.exit());
}
}以下是BaseRestService的摘录
public class BaseRESTService
{
private static final Log logger = LogFactory.getLog(BaseRESTService.class);
@Autowired
private HttpServletRequest request;
@ResponseStatus(HttpStatus.NOT_FOUND)
@ExceptionHandler(value = EntityNotFoundException.class)
public ErrorResponse handleEntityNotFoundException(EntityNotFoundException e){
ErrorResponse errorResponse = new ErrorResponse();
errorResponse.setErrorCode(HttpStatus.NOT_FOUND.value());
errorResponse.setErrorMessage(e.getMessage());
logger.error(e.getMessage(), e);
return errorResponse;
} 没有什么特别事情。所有涉及的类都很简单。我欢迎任何人对我做错事的想法.
发布于 2019-10-15 12:54:49
我解决了我的问题,这是菜鸟的错误。出于懒惰,我对整个代码库进行了一次组件扫描,其中提取了我们在普通jar中仅用于REST应用程序的控制器。组件扫描只有实际需要的包工作。
发布于 2019-10-15 05:44:50
您可以使用HttpServletRequest使用RequestContext
@Inject
private RequestContext requestContext;并在方法中调用getRequest():
HttpServletRequest request = requestContext.getRequest();https://stackoverflow.com/questions/58331760
复制相似问题