This is BlogController
:
package com.taskproject.blogproject.controller;
import com.taskproject.blogproject.entities.Blog;
import com.taskproject.blogproject.repositories.BlogRepository;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@RestController
@Service
@RequestMapping("/blogs")
public class BlogController {
private final BlogRepository blogRepository;
public BlogController(BlogRepository blogRepository) {
this.blogRepository = blogRepository;
}
@PostMapping
public ResponseEntity<Blog> createBlog(@RequestBody Blog blog) {
Blog savedBlog = blogRepository.save(blog);
return ResponseEntity.ok(savedBlog);
}
@GetMapping
public ResponseEntity<List<Blog>> getAllBlogs() {
List<Blog> blogs = blogRepository.findAll();
return ResponseEntity.ok(blogs);
}
@GetMapping("/{id}")
public ResponseEntity<Blog> getBlogById(@PathVariable Long id) {
return blogRepository.findById(id)
.map(ResponseEntity::ok)
.orElse(ResponseEntity.notFound().build());
}
@PutMapping("/{id}")
public ResponseEntity<Blog> updateBlog(@PathVariable Long id, @RequestBody Blog updatedBlog) {
return blogRepository.findById(id)
.map(blog -> {
blog.setTitle(updatedBlog.getTitle());
blog.setDescription(updatedBlog.getDescription());
Blog savedBlog = blogRepository.save(blog);
return ResponseEntity.ok(savedBlog);
})
.orElse(ResponseEntity.notFound().build());
}
@DeleteMapping("/{id}")
public ResponseEntity<Void> deleteBlog(@PathVariable Long id) {
return blogRepository.findById(id)
.map(blog -> {
blogRepository.delete(blog);
return ResponseEntity.ok().<Void>build();
})
.orElse(ResponseEntity.notFound().build());
}
}
This is pom.xml
:
\<?xml version="1.0" encoding="UTF-8"?\> \<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"\> \<modelVersion\>4.0.0\</modelVersion\> \<parent\> \<groupId\>org.springframework.boot\</groupId\> \<artifactId\>spring-boot-starter-parent\</artifactId\> \<version\>3.2.3\</version\> \<relativePath/\> \<!-- lookup parent from repository --\> \</parent\> \<groupId\>com.taskproject\</groupId\> \<artifactId\>BlogProject\</artifactId\> \<version\>0.0.1-SNAPSHOT\</version\> \<name\>BlogProject\</name\> \<description\>BlogProject\</description\> \<properties\> \<java.version\>17\</java.version\> \</properties\> \<dependencies\> \<dependency\> \<groupId\>org.springframework.boot\</groupId\> \<artifactId\>spring-boot-starter-data-jpa\</artifactId\> \</dependency\> \<dependency\> \<groupId\>org.springframework.boot\</groupId\> \<artifactId\>spring-boot-starter-web\</artifactId\> \</dependency\> \<dependency\> \<artifactId\>h2\</artifactId\> \<scope\>runtime\</scope\> \</dependency\> \<dependency\> \<groupId\>org.springframework.boot\</groupId\> \<artifactId\>spring-boot-starter-test\</artifactId\> \<scope\>test\</scope\> \</dependency\> \</dependencies\> \<build\> \<plugins\> \<plugin\> \<groupId\>org.springframework.boot\</groupId\> \<artifactId\>spring-boot-maven-plugin\</artifactId\> \</plugin\> \</plugins\> \</build\> \</project\>
##
Here is application.properties
:
spring.datasource.url=jdbc:mysql://localhost:3306/blogs
spring.datasource.username=root
spring.datasource.password=p@sssw0rd
spring.datasource.driver-class-name=com.taskproject.blogproject
spring.jpa.hibernate.ddl-auto=update
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL8Dialect
server.port=8080
spring.security.user.name=admin
spring.security.user.password=adminpassword
Here is exception description:
java.lang.ClassNotFoundException: com.taskproject.blogproject
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:641)
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:188)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:526)
at java.base/java.lang.Class.forName0(Native Method)
at java.base/java.lang.Class.forName(Class.java:534)
at java.base/java.lang.Class.forName(Class.java:513)
at org.springframework.util.ClassUtils.forName(ClassUtils.java:304)
at org.springframework.boot.autoconfigure.jdbc.DataSourceProperties.driverClassIsLoadable(DataSourceProperties.java:194)
at org.springframework.boot.autoconfigure.jdbc.DataSourceProperties.determineDriverClassName(DataSourceProperties.java:175)
at org.springframework.boot.autoconfigure.jdbc.PropertiesJdbcConnectionDetails.getDriverClassName(PropertiesJdbcConnectionDetails.java:49)
at org.springframework.boot.autoconfigure.jdbc.DataSourceConfiguration.createDataSource(DataSourceConfiguration.java:55)
at org.springframework.boot.autoconfigure.jdbc.DataSourceConfiguration$Hikari.dataSource(DataSourceConfiguration.java:117)
at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:103)
at java.base/java.lang.reflect.Method.invoke(Method.java:580)
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:140)
at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:647)
at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:639)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1335)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1165)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:562)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:522)
at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:325)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:323)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199)
at org.springframework.beans.factory.config.DependencyDescriptor.resolveCandidate(DependencyDescriptor.java:254)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1443)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1353)
at org.springframework.beans.factory.support.ConstructorResolver.resolveAutowiredArgument(ConstructorResolver.java:907)
at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:785)
at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:542)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1335)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1165)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:562)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:522)
at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:325)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:323)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:312)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199)
at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1231)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:949)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:624)
at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:146)
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:754)
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:456)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:334)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1354)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1343)
at com.taskproject.blogproject.BlogProjectApplication.main(BlogProjectApplication.java:10)
in Application.properties change
spring.datasource.driver-class-name=com.taskproject.blogproject
to
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver