I am trying to follow through this tutorial about Spring Boot and I am currently at the JPA section and I've ran into a problem that I cannot solve.
Where I am standing currently is I've built the entity, I've got all the dependencies I need for the DB (H2 Database), I've built the controller and the services if that matters for anything here and I get an error when I run my application.
Trying to run this:
insert into 'user'(id,name,birthdate)
values (1,sysdate(), 'Mason');
The error I am getting is this:
org.springframework.jdbc.datasource.init.ScriptStatementFailedException: Failed to execute SQL script statement #1 of URL [file:/C:/Users/jsalv/Desktop/tutorial/target/classes/data.sql]: insert into 'user'(id,name,birthdate) values (1,sysdate(), 'Mason'); nested exception is org.h2.jdbc.JdbcSQLSyntaxErrorException: Syntax error in SQL statement "insert into [*]'user'(id,name,birthdate) values (1,sysdate(), 'Mason')"; expected "identifier"; SQL statement:
insert into 'user'(id,name,birthdate) values (1,sysdate(), 'Mason') [42001-212]
This is my 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.7.0</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.in28minutes</groupId>
<artifactId>tutorial</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>tutorial</name>
<description>Some tutorial</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>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-hateoas</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>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>
And this is the entity that I am trying to work with:
package com.in28minutes.tutorial.user;
import com.sun.istack.NotNull;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.validation.constraints.Past;
import javax.validation.constraints.Size;
import java.util.Date;
#Entity
public class User {
#Id
#GeneratedValue
private Integer id;
#Size(min = 2)
private String name;
#Past
private Date birthDate;
public User(Integer id, String name, Date birthDate) {
this.id = id;
this.name = name;
this.birthDate = birthDate;
}
public User() {
}
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 Date getBirthDate() {
return birthDate;
}
public void setBirthDate(Date birthDate) {
this.birthDate = birthDate;
}
#Override
public String toString() {
return "User{" +
"id=" + id +
", name='" + name + '\'' +
", birthDate=" + birthDate +
'}';
}
}
I've looked into a few solutions here on SO, one of which was putting the user in brackets because it's an SQL specific keyword, that didn't work, kept getting the same error. Then I extended the syntax a little bit, looking at this example.
I do not understand where I am going wrong.
According to the documentation h2 documentation,
user is a reserved keyword which could not be used, unless surrounded with double quotes .
Try with
insert into "user"(id,birthdate,name)
values (1,sysdate(), 'Mason');
However I would strongly advice of renaming your entity into something that is not a reserved keyword in sql standard. H2 may avoid this with double quottes, but H2 is mostly used as a test database. Your real database may be from another vendor where it fails again, or even worse you switch database vendor some years down the road and the next vendor does not support this reserved keyword with some workaround and then you are doomed for major migrations.
If you insist on this solution take a read first here and also here.
insert into 'user'(id,birthdate,name)
values (1,sysdate(), 'Mason');
I guess the right query is
Related
I'm working my way through a tutorial video on unit testing with JUnit and Mockito and have run across some issues where something isn't playing nice and I'm trying to figure out the problem. The tutorial is creating an in memory database using H2 which is where I am having issues (the database has no data). I have created a new project with the sole purpose of creating the same database which works to a point and then fails with no data. After a couple days of research I'm hoping someone will see something glaringly obvious that I'm missing. I'm using IntelliJ Idea 21.1.2 as my IDE, Linux Mint OS. H2 is new to me.
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.5.2</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>TestH2</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>TestH2</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>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>com.h2database</groupId>
<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>
application.properties
spring.datasource.url=jdbc:h2:mem:item;DB_CLOSE_ON_EXIT=FALSE;DB_CLOSE_DELAY=-1
spring.h2.console.enabled=true
schema.sql
DROP TABLE IF EXISTS ITEM;
CREATE TABLE ITEM (
id INT,
name VARCHAR(255),
price INT,
quantity INT
);
data.sql
INSERT INTO ITEM(id, name, price, quantity)
VALUES
(10001,'Item1',10,20),
(10002,'Item2',5,10),
(10003,'Item3',15,2);
With the above and my application file the project builds and when I open the h2-console I get the expected result.
However. . .
If I try to add an Item.java file like this (note if I comment out the #Entity line it works as shown above)
package com.example.testh2.model;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Transient;
#Entity
public class Item {
#Id
private int id;
private String name;
private int price;
private int quantity;
#Transient
private int value;
public Item() {
}
public Item(int id, String name, int price, int quantity) {
this.id = id;
this.name = name;
this.price = price;
this.quantity = quantity;
}
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 int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
public int getQuantity() {
return quantity;
}
public void setQuantity(int quantity) {
this.quantity = quantity;
}
public int getValue() { return value; }
public void setValue(int value) { this.value = value; }
public String toString() {
return String.format("Item[%d, %s, %d, %d]", id, name, price, quantity);
}
}
Then there is no data
I'm guessing that I'm missing something simple, I just haven't Google'd the right question yet. I have tried the default as well as the current datasource.url, I've tried embedded H2 with a file (I see the items.mv.db file appear where it should but the console didn't find it nor did the IntelliJ database tools). Any help would be greatly appreciated.
in the application.properties file, add this line to turn off the conflicting automatic schema creation:
spring.jpa.hibernate.ddl-auto=none
you can read more about it here:
https://docs.spring.io/spring-boot/docs/1.1.0.M1/reference/html/howto-database-initialization.html
Points to remember :
H2 is an in-memory DB . that is if you re-start you application the existing data will be lost .
you have spring dev tools in your dependency , that restarts the app when you do a change/add/modify class/anything in your application .
based on your comment :-
However. . .
If I try to add an Item.java file like this (note if I comment out the
#Entity line it works as shown above)
It seems that you are adding the data first via H2 console and then adding/modifiying the class ,
if you add/modify the class after insertion of Data : spring dev tools will restart your app and your h2 data will be lost .
the recommendation is add the entity class and after that add data to the h2 db ,and make sure not to change anything in project after adding the data.
I apologize about the title of the question. I was exactly sure how to word the question.
A little background: I'm fairly new to Spring, so there's fairly a lot of things Spring-related that go completely over my head. I recently started a new practice project where I create an inventory application for medicines. As of now, I have my models and repositories set up. I've decided to implement a PostgreSQL database.
I've worked on a few Spring projects in the past, and if I'm remembering correctly, you can hardcode some instances of the models that you've created for testing purposes. So, when you run the Spring boot application, these instances will already be in the database, so I wouldn't have to add it through pgAdmin or the sql shell myself after the fact.
I'm confused as to where exactly I would "hard-code" this information and whether or not it would be in JSON format. I think it would go in the main function of my InventoryApplication.java because that's where my spring boot program is being run, but I'm unsure.
Here's a few of my files in order to understand the flow of my project:
InventoryApplication.java
package com.oasis.inventory;
import com.oasis.inventory.model.*;
import com.oasis.inventory.repository.*;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
#SpringBootApplication
public class InventoryApplication {
public static void main(String[] args) {
SpringApplication.run(InventoryApplication.class, args);
}
}
pharmaceutical.java
package com.oasis.inventory.model;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Set;
import javax.persistence.*;
#Entity
#Table(name = "pharmaceuticals")
public class Pharmaceutical {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;
#Column(name = "genericName")
private String genericName;
#Column(name = "brandNames")
private ArrayList<String> brandNames;
#Column(name = "strength" )
private String strength;
#Column(name = "quantity")
private int quantity;
#ManyToMany(fetch = FetchType.LAZY,
cascade = {
CascadeType.PERSIST,
CascadeType.MERGE
})
#JoinTable(name = "pharm_commonuses",
joinColumns = { #JoinColumn(name = "pharmaceutical_id") },
inverseJoinColumns = { #JoinColumn(name = "commonUse_id") })
private Set<CommonUse> commonUses = new HashSet<>();
public Pharmaceutical() {}
public Pharmaceutical(String genericName, ArrayList<String> brandNames, String strength,
int quantity) {
this.genericName = genericName;
this.brandNames = brandNames;
this.strength = strength;
this.quantity = quantity;
}
public String getGenericName() {
return genericName;
}
public void setGenericName(String genericName) {
this.genericName = genericName;
}
public ArrayList<String> getBrandNames() {
return brandNames;
}
public void setBrandNames(ArrayList<String> brandNames) {
this.brandNames = brandNames;
}
public String getStrength() {
return strength;
}
public void setStrength(String strength) {
this.strength = strength;
}
public int getQuantity() {
return quantity;
}
public void setQuantity(int quantity) {
this.quantity = quantity;
}
public long getId() {
return id;
}
#Override
public String toString() {
return "Pharmaceutical [id=" + id + ", genericName=" + genericName + ", brandNames=" + brandNames
+ ", strength=" + strength + ", quantity=" + quantity + ", commonUses=" + commonUses + "]";
}
public Set<CommonUse> getCommonUses() {
return commonUses;
}
}
application.properties
server.port=8081
spring.datasource.initialization-mode=always
spring.datasource.platform=postgres
spring.datasource.url=jdbc:postgresql://localhost:5432/testdb
spring.datasource.username=postgres
spring.datasource.password= sEcReT
spring.jpa.properties.hibernate.jdbc.lob.non_contextual_creation= true
spring.jpa.properties.hibernate.dialect= org.hibernate.dialect.PostgreSQLDialect
# Hibernate ddl auto (create, create-drop, validate, update)
# For prod, change to validate
spring.jpa.hibernate.ddl-auto= update
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.3.2.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.oasis</groupId>
<artifactId>inventory</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>inventory</name>
<description>pharmaceuticals inventory management system</description>
<properties>
<java.version>11</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>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<scope>runtime</scope>
</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>
It's close to a 2 year old question and I believe you have come up with a solution. I still wanted to share just in case someone would be looking for the same.
You can use CommandLineRunner to execute a transaction like persisting a record into the database. However, every time your application starts up it will always execute this and will lead to unwanted side effects but you can try it in your local development.
As #martinspielmann recommended, you can use FlywayDB or Liquibase to do the job. You can create versioned SQL files that may contain several queries for different transactions (CRUD) including database operations like creating/alter tables and more.
I try to create a website with spring. But when I try to coonect my project with my database and to work with it throw spring I get errors.I run mysql server only. I 100% know that port, password and username are right. What am I doing wrong?
run mysql server
sudo /etc/init.d/mysql start
like this
application.propeties:
spring.jpa.hibernate.ddl-auto=update
spring.datasource.url=jdbc:mysql://${MYSQL_HOST:localhost}:3306/java_spring_blog
spring.datasource.username=root
spring.datasource.password=root
dependcies:
<?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.6.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.coursework</groupId>
<artifactId>CourseWork</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>CourseWork</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>11</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-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
<version>2.3.0</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</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>
Model:
package com.coursework.CourseWork.Models;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
#Entity
public class Post {
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private String title, anons, fulltext;
private int views;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getAnons() {
return anons;
}
public void setAnons(String anons) {
this.anons = anons;
}
public String getFulltext() {
return fulltext;
}
public void setFulltext(String fulltext) {
this.fulltext = fulltext;
}
public int getViews() {
return views;
}
public void setViews(int views) {
this.views = views;
}
}
When I try to run programm I got an exeption:java.sql.SQLException: Access denied for user 'root'#'localhost'
And another one:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaConfiguration.class]: Invocation of init method failed; nested exception is org.hibernate.service.spi.ServiceException: Unable to create requested service [org.hibernate.engine.jdbc.env.spi.JdbcEnvironment]
I think it's because due to thirst one.
It works when I create a new user. Thanks to Ankit
I am trying to configure my springboot application using spring Data REST. I am having challenges displaying data from my entity class with Spring-Data REST. I have added the necessary dependency to my pom.xml file but when I try to access the endpoints on the web browser or via postman, I get nothing.
See the Error message from web browser and postman:
Whitelabel Error Page
This application has no explicit mapping for /error, so you are seeing this as a fallback.
Tue Mar 24 18:41:33 PDT 2020
There was an unexpected error (type=Not Found, status=404).
No message available
Postman error message
{
{
"timestamp" : "2020-03-25T01:13:38.867+0000",
"timestamp" : "2020-
"status" : 404,
"error" : "Not Found",
"message" : "No message available",
"path" : "/api/employees"
}
Here is the pom.xml file
<?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.1.13.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.dafe.spring</groupId>
<artifactId>AppLogger</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>AppLogger</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>
<!-- Add dependency for Spring Data REST -->
<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-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</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 my entity class with name Employee
package com.dafe.spring.logger.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="employee")
public class Employee {
// define fields
#Id
#GeneratedValue(strategy=GenerationType.IDENTITY)
#Column(name="id")
private int id;
#Column(name="first_name")
private String firstName;
#Column(name="last_name")
private String lastName;
#Column(name="email")
private String email;
// define constructors
public Employee() {
}
public Employee(String firstName, String lastName, String email) {
this.firstName = firstName;
this.lastName = lastName;
this.email = email;
}
// define getter/setter
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
// define tostring
#Override
public String toString() {
return "Employee [id=" + id + ", firstName=" + firstName + ", lastName=" + lastName + ", email=" + email + "]";
}
}
Here is my EmployeeRepository interface
package com.dafe.spring.logger.dao;
import org.springframework.data.jpa.repository.JpaRepository;
import com.dafe.spring.logger.entity.Employee;
public interface EmployeeRepository extends JpaRepository<Employee, Integer> {
}
I would appreciate any help. Thanks
Edit: This answer is not for Spring Data REST users because apparently it automatically configures the mappings.
I do not see a mapping for /api/employees in the code that you posted.
Have you defined a method that is annotated with #GetMapping("/api/employees")?
It should be in a class that is annotated with #RestController.
#GetMapping("/api/employees")
public List<Employee> getEmployees() {
// Get employees from EmployeeRepository
}
I fixed this issue by changing the base uri from my application.properties file, I added spring.data.rest.basePath=/api and it worked like magic
According to the documentation of Spring Data Rest, "by default, Spring Data REST serves up REST resources at the root URI, '/'. There are multiple ways to change the base path.
With Spring Boot 1.2 and later versions, you can do change the base URI by setting a single property in application.properties, as follows: spring.data.rest.basePath=/api "
For more details of Spring Data REST, use this link: Spring Data REST reference guide
This is error which I get when I try to run the Spring Boot app as java app:
org.hibernate.tool.schema.spi.CommandAcceptanceException: Error executing DDL via JDBC Statement
Caused by: java.sql.SQLSyntaxErrorException: Schema 'SA' does not exist
I have tride to change hibernate dialect in propereties in pom.xml (like written in other simillar topics about this problem) but it didn't help. I have also tried to change names of Topic class and fields.
This is the 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>com.julian.boot3</groupId>
<artifactId>boot3</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>boot3</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.4.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-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.apache.derby</groupId>
<artifactId>derby</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>
Main class:
package com.julian.spring;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
#SpringBootApplication
public class App {
public static void main(String[] args) {
SpringApplication.run(App.class, args);
}
}
POJO:
package com.julian.spring.pojo;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
#Entity
#Table(name="topics")
public class Topic {
#Id
private Integer id;
private String name;
private String description;
public Topic() {
}
public Topic(int id, String name, String description) {
this.id = id;
this.name = name;
this.description = description;
}
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 getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
}
Put this in application.properties file under resources:
spring.jpa.hibernate.ddl-auto=update
I solved it by downgrading version of spring-boot-starter-parent fomr 2.0.4.REALESE to 1.5.1.RELEASE
Please read https://spring.io/guides/gs/accessing-data-mysql/ this, is so regular question, how to arrange connection to database.
If you are not using MySQL, there are article for other databases, but for relational databases, all are the same.