Java 在Spring boot/Crawler4j应用程序中,假客户端总是抛出空指针异常
我在Spring引导应用程序中运行一个Crawler4j实例,我的OpenFeign客户端总是空的Java 在Spring boot/Crawler4j应用程序中,假客户端总是抛出空指针异常,java,spring,crawler4j,openfeign,Java,Spring,Crawler4j,Openfeign,我在Spring引导应用程序中运行一个Crawler4j实例,我的OpenFeign客户端总是空的 public class MyCrawler extends WebCrawler { @Autowired HubClient hubClient; @Override public void visit(Page page) { // Lots of crawler code... if (page.getParseData() instanceof
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
}
}
我丈夫的客户
@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将抛出空指针 为了将SpringBean(您的客户机)注入到您的Crawler4jWebCrawler对象中,您需要通过Spring实例化WebCrawler对象
为此,您需要编写的自定义实现,它提供/创建Spring管理的Web爬虫对象。为此,您的Web爬虫实现需要是一个Springbean,即至少要用
@Component
注释 你好,你能添加stacktrace吗?@MykhailoMoskura-对不起,是的。刚刚做过。当您注入hubClient或网页时,它是否为null。toString()返回null?hubClient为null。您是否在spring中将MyCrawler注册为bean?