2
回答
pipLine 中的 process 遇到一个空指针异常
华为云实践训练营,热门技术免费实践!>>>   

@黄亿华 你好,想跟你请教个问题:

@RestController
@RequestMapping("/magic")
public class WebMagicController implements Pipeline{
	@Autowired
	private StudentService	studentService;

	@ResponseBody
	@RequestMapping("/find/{id}")
	public Student find(@PathVariable("id") Long id) {
		Student student = studentService.get(id);
		return student;
	}
	
	@RequestMapping("/run")
	public void run() {
		testSave();//输出 false
		Spider.create(new QiuBaiPageProcessor()).addUrl("http://www.qiushibaike.com/textnew/")
		// 开启5个线程抓取
				.thread(5).addPipeline(new WebMagicController())
				// 启动爬虫
				.run();
	}

	@Override
	public void process(ResultItems resultItems, Task task) {
		List<String> ht_l = resultItems.get("list");
		saveStudent(ht_l);//输出为 true,报出 NullPointerException;
	}
	public void testSave(){
		System.err.println(studentService==null);
	}
	public void saveStudent(List<String> ht_l ) {
		System.err.println(studentService==null);
		List<Student> lists=new ArrayList<Student>();
		for (String string : ht_l) {
			Student student=new Student();
			student.setName(string);
			lists.add(student);
		}
		studentService.saveAll(lists);//这里的 studentService 为空
	}
}



唯独 process 的  XXXservice 是空的

举报
_简简单单_
发帖于2年前 2回/79阅
顶部