I'm working on an online tutorial, and in the step where I need to work with databases and add jpa, I cannot access it in my project.
I have successfully put the dependency in my pom.xml file, and have received no errors. I was also able to access spring jpa through my application.properties file. The problem is that when I wanted to extend CrudRepository, it wasn't being recognized...I then realized that when I went to manually import, it would not import org.springframework.data.
I have maven set to always update snapshots.
Here is my pom.xml file. I apologize for the formatting, but I couldn't get it to all appear on stackoverflow with the correct formatting from intellij:
<?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 http://maven.apache.org/xsd/ maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>demo3</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>demo3</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.1.RELEASE</version>
<relativePath/>
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</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>
And my 'application.properties' file:
spring.datasource.url=***
spring.datasource.username=***
spring.datasource.password=***
spring.jpa.database=MYSQL
spring.jpa.hibernate.ddl-auto = update
spring.jpa.show-sql=false
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect
Cheese class:
package com.example.demo.models;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
#Entity
public class Cheese {
#Id
#GeneratedValue
private int id;
#NotNull
#Size(min=3, max=20)
private String name;
#NotNull
#Size(min=1, message = "Description must not be empty")
private String description;
public Cheese(String name, String description) {
this.name = name;
this.description = description;
}
public int getId() {
return id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
}
CheeseDao (interface using Spring Data/CrudRepository)
package com.example.demo.models;
import com.example.demo.models.Cheese;
public class CheeseDao extends CrudRepository<Cheese, Integer> {
}
Add the Annotation to your DAO and it should be interface not class,
#Repository
public interface CheeseDao extends JpaRepository<Cheese, Integer> {
}
However, I would recommend you to use JpaRepository instead of CrudRepository. For details see this stackoverflow thread.
Now you can access them from any spring annotated class like below,
#Autowired
private CheeseDao cheeseDao;
It seem like you missing hibernate entity dependency, to resolve just add dependency below
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
</dependency>
Replace
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
with
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
Instead of
package com.example.demo.models;
import com.example.demo.models.Cheese;
public class CheeseDao extends CrudRepository<Cheese, Integer> {
}
try
package com.example.demo.models;
import com.example.demo.models.Cheese;
public class CheeseDao extends CrudRepository<Object, Integer> {
}
The CrudRepository<> expects types.
Related
During the walkthrough I have added the specified model, controller and repo. I have added all the required dependencies through spring boot (I beleive) as well as creating the DB through the MySql shell.
The db is the same as the example as well as the basic root user until I can get this to work then will add another user.
Any help would be appreciated as I have had this same issue every single time I have tried to set up a db.
My model
package com.avorion.dndwiki.Model;
import jakarta.persistence.Entity; import jakarta.persistence.GeneratedValue; import jakarta.persistence.GenerationType; import jakarta.persistence.Id;
#Entity // This tells Hibernate to make a table out of this class public class User {
#Id
#GeneratedValue(strategy=GenerationType.AUTO) private Integer id;
private String name;
private String email;
public Integer getId() { return id;
}
public void setId(Integer id) { this.id = id;
}
public String getName() { return name;
}
public void setName(String name) { this.name = name;
}
public String getEmail() { return email;
}
public void setEmail(String email) { this.email = email;
} }
My Controller
package com.avorion.dndwiki.Controller;
import com.avorion.dndwiki.Model.User; import com.avorion.dndwiki.Repository.UserRepository; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.ResponseBody;
#Controller // This means that this class is a Controller #RequestMapping(path="/demo") // This means URL's start with /demo (after Application path) public class MainController {
#Autowired // This means to get the bean called userRepository Which is auto-generated by Spring, we will use it to handle the data private UserRepository userRepository;
#PostMapping(path="/add") // Map ONLY POST Requests public #ResponseBody String addNewUser (#RequestParam String name
, #RequestParam String email) {
// #ResponseBody means the returned String is the response, not a view name
// #RequestParam means it is a parameter from the GET or POST request
User n = new User(); n.setName(name); n.setEmail(email); userRepository.save(n); return "Saved";
}
#GetMapping(path="/all") public #ResponseBody Iterable<User> getAllUsers() { This returns a JSON or XML with the users return userRepository.findAll();
} }
My Repo
package com.avorion.dndwiki.Repository;
import com.avorion.dndwiki.Model.User; import org.springframework.data.repository.CrudRepository;
//This will be AUTO IMPLEMENTED by Spring into a Bean called userRepository CRUD refers Create, Read, Update, Delete
public interface UserRepository extends CrudRepository<User, Integer> {
}
My application properties
spring.jpa.hibernate.ddl-auto=update
spring.datasource.url=jdbc:mysql://${MYSQL_HOST:localhost}:3306/db_example
spring.datasource.username=root#localhost
spring.datasource.password=thisIsntMyActualPassword
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
#spring.jpa.show-sql: true
My Pom(Theres a lot here,im just practicing)
<?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.0.1</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.Avorion</groupId>
<artifactId>DnDWiki</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>DnDWiki</name>
<description>DnDWiki</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-data-rest</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web-services</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-websocket</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.kafka</groupId>
<artifactId>spring-kafka</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.session</groupId>
<artifactId>spring-session-core</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>com.microsoft.sqlserver</groupId>
<artifactId>mssql-jdbc</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.kafka</groupId>
<artifactId>spring-kafka-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>
I have tried to set up a number of db with diffrent users and naming conventions. I have reset MySql as well as asking other software engineers.
I am using spring boot for my application when I run it it show me an
error This application has no explicit mapping for /error, so you
are seeing this as a fallback.
Sat Jun 06 11:44:29 IST 2020 There was an unexpected error (type=Not
Found, status=404). No message available I use my sql for database
and sql youg for database editor but it is not able to connect with
database
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>2.0.1.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<groupId>com.material</groupId>
<artifactId>MaterialComp</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>MaterialComp</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.18</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
UserEntity.java
package com.material.Entity;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
#Entity
#Table(name="userentity")
public class UserEntity {
#Id
#Column(name="id")
#GeneratedValue(strategy=GenerationType.IDENTITY)
private Long id;
#Column(name="name")
private String name;
#Column(name="password")
private String password;
#Column(name="type")
private String type;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
}
UserRepo.java
package com.material.Repo;
import org.springframework.data.jpa.repository.JpaRepository;
import com.material.Entity.UserEntity;
public interface UserRepository extends JpaRepository<UserEntity, Long>{
}
controller.java
package com.material.Controller;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.material.Entity.UserEntity;
import com.material.Repo.UserRepository;
#RestController
#CrossOrigin(origins= "http://localhost:4200")
#RequestMapping(path="user")
public class UserController {
#Autowired
private UserRepository userRepo;
#GetMapping("/get")
public List<UserEntity> getUsers(){
return userRepo.findAll();
}
}
application.properties
spring.datasource.url=jdbc:mysql://localhost/ecommerce
spring.datasource.username=root
spring.datasource.password=
spring.jpa.hibernate.ddl-auto=create-drop
spring.jpa.hibernate.use-new-id-generator-mappings=false
There's a few tricky things here.
Firstly, you've configured your server to listen on localhost:4200, which is fine.
You've done that with this line:
#CrossOrigin(origins= "http://localhost:4200")
Next you've told spring this:
#RequestMapping(path="user")
Spring will take user and add it to http://localhost:4200. So it's listening now on http://localhost:4200user - this isn't what you want, try adding a / so that your request mapping looks like this:
#RequestMapping(path="/user")
Your application should now be listening on http://localhost:4200/user. Great!
Next, to hit the getUsers method, you've defined #GetMapping("/get") which, combined with your other annotations will mean that to hit getUsers you need to make a GET to http://localhost:4200/user/get, assuming everything was working correctly.
A little trick is that #GetMapping is actually an alias for #RequestMapping(method = RequestMethod.GET) - so to tidy this up you could delete #RequestMapping(path="user") and just use
#GetMapping(value = "/users") and this would mean that a GET against http://localhost:4200/user would activate your getUsers method.
While running the project. I am getting the below error in console.
Description:
Parameter 0 of constructor in com.sam.cmsshoppingcart.controller.AdminPagesController required a bean of type 'com.sam.cmsshoppingcart.models.PageRepository' that could not be found.
Action:
Consider defining a bean of type 'com.sam.cmsshoppingcart.models.PageRepository' in your configuration.
please find the below screen shot.
enter image description here
Adding Code :
CmsShoppingCartApplication.Java
package com.sam.cmsshoppingcart;
#SpringBootApplication
public class CmsShoppingCartApplication {
public static void main(String[] args) {
SpringApplication.run(CmsShoppingCartApplication.class, args);
}
}
WebConfig.Java
package com.sam.cmsshoppingcart;
#Configuration
public class WebConfig implements WebMvcConfigurer {
#Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/").setViewName("home");
}
}
HomeController.Java
package com.sam.cmsshoppingcart.controller;
#Controller
public class HomeController {
#GetMapping("/someRandomPage")
public String home() {
return "home";
}
}
AdminPagesController.Java
package com.sam.cmsshoppingcart.controller;
#Controller
#RequestMapping("/admin/pages")
public class AdminPagesController {
#Autowired
private PageRepository pageRepo;
public AdminPagesController(PageRepository pageRepo) {
this.pageRepo = pageRepo;
}
#GetMapping
public String index(Model model) {
List<Page> pages = pageRepo.findAll();
model.addAttribute("pages", pages);
return "/admin/pages/index";
}
}
PageRepository.Java
package com.sam.cmsshoppingcart.models;
import org.springframework.data.jpa.repository.JpaRepository;
import com.sam.cmsshoppingcart.models.data.Page;
public interface PageRepository extends JpaRepository<Page, Integer>{
}
Page.Java
package com.sam.cmsshoppingcart.models.data;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
import lombok.Data;
#Entity
#Table(name="pages")
#Data
public class Page {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
private String title;
private String slug;
private String content;
private int sorting;
}
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>2.2.4.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web-services</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>5.3.7.Final</version>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-jpa</artifactId>
<version>2.2.6.RELEASE</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
<version>5.1.6</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
Clearly, PageRepository bean is missing in the configuration. Add #Repository
or #Component on top of PageRepository
#Repository
public interface PageRepository extends JpaRepository<Page, Integer>{}
I would like to create a web-application on Heroku implemented in Spring Boot. Finally, I managed to deploy a plain project with "Whitelabel error page 404", confirming correctness of deploying.
I followed by (almost the same): https://devcenter-assets0.herokucdn.com/articles/spring-boot-memcache
Unfortunately, I got only one (I hope) problem with Spring Data JPA (after adding a service (for example Spring Security and Lombok work properly) that is making it impossible for me to build app and use PostgreSQL:
Description:
Parameter 0 of constructor in io.github.plkpiotr.fifabackend.controller.TaskController required a bean named 'entityManagerFactory' that could not be found.
Action:
Consider defining a bean named 'entityManagerFactory' in your configuration.
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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>io.github.plkpiotr</groupId>
<artifactId>fifa-backend</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>fifa-backend</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<!--<dependency>-->
<!--<groupId>org.springframework.boot</groupId>-->
<!--<artifactId>spring-boot-starter-security</artifactId>-->
<!--</dependency>-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.22</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
<version>2.3.0</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
application.properties:
spring.datasource.driverClassName=org.postgresql.Driver
spring.datasource.maxActive=10
spring.datasource.maxIdle=5
spring.datasource.minIdle=2
spring.datasource.initialSize=5
spring.datasource.removeAbandoned=true
spring.jpa.properties.hibernate.temp.use_jdbc_metadata_defaults = false
spring.jpa.database-platform=org.hibernate.dialect.PostgreSQL9Dialect
spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration
Task.java:
package io.github.plkpiotr.fifabackend.model;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.validation.constraints.NotNull;
#Entity
public class Task {
#Id
#GeneratedValue(strategy=GenerationType.IDENTITY)
private Long id;
#NotNull
private String name;
public Task() {}
public Task(String name) {
this.name = name;
}
public Long getId() {
return this.id;
}
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
#Override
public String toString() {
return String.format("Task[id=%d, name='%s']", this.id, this.name);
}
}
TaskRepository.java:
package io.github.plkpiotr.fifabackend.repository;
import io.github.plkpiotr.fifabackend.model.Task;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;
#Repository
public interface TaskRepository extends CrudRepository<Task, Long> {}
TaskController.java:
package io.github.plkpiotr.fifabackend.controller;
import javax.validation.Valid;
import java.lang.Iterable;
import io.github.plkpiotr.fifabackend.model.Task;
import io.github.plkpiotr.fifabackend.repository.TaskRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
#Controller
#RequestMapping("/")
public class TaskController {
#Autowired
private TaskRepository taskRepo;
#Autowired
public TaskController(TaskRepository repo) {
this.taskRepo = repo;
}
#RequestMapping(method = RequestMethod.GET)
public String showAllTasks(ModelMap model) {
Iterable<Task> tasks = this.taskRepo.findAll();
model.addAttribute("tasks", tasks);
model.addAttribute("newTask", new Task());
return "task";
}
#RequestMapping(method = RequestMethod.POST)
public String newTask(ModelMap model,
#ModelAttribute("newTask") #Valid Task task,
BindingResult result) {
if (!result.hasErrors()) {
this.taskRepo.save(task);
}
return showAllTasks(model);
}
#RequestMapping(method = RequestMethod.DELETE)
public String deleteTask(ModelMap model, #RequestParam("taskId") Long id) {
this.taskRepo.deleteById(id);
return showAllTasks(model);
}
}
I tried:
1). Add differentiated dependencies (h2, hibernate-core, ...)
2). Remove /.m2/ folder and redownload dependencies
3). Don't cry after other failed attemps...
And if you are intrested in cloning repo:
https://github.com/plkpiotr/fifa-backend
In addition to this, if I deleted inside of TaskController.java I get:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'jpaMappingContext': Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: At least one JPA metamodel must be present!
How to fix the problems?
#EntityScan, #EnableJpaRepositories and #ComponentScan do not support a wildcard syntax. For example you currently use:
#EntityScan("io.github.plkpiotr.*")`
but it should be just the base package name
#EntityScan("io.github.plkpiotr")
I am new to Spring Boot and working on database connectivity. All the related classes are here only main class having run method is not posted.
It is giving me the error of no converter found for ArrayList.
Please help me if I am doing anything wrong.
//Customer.java
package springbootfirstapp.domain;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
#Entity
public class Customer {
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
#Column(name="id")
private int id;
private String name;
private String phone;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public Customer(int id, String name, String phone) {
super();
this.id = id;
this.name = name;
this.phone = phone;
}
public Customer() {
super();
}
}
//CustomerController.java
package springbootfirstapp.controller;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import springbootfirstapp.domain.Customer;
import springbootfirstapp.repo.CustomerRepo;
#RestController
#RequestMapping("/customer")
public class CustomerController {
#Autowired
CustomerRepo rp;
#RequestMapping("/findall")
#ResponseBody
public List<Customer> findall()
{
return rp.findAll();
}
}
//CustomerRepo.java
package springbootfirstapp.repo;
import org.springframework.data.jpa.repository.JpaRepository;
import springbootfirstapp.domain.Customer;
public interface CustomerRepo extends JpaRepository<Customer, Integer> {
}
//pom.xml
<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 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>SpringBootFirstApp</groupId>
<artifactId>springbootfirstapp</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.3.5.RELEASE</version>
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEndoing>UTF- 8</project.reporting.outputEndoing>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
STACKTRACE:
Generic Guidelines for such an error is to check for :
No Args Constructor
Getters & Setters
Jackson dependencies
Since first two aren't a problem, Please try adding the below two dependencies and let us know if its working.
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.4.3</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.4.3</version>
</dependency>
Add this dependency in pom.xml
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.8.1</version>
</dependency>
If I correctly understand your question, you need to add jackson mapper in pom.
Can you try that once and see your converter issue is resolved.
Thankyou everyone for the help...
Issue is resolved by adding Jackson Mapper dependency
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.5.2</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.5.2</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.5.2</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.jaxrs</groupId>
<artifactId>jackson-jaxrs-json-provider</artifactId>
<version>2.5.2</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.module</groupId>
<artifactId>jackson-module-jaxb-annotations</artifactId>
<version>2.5.2</version>
</dependency>