[英]How to initialize web-service
I'm trying to create a simple web-page, which is displaying the data, received from web-service 我正在尝试创建一个简单的网页,该网页显示从Web服务接收的数据
@Service
@Transactional
public class TopicService {
@Autowired
private TopicRepository topicRepository;
public int saveTopic(Topic topic){
return topicRepository.save(topic).getId();
}
public Iterable<Topic> findAllTopics(){
return topicRepository.findAll();
}
public Topic findTopicByID(Long id){
return topicRepository.findOne(id);
}
public List<Topic> findTopicsByTag(Tag tag){
return topicRepository.findAllByTopicTag(tag);
}
}
topic repository extends CRUD-repository 主题存储库扩展了CRUD存储库
@Repository
public interface TopicRepository extends CrudRepository<Topic, Long>
{
List<Topic> findAllByTopicTag(Tag currentTag);
}
the controller invokes a service in the following way 控制器通过以下方式调用服务
@Controller
public class HomeController {
private TopicService service;
private static final Logger logger = LoggerFactory.getLogger(HomeController.class);
@RequestMapping(value = "/", method = RequestMethod.GET)
public ModelAndView home(Locale locale, Model model)
{
Iterable<Topic> listTopic = service.findAllTopics();
return new ModelAndView("home", "model", listTopic);
}
}
this string 这个字符串
Iterable listTopic = service.findAllTopics(); 可迭代的listTopic = service.findAllTopics();
throws null-pointer exception. 抛出空指针异常。 I guess, because the service isn't initialized. 我猜是因为服务没有初始化。 How could I perform correct initialization of the service? 如何执行服务的正确初始化?
You need to autowire the TopicService
in HomeController
: 您需要在HomeController
自动连接TopicService
:
@Controller
public class HomeController {
@Aurowired
private TopicService service;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.