Connect to PostgreSQL from SpringBootApplication - java

I am building a simple Spring application from the scratch using Maven and PostgreSQL.
I've been following thousand of tutorials but it isn't clear for me where to store the different configurations to connect and work with a PostgreSQL database.
My 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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.springframework</groupId>
<artifactId>gs-rest-service</artifactId>
<version>0.1.0</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.3.2.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>9.4-1201-jdbc4</version>
</dependency>
</dependencies>
<properties>
<java.version>1.8</java.version>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
<repositories>
<repository>
<id>spring-releases</id>
<url>https://repo.spring.io/libs-release</url>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>spring-releases</id>
<url>https://repo.spring.io/libs-release</url>
</pluginRepository>
</pluginRepositories>
</project>
My src/main/java/quotes/Application.java file:
package quotes;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
#SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
My src/main/java/quotes/Quote.java file:
package quotes;
public class Quote {
private String content;
private String author;
public Quote(String content, String author) {
this.content = content;
this.author = author;
}
public String getContent() {
return this.content;
}
public Quote setContent(String content) {
this.content = content;
return this;
}
public String getAuthor() {
return this.author;
}
public Quote setAuthor(String author) {
this.author = author;
return this;
}
}
And my src/main/java/quotes/QuoteController.java file:
package quotes;
import java.util.concurrent.atomic.AtomicLong;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.CrossOrigin;
#Controller
public class QuoteController {
private static final String template = "Hello, %s!";
private final AtomicLong counter = new AtomicLong();
#CrossOrigin(origins = "*")
#RequestMapping("/random")
public #ResponseBody Quote randomQuote() {
return new Quote("¿A dónde vas? Patatas traigo", "Ortega y Pacheco");
}
}
I can build and run the application with Maven:
mvn clean package
java -jar target/*.jar
The idea now is to modify the code on QuoteController.java to connect to PostgreSQL and return a random stored quote.
Could you please give some advices / clues?

You should try this tutorial .
http://devcrumb.com/hibernate/spring-data-jpa-hibernate-maven

You need to add an application.properties file under src/main/resources
It should look like this for PostgreSQL:
spring.jpa.database=POSTGRESQL
spring.datasource.platform=postgres
spring.database.driverClassName=org.postgresql.Driver
spring.datasource.url=jdbc:postgresql://localhost:5432/your-db-name
spring.datasource.username=username
spring.datasource.password=password
Checkout this link for more info:
enter link description here

Related

Sending email via Spring Boot

I'm trying to create a contact form. I am using Spring Boot 3.0.0. and Thymeleaf. Everything runs on localhost. I get an unexpected error when I want to send a message:
javax.mail.Provider: com.sun.mail.imap.IMAPProvider not a subtype java.util.ServiceConfigurationError: javax.mail.Provider: com.sun.mail.imap.IMAPProvider not a subtype
pom.xml:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
<version>3.0.0</version>
</dependency>
<dependency>
<groupId>com.sun.mail</groupId>
<artifactId>javax.mail</artifactId>
<version>1.6.2</version>
</dependency>
Message class:
public class MessageInfoDto {
private String name;
private String email;
private String text;
public String getName() {
return name;
}
public String getEmail() {
return email;
}
public String getText() {
return text;
}
public void setName(String name) {
this.name = name;
}
public void setEmail(String email) {
this.email = email;
}
public void setText(String text) {
this.text = text;
}
Controller:
#Controller
public class MessageInfoController {
MessageInfoService messageInfoService;
public MessageInfoController(MessageInfoService messageInfoService) {
this.messageInfoService = messageInfoService;
}
#GetMapping("/contact")
public String emialForm(Model model) {
model.addAttribute("message", new MessageInfoDto());
return "contact";
}
#PostMapping("/send")
public String getMessage(MessageInfoDto message) {
messageInfoService.sendEmail(message);
return "redirect:/contact";
}
}
Service:
#Service
public class MessageInfoService {
private final JavaMailSender mailSender;
#Value("${spring.mail.username}")
private String owner;
public MessageInfoService(JavaMailSender mailSender) {
this.mailSender = mailSender;
}
public void sendEmail(MessageInfoDto message) {
SimpleMailMessage simpleMailMessage = new SimpleMailMessage();
simpleMailMessage.setTo(owner);
simpleMailMessage.setFrom(message.getEmail());
simpleMailMessage.setText(message.getText());
simpleMailMessage.setSubject("New Message");
simpleMailMessage.setReplyTo(message.getEmail());
mailSender.send(simpleMailMessage);
}
}
HTML(contact.html):
<body>
<main class="main-content" layout:fragment="content">
<h2 class="list-heading">Send us a message</h2>
<form action="#" th:action="#{/send}" method="post" enctype="multipart/form-data" class="mc-form" th:object="${message}">
<label for="name">Your Name</label>
<input type="text" id="name" placeholder="Your Name" th:field="*{name}" required>
<label for="email">Your e-mail</label>
<input type="text" id="email" placeholder="e-mail" th:field="*{email}">
<label for="message">Your message</label>
<textarea id="message" rows="10" th:field="*{text}"></textarea>
<button type="submit">Send</button>
</form>
</main>
</body>
application.yml:
spring:
mail:
host: smtp.gmail.com
port: 587
username: 10*****#gmail.com
password: [genereted password]
properties:
smtp:
auth: true
starttles:
enable: true
required: true
Please help, I've been googling this since yesterday and couldn't find an answer.
I've also tried with javax.mail-api.
You don't need this, please remove it.
<dependency>
<groupId>com.sun.mail</groupId>
<artifactId>javax.mail</artifactId>
<version>1.6.2</version>
</dependency>
Check MailSender import
USE import org.springframework.mail.MailSender;
NOT import org.springframework.mail.javamail.JavaMailSender;
spring-boot-starter-mail will create default MailSender , you don't create it (MailSender), just use #Autowired private MailSender mailSender;
Hello.java
package com.example.demo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
//import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.MailSender;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
#RestController
public class Hello {
#Autowired
private MailSender mailSender;
#RequestMapping("/sendMail")
public String sendConfirmationEmail() {
SimpleMailMessage msg = new SimpleMailMessage();
msg.setTo("aaa#localhost");
msg.setSubject("Testing from Spring Boot");
msg.setText("Hello World from Spring Boot Email");
mailSender.send(msg);
return "OK";
}
}
I am test ok in my simple test project.
curl http://localhost:8080/sendMail
And I use FakeSmtp jar to my fake mail server.
my spring boot 3 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.0.0</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>demo-mail</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo-mail</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>17</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</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-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>
MY application.properties
spring.mail.host=localhost
spring.mail.port=2525
spring.mail.username=demo#localhost
spring.mail.password=demopassword
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.transport.protocol=smtp
spring.mail.properties.mail.smtps.quitwait=false
spring.mail.properties.mail.smtp.socketFactory.port=465
spring.mail.properties.mail.smtp.socketFactory.class=javax.net.ssl.SSLSocketFactory
DemoMailApplication.java
package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
#SpringBootApplication
public class DemoMailApplication {
public static void main(String[] args) {
SpringApplication.run(DemoMailApplication.class, args);
}
}
DemoMailApplication.java
Hello.java
application.properties
pom.xml
Spring Boot 2.7.x send Mail with attachments
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.8</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>17</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-mail</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>
</project>
DemoApplication.java
package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
#SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
Hello.java
package com.example.demo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.MailSender;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.File;
#RestController
public class Hello {
#Autowired
private JavaMailSenderImpl mailSender;
#RequestMapping("/sendMail")
public String sendConfirmationEmail() throws MessagingException {
MimeMessage message = mailSender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(message, true);
helper.setTo("test#host.com");
helper.setText("Check out this image!");
FileSystemResource file = new FileSystemResource(new File("/YourAttachmentFile/Example.jpg"));
helper.addAttachment("CoolImage.jpg", file);
mailSender.send(message);
return "OK";
}
}
application.properties
spring.mail.host=localhost
spring.mail.port=2525
spring.mail.username=demo#localhost
spring.mail.password=demopassword
spring.mail.properties[mail.smtp.connectiontimeout]=5000
spring.mail.properties[mail.smtp.timeout]=3000
spring.mail.properties[mail.smtp.writetimeout]=5000
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.transport.protocol=smtp
spring.mail.properties.mail.smtps.quitwait=false
spring.mail.properties.mail.smtp.socketFactory.port=465
spring.mail.properties.mail.smtp.socketFactory.class=javax.net.ssl.SSLSocketFactory
Package
mvn clean package
Run
cd target
java -jar demo-0.0.1-SNAPSHOT.jar
Test
curl http://localhost:8080/sendMail
You can use FakeSmtp as Dummy Mail Server
REFERENCE
Example Code is copy from spring-boot-reference-2.7.8.pdf ( 11.4. Sending Email -> TIP See the reference documentation ) -> https://docs.spring.io/spring-framework/docs/5.3.25/reference/html/integration.html#mail (6.2.1. Sending Attachments and Inline Resources)
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSenderImpl;
public void sendEmail(){
JavaMailSenderImpl mailSender = new JavaMailSenderImpl();
mailSender.setHost("smtp.gmail.com");
mailSender.setPort(587);
mailSender.setUsername("yourEmail#example.com");
mailSender.setPassword("yourPassword");
SimpleMailMessage message = new SimpleMailMessage();
message.setFrom("sender#example.com");
message.setTo("receiver#example.com");
message.setSubject("Test Email");
message.setText("This is a test email sent using Spring Boot");
mailSender.send(message);
}

Spring boot 404 not found

Hello I am struggling to figure out what is my issue. Teacher did this in eclipse. I am on Intelleji. I made a project with Java 17 and the SDK 17 too. Spring boot 3.0.0 m1
It is basic code but when I try to get it working i get error 404. Driving me nuts. If anyone sees something that I am not seeing i would be extremely grateful.
the link i try in postman is as follow: localhost:8081/restws/services/patientservice/patients
which is what the teacher did in eclipse or string tool suite.
I am in intelleji ultimate.
thank you for any help.
my structure goes as follow:
.
└── restws
├── PatientService.java
├── PatientServiceImpl.java
├── RestwsApplication.java
└── model
└── Patient.java
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.0.0-M1</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.slinky</groupId>
<artifactId>restws</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>restws</name>
<description>restws</description>
<properties>
<java.version>17</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-spring-boot-starter-jaxrs</artifactId>
<version>3.3.7</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</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</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>
<repositories>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</pluginRepository>
</pluginRepositories>
PatientService.java
package com.slinky.restws;
import com.slinky.restws.model.Patient;
import jakarta.ws.rs.GET;
import jakarta.ws.rs.Path;
import java.util.List;
#Path("/patientservice")
public interface PatientService {
#Path("/patients")
#GET
List<Patient> getPatient();
}
Patient.java from com.slinky.restws.model
package com.slinky.restws.model;
import jakarta.xml.bind.annotation.XmlRootElement;
#XmlRootElement(name = "Patient")
public class Patient {
private long id;
private String name;
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;
}
}
application.properties
cxf.jaxrs.classes-scan=true
cxf.jaxrs.classes-scan-packages=org.codehaus.jackson.jaxrs, com.slinky.restws
server.servlet.context-path=/restws
server.port=8081
PatientServiceImpl.java
package com.slinky.restws;
import com.slinky.restws.model.Patient;
import org.springframework.stereotype.Service;
import java.util.*;
#Service
public class PatientServiceImpl implements PatientService{
Map<Long, Patient> patients = new HashMap<>();
long currentId = 123;
public PatientServiceImpl() {
init();
}
public void init() {
Patient patient = new Patient();
patient.setId(currentId);
patient.setName("John");
patients.put(patient.getId(), patient);
}
#Override
public List<Patient> getPatient() {
Collection<Patient> results = patients.values();
List<Patient> response = new ArrayList<>(results);
return response;
}
}
main which was created automatically
package com.slinky.restws;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
#SpringBootApplication
public class RestwsApplication {
public static void main(String[] args) {
SpringApplication.run(RestwsApplication.class, args);
}
}

Connect with MSSQL with Spring Reactive (R2DBC)

I am currently trying to establish a database connection with a Microsoft SQL Server.
Unfortunately I can not understand why it does not work. And the error message can unfortunately not give me precise information.
It looks like my code isn't even trying to connect to the database.
My Starterclaas:
#SpringBootApplication
public class R2Dbc3Application {
public static void main(String[] args) {
SpringApplication.run(R2Dbc3Application.class, args);
}
}
DatabaseConfiguration:
package com.example.config;
import io.r2dbc.mssql.MssqlConnectionConfiguration;
import io.r2dbc.mssql.MssqlConnectionFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.r2dbc.config.AbstractR2dbcConfiguration;
import org.springframework.data.r2dbc.repository.config.EnableR2dbcRepositories;
#Configuration
#EnableR2dbcRepositories("com.example.repository")
public class DatabaseConfiguration extends AbstractR2dbcConfiguration {
private final Logger log = LoggerFactory.getLogger(DatabaseConfiguration.class);
#Value("${spring.data.mssql.host}")
private String host;
#Value("${spring.data.mssql.database}")
private String database;
#Value("${spring.data.mssql.username}")
private String username;
#Value("${spring.data.mssql.password}")
private String password;
#Bean
#Override
public MssqlConnectionFactory connectionFactory() {
System.out.println("Connecting to database" + host);
return new MssqlConnectionFactory(MssqlConnectionConfiguration.builder()
.host(host)
.port(1453)
.database(database)
.username(username)
.password(password)
.build());
}
}
my DatabaseInitializer:
package com.example.config;
import com.example.domain.Person;
import com.example.repository.PersonRepository;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
#Component
public class DatabaseInitializer {
private final Logger log = LoggerFactory.getLogger(DatabaseInitializer.class);
#Autowired
PersonRepository personRepository;
public DatabaseInitializer(PersonRepository personRepository) {
this.personRepository = personRepository;
}
#PostConstruct
public void init() {
log.info("Initializing database if necessary");
personRepository.findAll().count().subscribe(count -> {
if (count == 0) {
log.info("Database is empty, inserting sample data");
createPerson("Josh", "Long", "Pivotal");
createPerson("Julien", "Dubois", "Microsoft");
} else {
log.info("Database is already initialized");
}
});
}
private void createPerson(String firstName, String lastName, String company) {
Person person = new Person();
person.setFirstName(firstName);
person.setLastName(lastName);
person.setCompany(company);
personRepository.save(person).log().subscribe();
}
}
Person.Java:
package com.example.domain;
import org.springframework.data.annotation.Id;
import org.springframework.data.relational.core.mapping.Table;
#Table("person")
public class Person {
#Id
private Long id;
private String firstName;
private String lastName;
private String company;
and getter/setter
My PersonRepository
package com.example.repository;
import com.example.domain.Person;
import org.springframework.data.repository.reactive.ReactiveCrudRepository;
import org.springframework.stereotype.Repository;
#Repository
public interface PersonRepository extends ReactiveCrudRepository<Person, Long> {
}
And my Controller:
package com.example.web;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.example.domain.Person;
import com.example.repository.PersonRepository;
import reactor.core.publisher.Flux;
#RestController
#RequestMapping("/")
public class PersonController {
private final PersonRepository personRepository;
public PersonController(PersonRepository personRepository) {
this.personRepository = personRepository;
}
#GetMapping("/persons")
public Flux<Person> list() {
return personRepository.findAll();
}
}
My Pom:
<?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.example</groupId>
<artifactId>R2DBC2</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>R2DBC2</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-webflux</artifactId>
</dependency>
<!-- see https://github.com/r2dbc/r2dbc-mssql/issues/77 -->
<dependency>
<groupId>io.projectreactor</groupId>
<artifactId>reactor-core</artifactId>
</dependency>
<dependency>
<groupId>io.r2dbc</groupId>
<artifactId>r2dbc-mssql</artifactId>
<version>1.0.0.M7</version>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-r2dbc</artifactId>
<version>1.0.0.gh-151-SNAPSHOT</version>
</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>io.projectreactor</groupId>
<artifactId>reactor-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
<repositories>
<repository>
<id>spring-libs-snapshot</id>
<url>https://repo.spring.io/libs-snapshot</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
</pluginRepository>
</pluginRepositories>
</project>
Error Message:
This application has no configured error view, so you are seeing this as a fallback.
Tue May 19 12:23:33 CEST 2020
[58026f55-7] There was an unexpected error (type=Not Found, status=404).
org.springframework.web.server.ResponseStatusException: 404 NOT_FOUND
at org.springframework.web.reactive.resource.ResourceWebHandler.lambda$handle$0(ResourceWebHandler.java:325)
Suppressed: reactor.core.publisher.FluxOnAssembly$OnAssemblyException:
Error has been observed at the following site(s):
|_ checkpoint ⇢ HTTP GET "/persons" [ExceptionHandlingWebHandler]
Stack trace:
at org.springframework.web.reactive.resource.ResourceWebHandler.lambda$handle$0(ResourceWebHandler.java:325)
at reactor.core.publisher.MonoDefer.subscribe(MonoDefer.java:44)
at reactor.core.publisher.Mono.subscribe(Mono.java:4210)
at reactor.core.publisher.FluxSwitchIfEmpty$SwitchIfEmptySubscriber.onComplete(FluxSwitchIfEmpty.java:75)
at reactor.core.publisher.MonoFlatMap$FlatMapMain.onComplete(MonoFlatMap.java:174)
at reactor.core.publisher.MonoNext$NextSubscriber.onComplete(MonoNext.java:96)
at reactor.core.publisher.FluxConcatMap$ConcatMapImmediate.drain(FluxConcatMap.java:359)
at reactor.core.publisher.FluxConcatMap$ConcatMapImmediate.onSubscribe(FluxConcatMap.java:21
org.springframework.web.server.ResponseStatusException: 404 NOT_FOUND
It means the endpoint mapping through #GetMapping was not hit. Remember the paths are joined with defined mappings at the class level and then at the method level, the current endpoint would look like:
localhost:8080//persons
This is incorrect.
As long as the #RestController is a root with no further mapping, don't include the "/" character as long as it adds another "layer" or "subpath" in the path. The correct usage would be #RequestMapping("/endpoint-name"). In your case, you don't want an extra "subpath" so omit the annotation:
#RestController
public class PersonController { ... }
Unfortunatelly, I couldn't find any reference to support my statement.

Cant connect MySQL database to my spring project (despite all post i have read) [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
i'am building a small APP, with google api and spring boot, and i need to connect mysql db to my project.
First : all my package are in same places (to avoid this kind of issues TT)
So i first try with the doc exemple ( https://spring.io/guides/gs/accessing-data-mysql/) by cloning the project
and its return me : Failed to obtain JDBC Connection.
I try with my own app and i get this :
Error creating bean with name 'entityManagerFactory' defined in + Failed to obtain JDBC Connection too.
application.properties
spring.jpa.hibernate.ddl-auto=none
spring.datasource.url=jdbc:mysql://localhost:8181/testuser
spring.datasource.username=root
spring.datasource.password=root
spring.jpa.properties.hibernate.dialect =
org.hibernate.dialect.MySQL5Dialect
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
dependencies :
<?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>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.1.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.devoteam.presales</groupId>
<artifactId>testspringsecu</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>testspringsecu</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>11</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.thymeleaf</groupId>
<artifactId>thymeleaf</artifactId>
<version>3.0.11.RELEASE</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>5.2.3.Final</version>
</dependency>
<dependency>
<groupId>com.google.oauth-client</groupId>
<artifactId>google-oauth-client</artifactId>
<version>1.27.0</version>
</dependency>
<dependency>
<groupId>org.thymeleaf</groupId>
<artifactId>thymeleaf-spring5</artifactId>
<version>3.0.11.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.security.oauth.boot</groupId>
<artifactId>spring-security-oauth2-autoconfigure</artifactId>
<version>2.1.2.RELEASE</version>
</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>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</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>org.webjars</groupId>
<artifactId>js-cookie</artifactId>
<version>2.1.0</version>
</dependency>
<dependency>
<groupId>com.google.api-client</groupId>
<artifactId>google-api-client</artifactId>
<version>1.23.0</version>
</dependency>
<dependency>
<groupId>com.google.oauth-client</groupId>
<artifactId>google-oauth-client-jetty</artifactId>
<version>1.23.0</version>
</dependency>
<dependency>
<groupId>com.google.apis</groupId>
<artifactId>google-api-services-sheets</artifactId>
<version>v4-rev493-1.23.0</version>
</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>
<dependency>
<groupId>com.vaadin.external.google</groupId>
<artifactId>android-json</artifactId>
<version>0.0.20131108.vaadin1</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
<repositories>
<repository>
<id>spring-snapshots</id>
<name>Spring Snapshots</name>
<url>https://repo.spring.io/snapshot</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>spring-snapshots</id>
<name>Spring Snapshots</name>
<url>https://repo.spring.io/snapshot</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
</pluginRepository>
<pluginRepository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</pluginRepository>
</pluginRepositories>
Entity :
package com.devoteam.presales.testspringsecu;
import javax.persistence.Entity;
#Entity
public class UsersDevo {
private Integer ID;
private String email;
private String nom;
private String prenom;
private String service;
public Integer getId() {
return ID;
}
public void setId(Integer id) {
this.ID = ID;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getNom() {
return nom;
}
public void setNom(String nom) {
this.nom = nom;
}
public String getPrenom() {
return prenom;
}
public void setPrenom(String prenom) {
this.prenom = prenom;
}
public String getService() {
return service;
}
public void setService(String service) {
this.service = service;
}
}
Repository :
package com.devoteam.presales.testspringsecu;
import com.devoteam.presales.testspringsecu.UsersDevo;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;
#Repository
public interface UsersRepo extends CrudRepository<UsersDevo, Long> {
}
test controller :
package com.devoteam.presales.testspringsecu;
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.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
#Controller
#RequestMapping(path = "/demo")
public class TestController {
#Autowired
UsersRepo usersRepo;
#GetMapping(path = "/all")
public #ResponseBody Iterable<UsersDevo> getAllUsers() {
return usersRepo.findAll();
}
}
main
package com.devoteam.presales.testspringsecu;
import java.security.Principal;
import java.util.*;
import org.json.JSONException;
import org.json.JSONObject;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.security.oauth2.provider.OAuth2Authentication;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
#SpringBootApplication
#RestController
public class TestspringsecuApplication {
#RequestMapping("/user")
public Principal user(Model model, Principal principal) throws
JSONException {
OAuth2Authentication authentication = (OAuth2Authentication)
principal;
return principal;
}
#GetMapping("/user")
public ModelAndView method() {
System.out.println("icila");
return new ModelAndView("redirect:" + "/");
}
public static void main(String[] args) {
SpringApplication.run(TestspringsecuApplication.class, args);
}
}
I also try a bunch of annotation.
EDIT : i fixe the issue witht the doc exemple with :spring.datasource.url=jdbc:mysql://localhost:8181/testuser?serverTimezone=EST5EDT
but i still have : rg.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory'
with my app
I did IT ( after looooog investigation).
And the answer ( for noob like me ) is that your entity must be exactly the same as your table.
my mistake was to forget the #Id
#GeneratedValue(strategy= GenerationType.AUTO) on private Integer ID.
SPring could be painfull some times ... ( often..)
and dont forget ?serverTimezone=EST5EDT after database url.
i hope saving some hours to someone.

Spring and RESTfull ConcurrentModel does not support null attribute value

I m experimenting with Spring and Rest in order to build and Web app for representing a simple list of the cryptocurrencies.
The controller:
package jasmin.merusic.cryptocurrency.controllers;
import jasmin.merusic.cryptocurrency.services.ApiService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
#Controller
public class DataController {
private final ApiService apiService;
public DataController(ApiService apiService) {
this.apiService = apiService;
}
#RequestMapping({"", "/", "/index","cryptos"})
public String index(Model model){
model.addAttribute("cryptos",apiService.getCrypto(10));
return "index";
}
}
And then i have the services package where i have the interface for the Api and the implementation for this service right here(where i m overriding a method ):
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
import java.util.List;
#Service
public class ApiServiceImpl implements ApiService{
private RestTemplate restTemplate;
#Autowired
public ApiServiceImpl(RestTemplate restTemplate) {
this.restTemplate = restTemplate;
}
#Override
public List<Crypto> getCrypto(Integer limit) {
CryptoData crypto = restTemplate.getForObject("https://api.coinmarketcap.com/v2/ticker/?limit=" + limit , CryptoData.class);
return crypto.getCryptos();
}
}
Here is the code of The RestTamplateConfig class
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;
#Configuration
public class RestTemplateConfig {
#Bean
public RestTemplate restTemplate(RestTemplateBuilder builder){
return builder.build();
}
}
And here is the class where i m trying to map from the API: `
public class CryptoData {
private List<Crypto> data;
public List<Crypto> getCryptos() {
return data;
}
public void setCryptos(List<Crypto> data) {
this.data = data;
}
}
public class Crypto implements Serializable
{
private Integer id;
private String name;
private String symbol;
private String websiteSlug;
private Integer rank;
private Double circulatingSupply;
private Double totalSupply;
private Double maxSupply;
private Quotes quotes;
private Integer lastUpdated;
private final static long serialVersionUID = 362556439034076810L;
//getters and setter
`
My POM File looks like this:
<?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>jasmin.merusic</groupId>
<artifactId>cryptocurrency</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>cryptocurrency</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.0.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-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.projectreactor</groupId>
<artifactId>reactor-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
<repositories>
<repository>
<id>spring-snapshots</id>
<name>Spring Snapshots</name>
<url>https://repo.spring.io/snapshot</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>spring-snapshots</id>
<name>Spring Snapshots</name>
<url>https://repo.spring.io/snapshot</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
</pluginRepository>
<pluginRepository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</pluginRepository>
</pluginRepositories>
The problem is that i cannot bind the data to the POJO and i have a error that is
2018-10-19 10:39:35.251 ERROR 14188 --- [ctor-http-nio-2] .a.w.r.e.DefaultErrorWebExceptionHandler : Failed to handle request [GET http://localhost:8080/index]
java.lang.IllegalArgumentException: ConcurrentModel does not support null attribute value
at org.springframework.util.Assert.notNull(Assert.java:193) ~[spring-core-5.0.4.RELEASE.jar:5.0.4.RELEASE]
at org.springframework.ui.ConcurrentModel.addAttribute(ConcurrentModel.java:75) ~[spring-context-5.0.4.RELEASE.jar:5.0.4.RELEASE]
at org.springframework.ui.ConcurrentModel.addAttribute(ConcurrentModel.java:39) ~[spring-context-5.0.4.RELEASE.jar:5.0.4.RELEASE]
at jasmin.merusic.cryptocurrency.controllers.DataController.index(DataController.java:20) ~[classes/:na]
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:na]
any help? i m a newvie to this and i m following something in order to this.
You are doing right, but the problem might be due to the lack of a proper setter/getter for the data field in the CryptoData class. By "proper" I mean getData() and setData(List<Crypto>).
If you look closer at the JSON snippet you are receiving, you would notice that the structure is a bit different from yours. It's actually a Map<Integer, Crypto>, not a List<Crypto>.
class CryptoData {
private Map<Integer, Crypto> data;
public Map<Integer, Crypto> getData() {
return data;
}
public void setData(Map<Integer, Crypto> data) {
this.data = data;
}
}

Categories

Resources