我在 Spring Boot 应用程序中运行 Crawler4j 实例,而我的 OpenFeign 客户端始终为空。
public class MyCrawler extends WebCrawler {
@Autowired
HubClient hubClient;
@Override
public void visit(Page page) {
// Lots of crawler code...
if (page.getParseData() instanceof HtmlParseData) {
hubClient.send(webPage.toString()); // Throws null pointer exception
}
}
我的 Hub 客户端
@FeignClient("hub-worker")
public interface HubClient {
@RequestMapping(method = RequestMethod.POST, value = "/pages", consumes = "application/json")
void send(String webPage);
//void createPage(WebPage webPage);
}
我的主要应用程序
@EnableEurekaClient
@EnableFeignClients
@SpringBootApplication
public class CrawlerApplication {
public static void main(String[] args) throws Exception {
SpringApplication.run(CrawlerApplication.class, args);
}
}
堆栈跟踪
ext length: 89106
Html length: 1048334
Number of outgoing links: 158
10:14:38.634 [Crawler 164] WARN e.u.ics.crawler4j.crawler.WebCrawler - Unhandled exception while fetching https://www.cnn.com: null
10:14:38.634 [Crawler 164] INFO e.u.ics.crawler4j.crawler.WebCrawler - Stacktrace:
java.lang.NullPointerException: null
at com.phishspider.crawler.MyCrawler.visit(MyCrawler.java:79)
at edu.uci.ics.crawler4j.crawler.WebCrawler.processPage(WebCrawler.java:523)
at edu.uci.ics.crawler4j.crawler.WebCrawler.run(WebCrawler.java:306)
at java.base/java.lang.Thread.run(Thread.java:834)
第 79 行是 hubClient 调用。当我将 hubVlient 分解到另一个类中时,我在爬虫类中实例化了 hubclient hc = new hubclient() 然后有一些方法 hc.send(page) 那个分解出来的类中的 hubClient 将抛出空指针。