Я запускаю весеннее приложение mvc с классами, аннотированными как @ controller, @ service, @ component и для уровня представления используется тимилеф. Однако при переходе к пути localhost: 8080 / owners данные о владельце не отображаются. Выглядит так, как будто данные сохраняются в объекте владельца, но ownerservicemap пусто без причины.
Ниже приведена ошибка
Owner data is loaded
2019-06-26 12:39:47.237 INFO 5776 --- [ restartedMain]
.ConditionEvaluationDeltaLoggingListener : Condition evaluation
unchanged
2019-06-26 12:39:50.475 INFO 5776 --- [nio-8080-exec-1]
o.a.c.c.C.[Tomcat-1].[localhost].[/] : Initializing Spring
DispatcherServlet 'dispatcherServlet'
2019-06-26 12:39:50.475 INFO 5776 --- [nio-8080-exec-1]
o.s.web.servlet.DispatcherServlet : Initializing Servlet
'dispatcherServlet'
2019-06-26 12:39:50.483 INFO 5776 --- [nio-8080-exec-1]
o.s.web.servlet.DispatcherServlet : Completed initialization in
8 ms
2019-06-26 12:39:50.507 ERROR 5776 --- [nio-8080-exec-1]
o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for
servlet [dispatcherServlet] in context with path [] threw exception
[Request processing failed; nested exception is
java.lang.NullPointerException] with root cause
java.lang.NullPointerException: null
at com.example.Project.controllers.OwnerController.listOwners(OwnerController.java:33)
~[classes/:na]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_211]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
~[na:1.8.0_211]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
~[na:1.8.0_211]
at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_211]
at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:190)
Класс DataLoader для загрузки данных
@Component public class DataLoader implements CommandLineRunner{
private final OwnerService ownerService;
public DataLoader()
{
ownerService=new OwnerServiceMap();
}
@Override
public void run(String... args) throws Exception {
// TODO Auto-generated method stub
Owner owner1=new Owner();
owner1.setId(1L);
owner1.setFirstName("ally");
owner1.setLastName("nilson");
Owner sa1=ownerService.save(owner1);
}}
Ниже приведен класс владельца контроллера
@RequestMapping("/owners") @Controller public class OwnerController {
private OwnerService ownerservice;
@Autowired
public OwnerController(OwnerService ownerservice)
{
this.ownerservice=ownerservice;
}
@RequestMapping({"","/","/index","/index.html"})
public String listOwners(Model model)
{
model.addAttribute("owner",ownerservice.findAll());// System.out.println(ownerservice.findById(1L).getLastName());
return "owner/index";
}}
Интерфейс OwnerService
public interface OwnerService extends CrudService<Owner, Long>{
Owner findByLastName(String lastname);}
AbstractmapService class
public abstract class AbstractMapService<T,ID> {
protected Map<ID,T> map=new HashMap<>();
Set<T> findAll()
{
return new HashSet<>(map.values());
}
T findById(ID id)
{
return map.get(id);
}
T save(ID id,T object)
{
map.put(id, object);
return object;
}
void deleteById(ID id)
{
map.remove(id);
}
void delete(T object)
{
map.entrySet().removeIf(entry->entry.getValue().equals(object));
}}
Класс OwnerServiceMap
@Service public class OwnerServiceMap extends AbstractMapService<Owner,Long>implements OwnerService{
@Override
public Set<Owner> findAll() {
// TODO Auto-generated method stub
return super.findAll();
}
@Override
public Owner findById(Long id) {
// TODO Auto-generated method stub
return super.findById(id);
}
@Override
public Owner save(Owner object) {
// TODO Auto-generated method stub
return super.save(object.getId(),object);
}
@Override
public void delete(Owner object) {
// TODO Auto-generated method stub
super.delete(object);
}
@Override
public void deleteById(Long id) {
// TODO Auto-generated method stub
super.deleteById(id);
}
@Override
public Owner findByLastName(String lastname) {
// TODO Auto-generated method stub
return null;
}
} * * тысяча двадцать-один