From 40013de660ae0b6855117a53a9a6c1d1ada1e865 Mon Sep 17 00:00:00 2001 From: Saikat Chakraborty Date: Wed, 21 Sep 2022 22:53:50 +0530 Subject: [PATCH] Updated Employee --- .../spring-boot-mysql/mysql-server/generatecerts.sh | 2 +- .../main/java/com/baeldung/boot/jpa/EmployeeController.java | 6 +++++- .../spring-boot-mysql/src/main/resources/application.yml | 4 ++-- 3 files changed, 8 insertions(+), 4 deletions(-) diff --git a/persistence-modules/spring-boot-mysql/mysql-server/generatecerts.sh b/persistence-modules/spring-boot-mysql/mysql-server/generatecerts.sh index c4eb3e70cf..b43a95e6c7 100644 --- a/persistence-modules/spring-boot-mysql/mysql-server/generatecerts.sh +++ b/persistence-modules/spring-boot-mysql/mysql-server/generatecerts.sh @@ -32,4 +32,4 @@ keytool -importcert -alias MySQLCACert.jks -file root-ca.pem \ -keystore truststore.jks -storepass mypassword openssl pkcs12 -export -in client-cert.pem -inkey client-key.pem -out certificate.p12 -name "certificate" -keytool -importkeystore -srckeystore certificate.p12 -srcstoretype pkcs12 -destkeystore cert.jks \ No newline at end of file +keytool -importkeystore -srckeystore certificate.p12 -srcstoretype pkcs12 -destkeystore client-cert.jks \ No newline at end of file diff --git a/persistence-modules/spring-boot-mysql/src/main/java/com/baeldung/boot/jpa/EmployeeController.java b/persistence-modules/spring-boot-mysql/src/main/java/com/baeldung/boot/jpa/EmployeeController.java index 8b6a233b45..663e28b82c 100644 --- a/persistence-modules/spring-boot-mysql/src/main/java/com/baeldung/boot/jpa/EmployeeController.java +++ b/persistence-modules/spring-boot-mysql/src/main/java/com/baeldung/boot/jpa/EmployeeController.java @@ -2,6 +2,9 @@ package com.baeldung.boot.jpa; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; +import org.springframework.web.client.HttpClientErrorException; + +import java.util.Optional; @RestController public class EmployeeController { @@ -11,7 +14,8 @@ public class EmployeeController { @GetMapping("/employee/{empId}") public Employee get(@PathVariable(name = "empId") Integer empId) { - return empRepository.findById(empId).get(); + Optional emp = empRepository.findById(empId); + return emp.orElse(null); } @PostMapping("/employee") diff --git a/persistence-modules/spring-boot-mysql/src/main/resources/application.yml b/persistence-modules/spring-boot-mysql/src/main/resources/application.yml index d7c559d92f..d70c21aad2 100644 --- a/persistence-modules/spring-boot-mysql/src/main/resources/application.yml +++ b/persistence-modules/spring-boot-mysql/src/main/resources/application.yml @@ -26,10 +26,10 @@ spring: profiles: "dev2" datasource: url: >- - jdbc:mysql://localhost:3306/test_db?sslMode=VERIFY_IDENTITY& + jdbc:mysql://localhost:3306/test_db?sslMode=VERIFY_CA& trustCertificateKeyStoreUrl=file:/Users/saikatchakraborty/tutorials/persistence-modules/spring-boot-mysql/mysql-server/certs/truststore.jks& trustCertificateKeyStorePassword=mypassword& - clientCertificateKeyStoreUrl=file:/Users/saikatchakraborty/tutorials/persistence-modules/spring-boot-mysql/mysql-server/certs/cert.jks& + clientCertificateKeyStoreUrl=file:/Users/saikatchakraborty/tutorials/persistence-modules/spring-boot-mysql/mysql-server/certs/client-cert.jks& clientCertificateKeyStorePassword=mypassword username: test_user password: Password2022 \ No newline at end of file