Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ClassNotFoundException: org.springframework.hateoas.Resource even though I have the dependency added

I have the following in my pom.xml

<dependency>
    <groupId>org.springframework.hateoas</groupId>
    <artifactId>spring-hateoas</artifactId>
</dependency>

But I still get the following message

Caused by: java.lang.NoClassDefFoundError: org/springframework/hateoas/Resource
    at java.lang.Class.getDeclaredMethods0(Native Method) ~[na:1.8.0_191]
    at java.lang.Class.privateGetDeclaredMethods(Class.java:2701) ~[na:1.8.0_191]
    at java.lang.Class.getDeclaredMethods(Class.java:1975) ~[na:1.8.0_191]
    at org.springframework.util.ReflectionUtils.getDeclaredMethods(ReflectionUtils.java:668) ~[spring-core-5.1.5.RELEASE.jar:5.1.5.RELEASE]
    ... 21 common frames omitted
Caused by: java.lang.ClassNotFoundException: org.springframework.hateoas.Resource
    at java.net.URLClassLoader.findClass(URLClassLoader.java:382) ~[na:1.8.0_191]
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424) ~[na:1.8.0_191]
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:349) ~[na:1.8.0_191]
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357) ~[na:1.8.0_191]
    ... 25 common frames omitted

I tried deleting the contents of the .m2 folder and downloading all the dependencies again. I tried force updating the maven project, but I still get the message when I start the Spring Boot project. Anyone know why I'm getting this even though I have added the dependency?

like image 526
Arya Avatar asked May 07 '19 05:05

Arya


2 Answers

For Spring Boot use spring-boot-starter-hateoas dependency

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-hateoas</artifactId>
  <version>2.1.4.RELEASE</version>
</dependency>
like image 190
user7294900 Avatar answered Sep 19 '22 12:09

user7294900


Add following dependency in pom.xml

       <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-hateoas</artifactId>
        <version>2.2.6.RELEASE</version>
       </dependency>

Write following method in your controller:-

@GetMapping("/student/{id}")
public EntityModel<Student> getstudent(@PathVariable int id) {
    Student student = studentRepo.findById(id).orElse(new Student());
    EntityModel<Student> resource=new EntityModel<Student>(student);
    Link link=WebMvcLinkBuilder.linkTo(this.getClass()).
              slash(studentRepo.findAll()).withSelfRel();
    resource.add(link.withRel("all-students"));
    return resource;
}
like image 23
Leena Varshney Avatar answered Sep 20 '22 12:09

Leena Varshney