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?
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>
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;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With