greetingMono);
13 | }
14 |
--------------------------------------------------------------------------------
/rsocket/src/main/resources/application.properties:
--------------------------------------------------------------------------------
1 | spring.rsocket.server.port=7000
--------------------------------------------------------------------------------
/rsocket/src/test/java/com/howtodoinjava/AppTest.java:
--------------------------------------------------------------------------------
1 | package com.howtodoinjava;
2 |
3 | import static org.junit.jupiter.api.Assertions.assertTrue;
4 | import org.junit.jupiter.api.Test;
5 |
6 | public class AppTest {
7 |
8 | @Test
9 | public void contextLoads() {
10 | assertTrue(true);
11 | }
12 | }
13 |
--------------------------------------------------------------------------------
/security/pom.xml:
--------------------------------------------------------------------------------
1 |
4 | 4.0.0
5 |
6 |
7 | com.howtodoinjava
8 | Spring-Boot3-Demos
9 | 1.0-SNAPSHOT
10 |
11 |
12 | security
13 | jar
14 | security
15 | https://howtodoinjava.com
16 |
17 |
18 |
19 | org.springframework.boot
20 | spring-boot-starter-web
21 |
22 |
23 | org.springframework.boot
24 | spring-boot-starter-data-jpa
25 |
26 |
27 | com.h2database
28 | h2
29 | runtime
30 |
31 |
32 | org.springframework.boot
33 | spring-boot-starter-security
34 |
35 |
36 |
37 |
--------------------------------------------------------------------------------
/security/src/main/java/com/howtodoinjava/App.java:
--------------------------------------------------------------------------------
1 | package com.howtodoinjava;
2 |
3 | import org.springframework.boot.SpringApplication;
4 | import org.springframework.boot.autoconfigure.SpringBootApplication;
5 |
6 | @SpringBootApplication
7 | public class App {
8 |
9 | public static void main(String[] args) {
10 | SpringApplication.run(App.class, args);
11 | }
12 | }
13 |
--------------------------------------------------------------------------------
/security/src/main/resources/application.properties:
--------------------------------------------------------------------------------
1 | spring.datasource.url=jdbc:h2:mem:testdb
2 | spring.datasource.driverClassName=org.h2.Driver
3 | spring.datasource.username=sa
4 | spring.datasource.password=
5 | spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
--------------------------------------------------------------------------------
/security/src/test/java/com/howtodoinjava/AppTest.java:
--------------------------------------------------------------------------------
1 | package com.howtodoinjava;
2 |
3 | import org.junit.jupiter.api.Assertions;
4 | import org.junit.jupiter.api.Test;
5 | import org.springframework.boot.test.context.SpringBootTest;
6 |
7 | @SpringBootTest
8 | public class AppTest {
9 |
10 | @Test
11 | public void contextLoads() {
12 | Assertions.assertTrue(true);
13 | }
14 | }
15 |
--------------------------------------------------------------------------------
/soap-client-example/pom.xml:
--------------------------------------------------------------------------------
1 |
4 | 4.0.0
5 |
6 | com.howtodoinjava
7 | Spring-Boot3-Demos
8 | 1.0-SNAPSHOT
9 |
10 |
11 | soap-client-example
12 | jar
13 |
14 | soap-client-example
15 | http://maven.apache.org
16 |
17 |
18 | UTF-8
19 |
20 |
21 |
22 |
23 | org.springframework.boot
24 | spring-boot-starter-web-services
25 |
26 |
27 | wsdl4j
28 | wsdl4j
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 | org.jvnet.jaxb2.maven2
37 | maven-jaxb2-plugin
38 | 0.15.3
39 |
40 |
41 |
42 | generate
43 |
44 |
45 |
46 |
47 | com.example.howtodoinjava.schemas.school
48 | ${project.basedir}/src/main/java
49 | ${project.basedir}/src/main/resources/wsdl
50 |
51 | *.wsdl
52 |
53 |
54 |
55 |
56 |
57 |
58 |
59 |
--------------------------------------------------------------------------------
/soap-client-example/src/main/java/META-INF/sun-jaxb.episode:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
--------------------------------------------------------------------------------
/soap-client-example/src/main/java/com/example/howtodoinjava/schemas/school/ObjectFactory.java:
--------------------------------------------------------------------------------
1 | //
2 | // This file was generated by the Eclipse Implementation of JAXB, v2.3.7
3 | // See https://eclipse-ee4j.github.io/jaxb-ri
4 | // Any modifications to this file will be lost upon recompilation of the source schema.
5 | // Generated on: 2024.05.29 at 03:20:02 PM IST
6 | //
7 |
8 |
9 | package com.example.howtodoinjava.schemas.school;
10 |
11 | import jakarta.xml.bind.annotation.XmlRegistry;
12 |
13 |
14 | /**
15 | * This object contains factory methods for each
16 | * Java content interface and Java element interface
17 | * generated in the com.example.howtodoinjava.schemas.school package.
18 | * An ObjectFactory allows you to programatically
19 | * construct new instances of the Java representation
20 | * for XML content. The Java representation of XML
21 | * content can consist of schema derived interfaces
22 | * and classes representing the binding of schema
23 | * type definitions, element declarations and model
24 | * groups. Factory methods for each of these are
25 | * provided in this class.
26 | *
27 | */
28 | @XmlRegistry
29 | public class ObjectFactory {
30 |
31 |
32 | /**
33 | * Create a new ObjectFactory that can be used to create new instances of schema derived classes for package: com.example.howtodoinjava.schemas.school
34 | *
35 | */
36 | public ObjectFactory() {
37 | }
38 |
39 | /**
40 | * Create an instance of {@link StudentDetailsRequest }
41 | *
42 | */
43 | public StudentDetailsRequest createStudentDetailsRequest() {
44 | return new StudentDetailsRequest();
45 | }
46 |
47 | /**
48 | * Create an instance of {@link StudentDetailsResponse }
49 | *
50 | */
51 | public StudentDetailsResponse createStudentDetailsResponse() {
52 | return new StudentDetailsResponse();
53 | }
54 |
55 | /**
56 | * Create an instance of {@link Student }
57 | *
58 | */
59 | public Student createStudent() {
60 | return new Student();
61 | }
62 |
63 | }
64 |
--------------------------------------------------------------------------------
/soap-client-example/src/main/java/com/example/howtodoinjava/schemas/school/StudentDetailsRequest.java:
--------------------------------------------------------------------------------
1 | //
2 | // This file was generated by the Eclipse Implementation of JAXB, v2.3.7
3 | // See https://eclipse-ee4j.github.io/jaxb-ri
4 | // Any modifications to this file will be lost upon recompilation of the source schema.
5 | // Generated on: 2024.05.29 at 03:20:02 PM IST
6 | //
7 |
8 |
9 | package com.example.howtodoinjava.schemas.school;
10 |
11 | import jakarta.xml.bind.annotation.XmlAccessType;
12 | import jakarta.xml.bind.annotation.XmlAccessorType;
13 | import jakarta.xml.bind.annotation.XmlElement;
14 | import jakarta.xml.bind.annotation.XmlRootElement;
15 | import jakarta.xml.bind.annotation.XmlType;
16 |
17 |
18 | /**
19 | *
Java class for anonymous complex type.
20 | *
21 | *
The following schema fragment specifies the expected content contained within this class.
22 | *
23 | *
24 | * <complexType>
25 | * <complexContent>
26 | * <restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
27 | * <sequence>
28 | * <element name="name" type="{http://www.w3.org/2001/XMLSchema}string"/>
29 | * </sequence>
30 | * </restriction>
31 | * </complexContent>
32 | * </complexType>
33 | *
34 | *
35 | *
36 | */
37 | @XmlAccessorType(XmlAccessType.FIELD)
38 | @XmlType(name = "", propOrder = {
39 | "name"
40 | })
41 | @XmlRootElement(name = "StudentDetailsRequest")
42 | public class StudentDetailsRequest {
43 |
44 | @XmlElement(required = true)
45 | protected String name;
46 |
47 | /**
48 | * Gets the value of the name property.
49 | *
50 | * @return
51 | * possible object is
52 | * {@link String }
53 | *
54 | */
55 | public String getName() {
56 | return name;
57 | }
58 |
59 | /**
60 | * Sets the value of the name property.
61 | *
62 | * @param value
63 | * allowed object is
64 | * {@link String }
65 | *
66 | */
67 | public void setName(String value) {
68 | this.name = value;
69 | }
70 |
71 | }
72 |
--------------------------------------------------------------------------------
/soap-client-example/src/main/java/com/example/howtodoinjava/schemas/school/StudentDetailsResponse.java:
--------------------------------------------------------------------------------
1 | //
2 | // This file was generated by the Eclipse Implementation of JAXB, v2.3.7
3 | // See https://eclipse-ee4j.github.io/jaxb-ri
4 | // Any modifications to this file will be lost upon recompilation of the source schema.
5 | // Generated on: 2024.05.29 at 03:20:02 PM IST
6 | //
7 |
8 |
9 | package com.example.howtodoinjava.schemas.school;
10 |
11 | import jakarta.xml.bind.annotation.XmlAccessType;
12 | import jakarta.xml.bind.annotation.XmlAccessorType;
13 | import jakarta.xml.bind.annotation.XmlElement;
14 | import jakarta.xml.bind.annotation.XmlRootElement;
15 | import jakarta.xml.bind.annotation.XmlType;
16 |
17 |
18 | /**
19 | * Java class for anonymous complex type.
20 | *
21 | *
The following schema fragment specifies the expected content contained within this class.
22 | *
23 | *
24 | * <complexType>
25 | * <complexContent>
26 | * <restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
27 | * <sequence>
28 | * <element name="Student" type="{http://www.howtodoinjava.com/xml/school}Student"/>
29 | * </sequence>
30 | * </restriction>
31 | * </complexContent>
32 | * </complexType>
33 | *
34 | *
35 | *
36 | */
37 | @XmlAccessorType(XmlAccessType.FIELD)
38 | @XmlType(name = "", propOrder = {
39 | "student"
40 | })
41 | @XmlRootElement(name = "StudentDetailsResponse")
42 | public class StudentDetailsResponse {
43 |
44 | @XmlElement(name = "Student", required = true)
45 | protected Student student;
46 |
47 | /**
48 | * Gets the value of the student property.
49 | *
50 | * @return
51 | * possible object is
52 | * {@link Student }
53 | *
54 | */
55 | public Student getStudent() {
56 | return student;
57 | }
58 |
59 | /**
60 | * Sets the value of the student property.
61 | *
62 | * @param value
63 | * allowed object is
64 | * {@link Student }
65 | *
66 | */
67 | public void setStudent(Student value) {
68 | this.student = value;
69 | }
70 |
71 | }
72 |
--------------------------------------------------------------------------------
/soap-client-example/src/main/java/com/example/howtodoinjava/schemas/school/package-info.java:
--------------------------------------------------------------------------------
1 | //
2 | // This file was generated by the Eclipse Implementation of JAXB, v2.3.7
3 | // See https://eclipse-ee4j.github.io/jaxb-ri
4 | // Any modifications to this file will be lost upon recompilation of the source schema.
5 | // Generated on: 2024.05.29 at 03:20:02 PM IST
6 | //
7 |
8 | @jakarta.xml.bind.annotation.XmlSchema(namespace = "http://www.howtodoinjava.com/xml/school", elementFormDefault = jakarta.xml.bind.annotation.XmlNsForm.QUALIFIED)
9 | package com.example.howtodoinjava.schemas.school;
10 |
--------------------------------------------------------------------------------
/soap-client-example/src/main/java/com/howtodoinjava/App.java:
--------------------------------------------------------------------------------
1 | package com.howtodoinjava;
2 |
3 | import com.howtodoinjava.soap.client.SOAPConnector;
4 | import org.springframework.boot.CommandLineRunner;
5 | import org.springframework.boot.SpringApplication;
6 | import org.springframework.boot.autoconfigure.SpringBootApplication;
7 | import org.springframework.context.annotation.Bean;
8 | import com.example.howtodoinjava.schemas.school.StudentDetailsRequest;
9 | import com.example.howtodoinjava.schemas.school.StudentDetailsResponse;
10 |
11 | @SpringBootApplication
12 | public class App {
13 |
14 | public static void main(String[] args) {
15 | SpringApplication.run(App.class, args);
16 | }
17 |
18 | @Bean
19 | CommandLineRunner lookup(SOAPConnector soapConnector) {
20 | return args -> {
21 | String name = "Lokesh"; //Default Name
22 | if (args.length > 0) {
23 | name = args[0];
24 | }
25 | StudentDetailsRequest request = new StudentDetailsRequest();
26 | request.setName(name);
27 | StudentDetailsResponse response = (StudentDetailsResponse) soapConnector.callWebService("http://localhost:8080/service/student-details", request);
28 | System.out.println("Got Response As below ========= : ");
29 | System.out.println("Name : " + response.getStudent().getName());
30 | System.out.println("Standard : " + response.getStudent().getStandard());
31 | System.out.println("Address : " + response.getStudent().getAddress());
32 | };
33 | }
34 | }
35 |
--------------------------------------------------------------------------------
/soap-client-example/src/main/java/com/howtodoinjava/soap/client/Config.java:
--------------------------------------------------------------------------------
1 | package com.howtodoinjava.soap.client;
2 |
3 | import org.springframework.context.annotation.Bean;
4 | import org.springframework.context.annotation.Configuration;
5 | import org.springframework.oxm.jaxb.Jaxb2Marshaller;
6 |
7 | @Configuration
8 | public class Config {
9 | @Bean
10 | public Jaxb2Marshaller marshaller() {
11 | Jaxb2Marshaller marshaller = new Jaxb2Marshaller();
12 | // this is the package name specified in the specified in
13 | // pom.xml
14 | marshaller.setContextPath("com.example.howtodoinjava.schemas.school");
15 | return marshaller;
16 | }
17 |
18 | @Bean
19 | public SOAPConnector soapConnector(Jaxb2Marshaller marshaller) {
20 | SOAPConnector client = new SOAPConnector();
21 | client.setDefaultUri("http://localhost:8080/service/student-details");
22 | client.setMarshaller(marshaller);
23 | client.setUnmarshaller(marshaller);
24 | return client;
25 | }
26 | }
27 |
--------------------------------------------------------------------------------
/soap-client-example/src/main/java/com/howtodoinjava/soap/client/SOAPConnector.java:
--------------------------------------------------------------------------------
1 | package com.howtodoinjava.soap.client;
2 |
3 | import org.springframework.ws.client.core.support.WebServiceGatewaySupport;
4 |
5 | public class SOAPConnector extends WebServiceGatewaySupport {
6 |
7 | public Object callWebService(String url, Object request){
8 | return getWebServiceTemplate().marshalSendAndReceive(url, request);
9 | }
10 | }
11 |
--------------------------------------------------------------------------------
/soap-client-example/src/main/resources/application.properties:
--------------------------------------------------------------------------------
1 | server.port = 9090
2 | logging.level.org.springframework.ws=TRACE
3 |
--------------------------------------------------------------------------------
/soap-ws-example/pom.xml:
--------------------------------------------------------------------------------
1 |
4 | 4.0.0
5 |
6 | com.howtodoinjava
7 | Spring-Boot3-Demos
8 | 1.0-SNAPSHOT
9 |
10 |
11 | soap-ws-example
12 | jar
13 |
14 | soap-ws-example
15 | http://maven.apache.org
16 |
17 |
18 | UTF-8
19 |
20 |
21 |
22 |
23 | org.springframework.boot
24 | spring-boot-starter-web-services
25 |
26 |
27 | wsdl4j
28 | wsdl4j
29 |
30 |
31 |
32 |
33 |
34 |
35 | org.codehaus.mojo
36 | jaxb2-maven-plugin
37 | 3.1.0
38 |
39 |
40 | xjc
41 |
42 | xjc
43 |
44 |
45 |
46 |
47 |
48 | src/main/resources/student.xsd
49 |
50 | src/main/java
51 | false
52 |
53 |
54 |
55 |
56 |
57 |
58 |
--------------------------------------------------------------------------------
/soap-ws-example/src/main/java/META-INF/JAXB/episode_xjc.xjb:
--------------------------------------------------------------------------------
1 |
2 |
3 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
--------------------------------------------------------------------------------
/soap-ws-example/src/main/java/com/howtodoinjava/App.java:
--------------------------------------------------------------------------------
1 | package com.howtodoinjava;
2 |
3 | import org.springframework.boot.SpringApplication;
4 | import org.springframework.boot.autoconfigure.SpringBootApplication;
5 |
6 | @SpringBootApplication
7 | public class App {
8 | public static void main(String[] args) {
9 | SpringApplication.run(App.class);
10 | }
11 | }
12 |
--------------------------------------------------------------------------------
/soap-ws-example/src/main/java/com/howtodoinjava/config/SoapConfig.java:
--------------------------------------------------------------------------------
1 | package com.howtodoinjava.config;
2 |
3 | import org.springframework.boot.web.servlet.ServletRegistrationBean;
4 | import org.springframework.context.ApplicationContext;
5 | import org.springframework.context.annotation.Bean;
6 | import org.springframework.context.annotation.Configuration;
7 | import org.springframework.core.io.ClassPathResource;
8 | import org.springframework.ws.config.annotation.EnableWs;
9 | import org.springframework.ws.config.annotation.WsConfigurerAdapter;
10 | import org.springframework.ws.transport.http.MessageDispatcherServlet;
11 | import org.springframework.ws.wsdl.wsdl11.DefaultWsdl11Definition;
12 | import org.springframework.xml.xsd.SimpleXsdSchema;
13 | import org.springframework.xml.xsd.XsdSchema;
14 |
15 | @EnableWs
16 | @Configuration
17 | public class SoapConfig extends WsConfigurerAdapter {
18 |
19 | @Bean
20 | public ServletRegistrationBean messageDispatcherServlet(ApplicationContext applicationContext) {
21 | MessageDispatcherServlet servlet = new MessageDispatcherServlet();
22 | servlet.setApplicationContext(applicationContext);
23 | servlet.setTransformWsdlLocations(true);
24 | return new ServletRegistrationBean(servlet, "/service/*");
25 | }
26 |
27 | @Bean(name = "studentDetailsWsdl")
28 | public DefaultWsdl11Definition defaultWsdl11Definition(XsdSchema studentSchema) {
29 | DefaultWsdl11Definition wsdl11Definition = new DefaultWsdl11Definition();
30 | wsdl11Definition.setPortTypeName("StudentDetailsPort");
31 | wsdl11Definition.setLocationUri("/service/student-details");
32 | wsdl11Definition.setTargetNamespace("http://www.howtodoinjava.com/xml/school");
33 | wsdl11Definition.setSchema(studentSchema);
34 | return wsdl11Definition;
35 | }
36 |
37 | @Bean
38 | public XsdSchema studentSchema() {
39 | return new SimpleXsdSchema(new ClassPathResource("student.xsd"));
40 | }
41 | }
42 |
--------------------------------------------------------------------------------
/soap-ws-example/src/main/java/com/howtodoinjava/endpoints/StudentEndpoint.java:
--------------------------------------------------------------------------------
1 | package com.howtodoinjava.endpoints;
2 |
3 | import com.howtodoinjava.repository.StudentRepository;
4 | import org.springframework.beans.factory.annotation.Autowired;
5 | import org.springframework.ws.server.endpoint.annotation.Endpoint;
6 | import org.springframework.ws.server.endpoint.annotation.PayloadRoot;
7 | import org.springframework.ws.server.endpoint.annotation.RequestPayload;
8 | import org.springframework.ws.server.endpoint.annotation.ResponsePayload;
9 | import com.howtodoinjava.xml.school.StudentDetailsRequest;
10 | import com.howtodoinjava.xml.school.StudentDetailsResponse;
11 |
12 | @Endpoint
13 | public class StudentEndpoint
14 | {
15 | private static final String NAMESPACE_URI = "http://www.howtodoinjava.com/xml/school";
16 |
17 | private StudentRepository StudentRepository;
18 |
19 | @Autowired
20 | public StudentEndpoint(StudentRepository StudentRepository) {
21 | this.StudentRepository = StudentRepository;
22 | }
23 |
24 | @PayloadRoot(namespace = NAMESPACE_URI, localPart = "StudentDetailsRequest")
25 | @ResponsePayload
26 | public StudentDetailsResponse getStudent(@RequestPayload StudentDetailsRequest request) {
27 | StudentDetailsResponse response = new StudentDetailsResponse();
28 | response.setStudent(StudentRepository.findStudent(request.getName()));
29 |
30 | return response;
31 | }
32 | }
33 |
--------------------------------------------------------------------------------
/soap-ws-example/src/main/java/com/howtodoinjava/repository/StudentRepository.java:
--------------------------------------------------------------------------------
1 | package com.howtodoinjava.repository;
2 |
3 | import java.util.HashMap;
4 | import java.util.Map;
5 | import jakarta.annotation.PostConstruct;
6 | import org.springframework.stereotype.Component;
7 | import org.springframework.util.Assert;
8 | import com.howtodoinjava.xml.school.Student;
9 |
10 | @Component
11 | public class StudentRepository {
12 | private static final Map students = new HashMap<>();
13 |
14 | @PostConstruct
15 | public void initData() {
16 |
17 | Student student = new Student();
18 | student.setName("Sajal");
19 | student.setStandard(5);
20 | student.setAddress("Pune");
21 | students.put(student.getName(), student);
22 |
23 | student = new Student();
24 | student.setName("Kajal");
25 | student.setStandard(5);
26 | student.setAddress("Chicago");
27 | students.put(student.getName(), student);
28 |
29 | student = new Student();
30 | student.setName("Lokesh");
31 | student.setStandard(6);
32 | student.setAddress("Delhi");
33 | students.put(student.getName(), student);
34 |
35 | student = new Student();
36 | student.setName("Sukesh");
37 | student.setStandard(7);
38 | student.setAddress("Noida");
39 | students.put(student.getName(), student);
40 | }
41 |
42 | public Student findStudent(String name) {
43 | Assert.notNull(name, "The Student's name must not be null");
44 | return students.get(name);
45 | }
46 | }
47 |
--------------------------------------------------------------------------------
/soap-ws-example/src/main/java/com/howtodoinjava/xml/school/ObjectFactory.java:
--------------------------------------------------------------------------------
1 | //
2 | // This file was generated by the Eclipse Implementation of JAXB, v3.0.0
3 | // See https://eclipse-ee4j.github.io/jaxb-ri
4 | // Any modifications to this file will be lost upon recompilation of the source schema.
5 | // Generated on: 2024.05.29 at 01:06:41 PM IST
6 | //
7 |
8 |
9 | package com.howtodoinjava.xml.school;
10 |
11 | import jakarta.xml.bind.annotation.XmlRegistry;
12 |
13 |
14 | /**
15 | * This object contains factory methods for each
16 | * Java content interface and Java element interface
17 | * generated in the com.howtodoinjava.xml.school package.
18 | * An ObjectFactory allows you to programatically
19 | * construct new instances of the Java representation
20 | * for XML content. The Java representation of XML
21 | * content can consist of schema derived interfaces
22 | * and classes representing the binding of schema
23 | * type definitions, element declarations and model
24 | * groups. Factory methods for each of these are
25 | * provided in this class.
26 | *
27 | */
28 | @XmlRegistry
29 | public class ObjectFactory {
30 |
31 |
32 | /**
33 | * Create a new ObjectFactory that can be used to create new instances of schema derived classes for package: com.howtodoinjava.xml.school
34 | *
35 | */
36 | public ObjectFactory() {
37 | }
38 |
39 | /**
40 | * Create an instance of {@link StudentDetailsRequest }
41 | *
42 | */
43 | public StudentDetailsRequest createStudentDetailsRequest() {
44 | return new StudentDetailsRequest();
45 | }
46 |
47 | /**
48 | * Create an instance of {@link StudentDetailsResponse }
49 | *
50 | */
51 | public StudentDetailsResponse createStudentDetailsResponse() {
52 | return new StudentDetailsResponse();
53 | }
54 |
55 | /**
56 | * Create an instance of {@link Student }
57 | *
58 | */
59 | public Student createStudent() {
60 | return new Student();
61 | }
62 |
63 | }
64 |
--------------------------------------------------------------------------------
/soap-ws-example/src/main/java/com/howtodoinjava/xml/school/StudentDetailsRequest.java:
--------------------------------------------------------------------------------
1 | //
2 | // This file was generated by the Eclipse Implementation of JAXB, v3.0.0
3 | // See https://eclipse-ee4j.github.io/jaxb-ri
4 | // Any modifications to this file will be lost upon recompilation of the source schema.
5 | // Generated on: 2024.05.29 at 01:06:41 PM IST
6 | //
7 |
8 |
9 | package com.howtodoinjava.xml.school;
10 |
11 | import jakarta.xml.bind.annotation.XmlAccessType;
12 | import jakarta.xml.bind.annotation.XmlAccessorType;
13 | import jakarta.xml.bind.annotation.XmlElement;
14 | import jakarta.xml.bind.annotation.XmlRootElement;
15 | import jakarta.xml.bind.annotation.XmlType;
16 |
17 |
18 | /**
19 | *
Java class for anonymous complex type.
20 | *
21 | *
The following schema fragment specifies the expected content contained within this class.
22 | *
23 | *
24 | * <complexType>
25 | * <complexContent>
26 | * <restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
27 | * <sequence>
28 | * <element name="name" type="{http://www.w3.org/2001/XMLSchema}string"/>
29 | * </sequence>
30 | * </restriction>
31 | * </complexContent>
32 | * </complexType>
33 | *
34 | *
35 | *
36 | */
37 | @XmlAccessorType(XmlAccessType.FIELD)
38 | @XmlType(name = "", propOrder = {
39 | "name"
40 | })
41 | @XmlRootElement(name = "StudentDetailsRequest")
42 | public class StudentDetailsRequest {
43 |
44 | @XmlElement(required = true)
45 | protected String name;
46 |
47 | /**
48 | * Gets the value of the name property.
49 | *
50 | * @return
51 | * possible object is
52 | * {@link String }
53 | *
54 | */
55 | public String getName() {
56 | return name;
57 | }
58 |
59 | /**
60 | * Sets the value of the name property.
61 | *
62 | * @param value
63 | * allowed object is
64 | * {@link String }
65 | *
66 | */
67 | public void setName(String value) {
68 | this.name = value;
69 | }
70 |
71 | }
72 |
--------------------------------------------------------------------------------
/soap-ws-example/src/main/java/com/howtodoinjava/xml/school/StudentDetailsResponse.java:
--------------------------------------------------------------------------------
1 | //
2 | // This file was generated by the Eclipse Implementation of JAXB, v3.0.0
3 | // See https://eclipse-ee4j.github.io/jaxb-ri
4 | // Any modifications to this file will be lost upon recompilation of the source schema.
5 | // Generated on: 2024.05.29 at 01:06:41 PM IST
6 | //
7 |
8 |
9 | package com.howtodoinjava.xml.school;
10 |
11 | import jakarta.xml.bind.annotation.XmlAccessType;
12 | import jakarta.xml.bind.annotation.XmlAccessorType;
13 | import jakarta.xml.bind.annotation.XmlElement;
14 | import jakarta.xml.bind.annotation.XmlRootElement;
15 | import jakarta.xml.bind.annotation.XmlType;
16 |
17 |
18 | /**
19 | * Java class for anonymous complex type.
20 | *
21 | *
The following schema fragment specifies the expected content contained within this class.
22 | *
23 | *
24 | * <complexType>
25 | * <complexContent>
26 | * <restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
27 | * <sequence>
28 | * <element name="Student" type="{http://www.howtodoinjava.com/xml/school}Student"/>
29 | * </sequence>
30 | * </restriction>
31 | * </complexContent>
32 | * </complexType>
33 | *
34 | *
35 | *
36 | */
37 | @XmlAccessorType(XmlAccessType.FIELD)
38 | @XmlType(name = "", propOrder = {
39 | "student"
40 | })
41 | @XmlRootElement(name = "StudentDetailsResponse")
42 | public class StudentDetailsResponse {
43 |
44 | @XmlElement(name = "Student", required = true)
45 | protected Student student;
46 |
47 | /**
48 | * Gets the value of the student property.
49 | *
50 | * @return
51 | * possible object is
52 | * {@link Student }
53 | *
54 | */
55 | public Student getStudent() {
56 | return student;
57 | }
58 |
59 | /**
60 | * Sets the value of the student property.
61 | *
62 | * @param value
63 | * allowed object is
64 | * {@link Student }
65 | *
66 | */
67 | public void setStudent(Student value) {
68 | this.student = value;
69 | }
70 |
71 | }
72 |
--------------------------------------------------------------------------------
/soap-ws-example/src/main/java/com/howtodoinjava/xml/school/package-info.java:
--------------------------------------------------------------------------------
1 | //
2 | // This file was generated by the Eclipse Implementation of JAXB, v3.0.0
3 | // See https://eclipse-ee4j.github.io/jaxb-ri
4 | // Any modifications to this file will be lost upon recompilation of the source schema.
5 | // Generated on: 2024.05.29 at 01:06:41 PM IST
6 | //
7 |
8 | @jakarta.xml.bind.annotation.XmlSchema(namespace = "http://www.howtodoinjava.com/xml/school", elementFormDefault = jakarta.xml.bind.annotation.XmlNsForm.QUALIFIED)
9 | package com.howtodoinjava.xml.school;
10 |
--------------------------------------------------------------------------------
/soap-ws-example/src/main/resources/student.xsd:
--------------------------------------------------------------------------------
1 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
--------------------------------------------------------------------------------
/spring-boot-hateoas/pom.xml:
--------------------------------------------------------------------------------
1 |
4 | 4.0.0
5 |
6 | com.howtodoinjava
7 | Spring-Boot3-Demos
8 | 1.0-SNAPSHOT
9 |
10 |
11 | spring-boot-hateoas
12 | jar
13 | spring-boot-hateoas
14 | https://howtodoinjava.com
15 |
16 |
17 |
18 | org.springframework.boot
19 | spring-boot-starter-web
20 |
21 |
22 | org.springframework.boot
23 | spring-boot-starter-data-jpa
24 |
25 |
26 | org.springframework.boot
27 | spring-boot-starter-hateoas
28 |
29 |
30 | com.h2database
31 | h2
32 | runtime
33 |
34 |
35 |
36 |
--------------------------------------------------------------------------------
/spring-boot-hateoas/src/main/java/com/howtodoinjava/App.java:
--------------------------------------------------------------------------------
1 | package com.howtodoinjava;
2 |
3 | import org.springframework.boot.SpringApplication;
4 | import org.springframework.boot.autoconfigure.SpringBootApplication;
5 | import org.springframework.context.annotation.Bean;
6 | import org.springframework.web.filter.ForwardedHeaderFilter;
7 |
8 | @SpringBootApplication
9 | //@EnableAutoConfiguration(exclude = HypermediaAutoConfiguration.class)
10 | public class App {
11 |
12 | public static void main(String[] args) {
13 | SpringApplication.run(App.class, args);
14 | }
15 |
16 | @Bean
17 | ForwardedHeaderFilter forwardedHeaderFilter() {
18 | return new ForwardedHeaderFilter();
19 | }
20 | }
21 |
--------------------------------------------------------------------------------
/spring-boot-hateoas/src/main/java/com/howtodoinjava/rest/entity/ActorEntity.java:
--------------------------------------------------------------------------------
1 | package com.howtodoinjava.rest.entity;
2 |
3 | import java.io.Serializable;
4 | import java.util.List;
5 |
6 | import jakarta.persistence.CascadeType;
7 | import jakarta.persistence.Entity;
8 | import jakarta.persistence.GeneratedValue;
9 | import jakarta.persistence.GenerationType;
10 | import jakarta.persistence.Id;
11 | import jakarta.persistence.JoinColumn;
12 | import jakarta.persistence.JoinTable;
13 | import jakarta.persistence.ManyToMany;
14 | import jakarta.persistence.Table;
15 |
16 | import lombok.AllArgsConstructor;
17 | import lombok.Builder;
18 | import lombok.Data;
19 | import lombok.NoArgsConstructor;
20 | import lombok.ToString;
21 |
22 | @Data
23 | @Builder
24 | @AllArgsConstructor
25 | @NoArgsConstructor
26 | @ToString(exclude = "albums")
27 | @Entity
28 | @Table(name="actor")
29 | public class ActorEntity implements Serializable
30 | {
31 | private static final long serialVersionUID = 1L;
32 |
33 | @Id
34 | @GeneratedValue(strategy = GenerationType.IDENTITY)
35 | private Long id;
36 | private String firstName;
37 | private String lastName;
38 | private String birthDate;
39 |
40 | @ManyToMany(cascade=CascadeType.ALL)
41 | @JoinTable(
42 | name = "actor_album",
43 | joinColumns = @JoinColumn(name = "actor_id"),
44 | inverseJoinColumns = @JoinColumn(name = "album_id"))
45 | private List albums;
46 | }
47 |
--------------------------------------------------------------------------------
/spring-boot-hateoas/src/main/java/com/howtodoinjava/rest/entity/AlbumEntity.java:
--------------------------------------------------------------------------------
1 | package com.howtodoinjava.rest.entity;
2 |
3 | import java.io.Serializable;
4 | import java.util.List;
5 |
6 | import jakarta.persistence.Entity;
7 | import jakarta.persistence.FetchType;
8 | import jakarta.persistence.GeneratedValue;
9 | import jakarta.persistence.GenerationType;
10 | import jakarta.persistence.Id;
11 | import jakarta.persistence.ManyToMany;
12 | import jakarta.persistence.Table;
13 |
14 | import lombok.AllArgsConstructor;
15 | import lombok.Builder;
16 | import lombok.Data;
17 | import lombok.NoArgsConstructor;
18 | import lombok.ToString;
19 |
20 | @Data
21 | @Builder
22 | @AllArgsConstructor
23 | @NoArgsConstructor
24 | @Entity
25 | @ToString(exclude = "actors")
26 | @Table(name="album")
27 | public class AlbumEntity implements Serializable
28 | {
29 | private static final long serialVersionUID = 1L;
30 |
31 | @Id
32 | @GeneratedValue(strategy = GenerationType.IDENTITY)
33 | private Long id;
34 | private String title;
35 | private String description;
36 | private String releaseDate;
37 |
38 | @ManyToMany(mappedBy = "albums",fetch = FetchType.EAGER)
39 | private List actors;
40 | }
41 |
--------------------------------------------------------------------------------
/spring-boot-hateoas/src/main/java/com/howtodoinjava/rest/model/ActorModel.java:
--------------------------------------------------------------------------------
1 | package com.howtodoinjava.rest.model;
2 |
3 | import java.util.List;
4 |
5 | import org.springframework.hateoas.RepresentationModel;
6 | import org.springframework.hateoas.server.core.Relation;
7 |
8 | import com.fasterxml.jackson.annotation.JsonInclude;
9 | import com.fasterxml.jackson.annotation.JsonInclude.Include;
10 |
11 | import lombok.AllArgsConstructor;
12 | import lombok.Builder;
13 | import lombok.Data;
14 | import lombok.EqualsAndHashCode;
15 | import lombok.NoArgsConstructor;
16 |
17 | @Data
18 | @Builder
19 | @NoArgsConstructor
20 | @AllArgsConstructor
21 | @EqualsAndHashCode(callSuper = false)
22 | @Relation(collectionRelation = "actors", itemRelation = "actor")
23 | @JsonInclude(Include.NON_NULL)
24 | public class ActorModel extends RepresentationModel
25 | {
26 | private Long id;
27 | private String firstName;
28 | private String lastName;
29 | private String birthDate;
30 |
31 | private List albums;
32 | }
33 |
--------------------------------------------------------------------------------
/spring-boot-hateoas/src/main/java/com/howtodoinjava/rest/model/AlbumModel.java:
--------------------------------------------------------------------------------
1 | package com.howtodoinjava.rest.model;
2 |
3 | import java.util.List;
4 |
5 | import org.springframework.hateoas.RepresentationModel;
6 | import org.springframework.hateoas.server.core.Relation;
7 |
8 | import com.fasterxml.jackson.annotation.JsonInclude;
9 | import com.fasterxml.jackson.annotation.JsonInclude.Include;
10 |
11 | import lombok.AllArgsConstructor;
12 | import lombok.Builder;
13 | import lombok.Data;
14 | import lombok.EqualsAndHashCode;
15 | import lombok.NoArgsConstructor;
16 |
17 | @Data
18 | @Builder
19 | @NoArgsConstructor
20 | @AllArgsConstructor
21 | @EqualsAndHashCode(callSuper = false)
22 | @Relation(collectionRelation = "albums", itemRelation = "album")
23 | @JsonInclude(Include.NON_NULL)
24 | public class AlbumModel extends RepresentationModel
25 | {
26 | private Long id;
27 | private String title;
28 | private String description;
29 | private String releaseDate;
30 |
31 | private List actors;
32 | }
33 |
--------------------------------------------------------------------------------
/spring-boot-hateoas/src/main/java/com/howtodoinjava/rest/repository/ActorRepository.java:
--------------------------------------------------------------------------------
1 | package com.howtodoinjava.rest.repository;
2 |
3 | import com.howtodoinjava.rest.entity.ActorEntity;
4 | import org.springframework.data.repository.ListCrudRepository;
5 | import org.springframework.data.repository.ListPagingAndSortingRepository;
6 |
7 | public interface ActorRepository extends ListCrudRepository,
8 | ListPagingAndSortingRepository {
9 |
10 | }
11 |
--------------------------------------------------------------------------------
/spring-boot-hateoas/src/main/java/com/howtodoinjava/rest/repository/AlbumRepository.java:
--------------------------------------------------------------------------------
1 | package com.howtodoinjava.rest.repository;
2 |
3 | import com.howtodoinjava.rest.entity.AlbumEntity;
4 | import org.springframework.data.repository.ListCrudRepository;
5 | import org.springframework.data.repository.ListPagingAndSortingRepository;
6 |
7 | public interface AlbumRepository extends ListCrudRepository,
8 | ListPagingAndSortingRepository {
9 |
10 | }
--------------------------------------------------------------------------------
/spring-boot-hateoas/src/main/resources/application.properties:
--------------------------------------------------------------------------------
1 | spring.datasource.url=jdbc:h2:mem:test
2 | spring.datasource.driverClassName=org.h2.Driver
3 | spring.datasource.username=sa
4 | spring.datasource.password=
5 | spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
6 |
7 | spring.jpa.hibernate.ddl-auto=none
8 |
9 | #spring.hateoas.use-hal-as-default-json-media-type=false
--------------------------------------------------------------------------------
/spring-boot-hateoas/src/main/resources/data.sql:
--------------------------------------------------------------------------------
1 | INSERT INTO actor VALUES ('1', 'John', 'Doe', '10-Jan-1952');
2 | INSERT INTO actor VALUES ('2', 'Amy', 'Eugene', '05-07-1985');
3 | INSERT INTO actor VALUES ('3', 'Laverne', 'Mann', '11-12-1988');
4 | INSERT INTO actor VALUES ('4', 'Janice', 'Preston', '19-02-1960');
5 | INSERT INTO actor VALUES ('5', 'Pauline', 'Rios', '29-08-1977');
6 |
7 | INSERT INTO album VALUES ('1', 'Top Hits Vol 1', 'Top hits vol 1. description', '10-03-1981');
8 | INSERT INTO album VALUES ('2', 'Top Hits Vol 2', 'Top hits vol 2. description', '10-03-1982');
9 | INSERT INTO album VALUES ('3', 'Top Hits Vol 3', 'Top hits vol 3. description', '10-03-1983');
10 | INSERT INTO album VALUES ('4', 'Top Hits Vol 4', 'Top hits vol 4. description', '10-03-1984');
11 | INSERT INTO album VALUES ('5', 'Top Hits Vol 5', 'Top hits vol 5. description', '10-03-1985');
12 | INSERT INTO album VALUES ('6', 'Top Hits Vol 6', 'Top hits vol 6. description', '10-03-1986');
13 | INSERT INTO album VALUES ('7', 'Top Hits Vol 7', 'Top hits vol 7. description', '10-03-1987');
14 | INSERT INTO album VALUES ('8', 'Top Hits Vol 8', 'Top hits vol 8. description', '10-03-1988');
15 | INSERT INTO album VALUES ('9', 'Top Hits Vol 9', 'Top hits vol 9. description', '10-03-1989');
16 | INSERT INTO album VALUES ('10', 'Top Hits Vol 10', 'Top hits vol 10. description', '10-03-1990');
17 |
18 | INSERT INTO actor_album VALUES (1, 1);
19 | INSERT INTO actor_album VALUES (1, 2);
20 | INSERT INTO actor_album VALUES (2, 3);
21 | INSERT INTO actor_album VALUES (2, 4);
22 | INSERT INTO actor_album VALUES (3, 5);
23 | INSERT INTO actor_album VALUES (3, 6);
24 | INSERT INTO actor_album VALUES (4, 7);
25 | INSERT INTO actor_album VALUES (4, 8);
26 | INSERT INTO actor_album VALUES (5, 9);
27 | INSERT INTO actor_album VALUES (5, 10);
--------------------------------------------------------------------------------
/spring-boot-hateoas/src/main/resources/schema.sql:
--------------------------------------------------------------------------------
1 | CREATE TABLE actor (
2 | id INT PRIMARY KEY,
3 | first_name VARCHAR(255) NULL,
4 | last_name VARCHAR(255) NULL,
5 | birth_date VARCHAR(255) NULL
6 | );
7 |
8 | CREATE TABLE album (
9 | id INT PRIMARY KEY,
10 | title VARCHAR(255) NULL,
11 | description VARCHAR(255) NULL,
12 | release_date VARCHAR(255) NULL
13 | );
14 |
15 | CREATE TABLE actor_album (
16 | actor_id INT,
17 | album_id INT
18 | );
--------------------------------------------------------------------------------
/spring-boot-hateoas/src/test/java/com/howtodoinjava/AppTest.java:
--------------------------------------------------------------------------------
1 | package com.howtodoinjava;
2 |
3 | import static org.junit.jupiter.api.Assertions.assertTrue;
4 | import org.junit.jupiter.api.Test;
5 | import org.springframework.boot.test.context.SpringBootTest;
6 |
7 | @SpringBootTest
8 | public class AppTest {
9 |
10 | @Test
11 | public void contextLoads() {
12 | assertTrue(true);
13 | }
14 | }
--------------------------------------------------------------------------------
/spring-restclient/README.md:
--------------------------------------------------------------------------------
1 | # Related Tutorials
2 |
3 | * [Spring RestClient](https://howtodoinjava.com/spring/spring-restclient/)
4 | * [Fake REST APIs for Unit Testing](https://howtodoinjava.com/angular/mock-rest-server/)
5 |
--------------------------------------------------------------------------------
/spring-restclient/db.json:
--------------------------------------------------------------------------------
1 | {
2 | "employees": [
3 | {
4 | "id": 1,
5 | "name": "Lokesh",
6 | "status": "Active"
7 | },
8 | {
9 | "id": 2,
10 | "name": "Andy",
11 | "status": "Inactive"
12 | },
13 | {
14 | "id": 3,
15 | "name": "Brian",
16 | "status": "Active"
17 | },
18 | {
19 | "id": 4,
20 | "name": "Charles",
21 | "status": "Inactive"
22 | }
23 | ]
24 | }
--------------------------------------------------------------------------------
/spring-restclient/pom.xml:
--------------------------------------------------------------------------------
1 |
4 | 4.0.0
5 |
6 |
7 | org.springframework.boot
8 | spring-boot-starter-parent
9 | 3.2.0-M3
10 |
11 |
12 |
13 | com.howtodoinjava.app
14 | spring-restclient
15 | jar
16 | spring-restclient
17 | https://howtodoinjava.com
18 |
19 |
20 | UTF-8
21 | 21
22 |
23 |
24 |
25 |
26 | org.projectlombok
27 | lombok
28 | true
29 | 1.18.30
30 |
31 |
32 | org.springframework.boot
33 | spring-boot-starter-web
34 |
35 |
36 | org.springframework.boot
37 | spring-boot-starter-webflux
38 |
39 |
40 | org.springframework.boot
41 | spring-boot-starter-test
42 |
43 |
44 | org.apache.httpcomponents.client5
45 | httpclient5
46 | 5.2.1
47 |
48 |
49 | org.apache.httpcomponents.core5
50 | httpcore5-reactive
51 | 5.2.1
52 |
53 |
54 |
55 |
56 |
57 | spring-milestone
58 | Spring Milestones
59 | https://repo.spring.io/milestone
60 |
61 |
62 |
63 |
--------------------------------------------------------------------------------
/spring-restclient/src/main/java/com/howtodoinjava/app/config/WebConfig.java:
--------------------------------------------------------------------------------
1 | package com.howtodoinjava.app.config;
2 |
3 | import org.apache.hc.client5.http.impl.async.CloseableHttpAsyncClient;
4 | import org.apache.hc.client5.http.impl.async.HttpAsyncClientBuilder;
5 | import org.apache.hc.client5.http.impl.async.HttpAsyncClients;
6 | import org.springframework.context.annotation.Bean;
7 | import org.springframework.context.annotation.Configuration;
8 | import org.springframework.http.client.reactive.ClientHttpConnector;
9 | import org.springframework.http.client.reactive.HttpComponentsClientHttpConnector;
10 | import org.springframework.web.reactive.function.client.WebClient;
11 |
12 | @Configuration
13 | public class WebConfig {
14 |
15 | @Bean
16 | public WebClient webClient() {
17 | HttpAsyncClientBuilder clientBuilder = HttpAsyncClients.custom();
18 | //clientBuilder.setDefaultRequestConfig();
19 | CloseableHttpAsyncClient client = clientBuilder.build();
20 | ClientHttpConnector connector = new HttpComponentsClientHttpConnector(client);
21 | return WebClient.builder().clientConnector(connector).build();
22 | }
23 | }
24 |
--------------------------------------------------------------------------------
/spring-restclient/src/main/java/com/howtodoinjava/app/exception/ApplicationException.java:
--------------------------------------------------------------------------------
1 | package com.howtodoinjava.app.exception;
2 |
3 | public class ApplicationException extends RuntimeException {
4 |
5 | public ApplicationException(String message) {
6 | super(message);
7 | }
8 | }
9 |
--------------------------------------------------------------------------------
/spring-restclient/src/main/java/com/howtodoinjava/app/model/Employee.java:
--------------------------------------------------------------------------------
1 | package com.howtodoinjava.app.model;
2 |
3 | import lombok.AllArgsConstructor;
4 | import lombok.Data;
5 | import lombok.NoArgsConstructor;
6 |
7 | @Data
8 | @AllArgsConstructor
9 | @NoArgsConstructor
10 | public class Employee {
11 |
12 | private long id;
13 | private String name;
14 | private String status;
15 | }
16 |
--------------------------------------------------------------------------------
/spring-restclient/src/main/java/com/howtodoinjava/app/web/EmployeeController.java:
--------------------------------------------------------------------------------
1 | package com.howtodoinjava.app.web;
2 |
3 | import com.howtodoinjava.app.model.Employee;
4 | import org.springframework.http.HttpStatusCode;
5 | import org.springframework.web.bind.annotation.RestController;
6 | import org.springframework.web.reactive.function.BodyInserters;
7 | import org.springframework.web.reactive.function.client.WebClient;
8 | import org.springframework.web.reactive.function.client.WebClientResponseException;
9 |
10 | import java.net.URI;
11 |
12 | @RestController
13 | public class EmployeeController {
14 |
15 | private final WebClient webClient;
16 |
17 | public EmployeeController(WebClient webClient) {
18 | this.webClient = webClient;
19 | }
20 |
21 | public void createEmployee(){
22 |
23 | Employee newEmployee = new Employee(1L, "Lokesh Gupta", "Active");
24 |
25 | webClient.post()
26 | .uri("/employees")
27 | .bodyValue(BodyInserters.fromValue(newEmployee))
28 | .retrieve()
29 | .toBodilessEntity()
30 | .subscribe(
31 | responseEntity -> {
32 | // Handle success response here
33 | HttpStatusCode status = responseEntity.getStatusCode();
34 | URI location = responseEntity.getHeaders().getLocation();
35 | // handle response as necessary
36 | },
37 | error -> {
38 | // Handle the error here
39 | if (error instanceof WebClientResponseException ex) {
40 | HttpStatusCode status = ex.getStatusCode();
41 | System.out.println("Error Status Code: " + status.value());
42 | //...
43 | } else {
44 | // Handle other types of errors
45 | System.err.println("An unexpected error occurred: " + error.getMessage());
46 | }
47 | }
48 | );
49 | }
50 | }
51 |
--------------------------------------------------------------------------------
/structured-logging/.gitattributes:
--------------------------------------------------------------------------------
1 | /mvnw text eol=lf
2 | *.cmd text eol=crlf
3 |
--------------------------------------------------------------------------------
/structured-logging/.gitignore:
--------------------------------------------------------------------------------
1 | HELP.md
2 | target/
3 | !.mvn/wrapper/maven-wrapper.jar
4 | !**/src/main/**/target/
5 | !**/src/test/**/target/
6 |
7 | ### STS ###
8 | .apt_generated
9 | .classpath
10 | .factorypath
11 | .project
12 | .settings
13 | .springBeans
14 | .sts4-cache
15 |
16 | ### IntelliJ IDEA ###
17 | .idea
18 | *.iws
19 | *.iml
20 | *.ipr
21 |
22 | ### NetBeans ###
23 | /nbproject/private/
24 | /nbbuild/
25 | /dist/
26 | /nbdist/
27 | /.nb-gradle/
28 | build/
29 | !**/src/main/**/build/
30 | !**/src/test/**/build/
31 |
32 | ### VS Code ###
33 | .vscode/
34 |
--------------------------------------------------------------------------------
/structured-logging/.mvn/wrapper/maven-wrapper.properties:
--------------------------------------------------------------------------------
1 | # Licensed to the Apache Software Foundation (ASF) under one
2 | # or more contributor license agreements. See the NOTICE file
3 | # distributed with this work for additional information
4 | # regarding copyright ownership. The ASF licenses this file
5 | # to you under the Apache License, Version 2.0 (the
6 | # "License"); you may not use this file except in compliance
7 | # with the License. You may obtain a copy of the License at
8 | #
9 | # http://www.apache.org/licenses/LICENSE-2.0
10 | #
11 | # Unless required by applicable law or agreed to in writing,
12 | # software distributed under the License is distributed on an
13 | # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14 | # KIND, either express or implied. See the License for the
15 | # specific language governing permissions and limitations
16 | # under the License.
17 | wrapperVersion=3.3.2
18 | distributionType=only-script
19 | distributionUrl=https://repo.maven.apache.org/maven2/org/apache/maven/apache-maven/3.9.9/apache-maven-3.9.9-bin.zip
20 |
--------------------------------------------------------------------------------
/structured-logging/src/main/java/com/howtodoinjava/demo/CustomLogFormatter.java:
--------------------------------------------------------------------------------
1 | package com.howtodoinjava.demo;
2 |
3 | import ch.qos.logback.classic.spi.ILoggingEvent;
4 | import org.springframework.boot.logging.structured.StructuredLogFormatter;
5 |
6 | public class CustomLogFormatter implements StructuredLogFormatter {
7 |
8 | @Override
9 | public String format(ILoggingEvent event) {
10 |
11 | // Use a StringBuilder for efficient string concatenation
12 | StringBuilder logBuilder = new StringBuilder();
13 |
14 | // Add basic log details
15 | logBuilder.append("time=").append(event.getTimeStamp())
16 | .append(",logger=").append(event.getLoggerName())
17 | .append(",level=").append(event.getLevel())
18 | .append(",message=\"").append(event.getFormattedMessage()).append("\"");
19 |
20 | // Iterate over key-value pairs and append as CSV
21 | if (event.getKeyValuePairs() != null) {
22 | event.getKeyValuePairs()
23 | .stream()
24 | .forEach((pair) -> {
25 | logBuilder.append(",").append(pair.key).append("=").append(pair.value);
26 | });
27 | }
28 |
29 | // Iterate over MDC map and append as CSV
30 | if (event.getMDCPropertyMap() != null) {
31 | event.getMDCPropertyMap()
32 | .entrySet()
33 | .stream()
34 | .forEach((entry) -> {
35 | logBuilder.append(",").append(entry.getKey()).append("=").append(entry.getValue());
36 | });
37 | }
38 |
39 | // Add a newline at the end of the log
40 | logBuilder.append("\n");
41 |
42 | return logBuilder.toString();
43 | }
44 | }
--------------------------------------------------------------------------------
/structured-logging/src/main/java/com/howtodoinjava/demo/StructuredLoggingApplication.java:
--------------------------------------------------------------------------------
1 | package com.howtodoinjava.demo;
2 |
3 | import org.slf4j.Logger;
4 | import org.slf4j.LoggerFactory;
5 | import org.slf4j.MDC;
6 | import org.springframework.boot.CommandLineRunner;
7 | import org.springframework.boot.SpringApplication;
8 | import org.springframework.boot.autoconfigure.SpringBootApplication;
9 |
10 | @SpringBootApplication
11 | public class StructuredLoggingApplication implements CommandLineRunner {
12 |
13 | final static Logger log = LoggerFactory.getLogger(StructuredLoggingApplication.class);
14 |
15 | public static void main(String[] args) {
16 | SpringApplication.run(StructuredLoggingApplication.class, args);
17 | }
18 |
19 | @Override
20 | public void run(String... args) throws Exception {
21 | log.trace("Trace log");
22 | log.debug("Debug log");
23 |
24 | log.info("Info log");
25 | log.warn("Hey, This is a warning!");
26 | log.error("Oops! We have an Error. OK");
27 |
28 | /*MDC.put("MyKey1", "MyValue1");
29 | MDC.put("MyKey2", "MyValue2");*/
30 |
31 | /*log.atInfo()
32 | .setMessage("Info log")
33 | .addKeyValue("MyKey1", "MyValue1")
34 | .addKeyValue("MyKey2", "MyValue2")
35 | .log();*/
36 | }
37 | }
38 |
--------------------------------------------------------------------------------
/structured-logging/src/main/resources/application.properties:
--------------------------------------------------------------------------------
1 | spring.application.name=structured-logging
2 |
3 | # ecs, gelf, logstash or Custom Log Event
4 | logging.structured.format.console=com.howtodoinjava.demo.CustomLogFormatter
5 | logging.structured.format.file=ecs
6 | logging.file.name=${java.io.tmpdir}/app.log
7 |
8 | # ecs properties
9 |
10 | logging.structured.ecs.service.name=MyService
11 | logging.structured.ecs.service.version=1
12 | logging.structured.ecs.service.environment=Production
13 | logging.structured.ecs.service.node-name=Primary
14 |
15 | # gelf properties
16 |
17 | logging.structured.gelf.host=MyService
18 | logging.structured.gelf.service.version=1
19 |
20 | # logstash properties
21 |
22 | logging.structured.json.add.host=MyService
23 | logging.structured.json.add.version=1
24 | logging.structured.json.exclude=level_value
25 | logging.structured.json.rename.logger_name=logger_class
26 |
--------------------------------------------------------------------------------
/structured-logging/src/test/java/com/howtodoinjava/demo/StructuredLoggingApplicationTests.java:
--------------------------------------------------------------------------------
1 | package com.howtodoinjava.demo;
2 |
3 | import org.junit.jupiter.api.Test;
4 | import org.springframework.boot.test.context.SpringBootTest;
5 |
6 | @SpringBootTest
7 | class StructuredLoggingApplicationTests {
8 |
9 | @Test
10 | void contextLoads() {
11 | }
12 |
13 | }
14 |
--------------------------------------------------------------------------------
/task-scheduler/README.md:
--------------------------------------------------------------------------------
1 | # Related Tutorials
2 |
3 | - [Spring Boot @Scheduled Task Execution Example](https://howtodoinjava.com/spring-boot/enable-scheduling-scheduled-job-example/)
4 | - [Spring Task Scheduling](https://howtodoinjava.com/spring-core/spring-scheduled-annotation/)
--------------------------------------------------------------------------------
/task-scheduler/pom.xml:
--------------------------------------------------------------------------------
1 |
4 | 4.0.0
5 |
6 | com.howtodoinjava
7 | Spring-Boot3-Demos
8 | 1.0-SNAPSHOT
9 |
10 |
11 | task-scheduler
12 | jar
13 | task-scheduler
14 | https://howtodoinjava.com
15 |
16 |
17 | UTF-8
18 |
19 |
20 |
21 |
22 | org.springframework.boot
23 | spring-boot-starter
24 |
25 |
26 | org.springframework.boot
27 | spring-boot-starter-web
28 |
29 |
30 |
31 |
--------------------------------------------------------------------------------
/task-scheduler/src/main/java/com/howtodoinjava/App.java:
--------------------------------------------------------------------------------
1 | package com.howtodoinjava;
2 |
3 | import org.springframework.boot.SpringApplication;
4 | import org.springframework.boot.autoconfigure.SpringBootApplication;
5 |
6 | @SpringBootApplication
7 | public class App {
8 |
9 | public static void main(String[] args) {
10 |
11 | SpringApplication.run(App.class, args);
12 | }
13 | }
14 |
--------------------------------------------------------------------------------
/task-scheduler/src/main/java/com/howtodoinjava/config/SchedulerConfig.java:
--------------------------------------------------------------------------------
1 | package com.howtodoinjava.config;
2 |
3 | import org.springframework.context.annotation.Bean;
4 | import org.springframework.context.annotation.Configuration;
5 | import org.springframework.scheduling.annotation.EnableAsync;
6 | import org.springframework.scheduling.annotation.EnableScheduling;
7 | import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
8 |
9 | @Configuration
10 | @EnableScheduling
11 | @EnableAsync
12 | public class SchedulerConfig {
13 |
14 | @Bean
15 | public ThreadPoolTaskScheduler threadPoolTaskScheduler() {
16 |
17 | ThreadPoolTaskScheduler threadPoolTaskScheduler = new ThreadPoolTaskScheduler();
18 | threadPoolTaskScheduler.setPoolSize(5);
19 | threadPoolTaskScheduler.setThreadNamePrefix("ThreadPoolTaskScheduler");
20 | return threadPoolTaskScheduler;
21 | }
22 | }
23 |
--------------------------------------------------------------------------------
/task-scheduler/src/main/java/com/howtodoinjava/tasks/Task.java:
--------------------------------------------------------------------------------
1 | package com.howtodoinjava.tasks;
2 |
3 | import java.time.LocalDateTime;
4 | import lombok.extern.java.Log;
5 | import org.springframework.scheduling.annotation.Async;
6 | import org.springframework.scheduling.annotation.Scheduled;
7 | import org.springframework.stereotype.Component;
8 |
9 | @Component
10 | @Log
11 | public class Task {
12 |
13 | @Scheduled(initialDelay = 1000, fixedRate = 10000)
14 | public void run() {
15 | log.info("Current time is :: " + LocalDateTime.now());
16 | }
17 |
18 | @Scheduled(fixedRate = 10000)
19 | @Async
20 | public void taskWithConcurrentExecutions() {
21 | log.info("Current time is :: " + LocalDateTime.now());
22 | }
23 | }
24 |
--------------------------------------------------------------------------------
/task-scheduler/src/main/resources/application.properties:
--------------------------------------------------------------------------------
1 | spring.task.scheduling.pool.size=5
--------------------------------------------------------------------------------
/task-scheduler/src/test/java/com/howtodoinjava/AppTest.java:
--------------------------------------------------------------------------------
1 | package com.howtodoinjava;
2 |
3 | import org.junit.jupiter.api.Test;
4 | import org.springframework.boot.test.context.SpringBootTest;
5 |
6 | @SpringBootTest
7 | public class AppTest
8 | {
9 | @Test
10 | void contextLoads(){
11 |
12 | }
13 | }
14 |
--------------------------------------------------------------------------------
/task-scheduler/target/classes/application.properties:
--------------------------------------------------------------------------------
1 | spring.task.scheduling.pool.size=5
--------------------------------------------------------------------------------
/virtual-threads-demo/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
5 | 4.0.0
6 |
7 | virtual-threads-demo
8 | 1.0-SNAPSHOT
9 | virtual-threads-demo
10 | https://howtodoinjava.com
11 |
12 |
13 | com.howtodoinjava
14 | Spring-Boot3-Demos
15 | 1.0-SNAPSHOT
16 |
17 |
18 |
19 |
20 | org.springframework.boot
21 | spring-boot-starter-web
22 |
23 |
24 | org.springframework.boot
25 | spring-boot-devtools
26 | runtime
27 | true
28 |
29 |
30 |
31 |
32 |
33 |
42 |
43 | org.springframework.boot
44 | spring-boot-maven-plugin
45 |
46 |
47 |
48 | org.projectlombok
49 | lombok
50 |
51 |
52 |
53 |
54 |
55 |
56 |
57 |
--------------------------------------------------------------------------------
/virtual-threads-demo/src/main/java/com/howtodoinjava/App.java:
--------------------------------------------------------------------------------
1 | package com.howtodoinjava;
2 |
3 | import java.time.Duration;
4 | import java.time.Instant;
5 | import java.time.LocalDateTime;
6 | import java.util.stream.IntStream;
7 | import lombok.extern.slf4j.Slf4j;
8 | import org.springframework.beans.factory.annotation.Autowired;
9 | import org.springframework.boot.CommandLineRunner;
10 | import org.springframework.boot.SpringApplication;
11 | import org.springframework.boot.autoconfigure.SpringBootApplication;
12 | import org.springframework.context.ApplicationEventPublisher;
13 |
14 | @Slf4j
15 | @SpringBootApplication
16 | public class App implements CommandLineRunner {
17 |
18 | public static void main(String[] args) {
19 | SpringApplication.run(App.class, args);
20 | }
21 |
22 | @Autowired
23 | ApplicationEventPublisher publisher;
24 |
25 | @Override
26 | public void run(String... args) throws Exception {
27 | Instant start = Instant.now();
28 | IntStream.rangeClosed(1, 10_000)
29 | .forEachOrdered(i ->
30 | publisher.publishEvent("Hello #" + i + " at:" + LocalDateTime.now())
31 | );
32 |
33 | Instant finish = Instant.now();
34 | long timeElapsed = Duration.between(start, finish).toMillis();
35 |
36 | log.info("=====Elapsed time : {} ========", timeElapsed);
37 | }
38 | }
39 |
--------------------------------------------------------------------------------
/virtual-threads-demo/src/main/java/com/howtodoinjava/AppEventListener.java:
--------------------------------------------------------------------------------
1 | package com.howtodoinjava;
2 |
3 | import lombok.extern.slf4j.Slf4j;
4 | import org.springframework.context.event.EventListener;
5 | import org.springframework.scheduling.annotation.Async;
6 | import org.springframework.stereotype.Component;
7 |
8 | @Component
9 | @Slf4j
10 | public class AppEventListener {
11 | @EventListener
12 | @Async
13 | public void onGreetingEvent(String message) {
14 | log.info(Thread.currentThread() + " :: Received: {}", message);
15 | }
16 | }
17 |
--------------------------------------------------------------------------------
/virtual-threads-demo/src/main/java/com/howtodoinjava/config/AsyncConfig.java:
--------------------------------------------------------------------------------
1 | package com.howtodoinjava.config;
2 |
3 | import java.util.concurrent.Executors;
4 | import org.springframework.boot.autoconfigure.task.TaskExecutionAutoConfiguration;
5 | import org.springframework.boot.web.embedded.tomcat.TomcatProtocolHandlerCustomizer;
6 | import org.springframework.context.annotation.Bean;
7 | import org.springframework.context.annotation.Configuration;
8 | import org.springframework.core.task.AsyncTaskExecutor;
9 | import org.springframework.core.task.support.TaskExecutorAdapter;
10 | import org.springframework.scheduling.annotation.EnableAsync;
11 |
12 | @Configuration
13 | @EnableAsync
14 | public class AsyncConfig {
15 |
16 | @Bean(TaskExecutionAutoConfiguration.APPLICATION_TASK_EXECUTOR_BEAN_NAME)
17 | public AsyncTaskExecutor asyncTaskExecutor() {
18 | //TODO: Enable in Java 20
19 | //return new TaskExecutorAdapter(Executors.newVirtualThreadPerTaskExecutor());
20 | return new TaskExecutorAdapter(Executors.newSingleThreadExecutor());
21 | }
22 |
23 | @Bean
24 | public TomcatProtocolHandlerCustomizer> protocolHandlerVirtualThreadExecutorCustomizer() {
25 | return protocolHandler -> {
26 | //TODO: Enable in Java 20
27 | //protocolHandler.setExecutor(Executors.newVirtualThreadPerTaskExecutor());
28 | };
29 | }
30 | }
31 |
--------------------------------------------------------------------------------
/virtual-threads-demo/src/main/resources/application.properties:
--------------------------------------------------------------------------------
1 | logging.level.web=TRACE
2 | logging.level.com.howtodoinjava=DEBUG
3 |
4 | spring.main.allow-bean-definition-overriding=true
--------------------------------------------------------------------------------
/virtual-threads-demo/src/test/java/com/howtodoinjava/AppTest.java:
--------------------------------------------------------------------------------
1 | package com.howtodoinjava;
2 |
3 | import org.junit.jupiter.api.Nested;
4 | import org.junit.jupiter.api.Test;
5 | import org.springframework.boot.test.context.SpringBootTest;
6 |
7 | public class AppTest
8 | {
9 | @Nested
10 | @SpringBootTest
11 | class DemoApplicationTests {
12 |
13 | @Test
14 | void contextLoads() {
15 | }
16 |
17 | }
18 | }
19 |
--------------------------------------------------------------------------------
/webflux-part-event-api-example/.gitignore:
--------------------------------------------------------------------------------
1 | target/
2 | !.mvn/wrapper/maven-wrapper.jar
3 | !**/src/main/**/target/
4 | !**/src/test/**/target/
5 |
6 | ### IntelliJ IDEA ###
7 | .idea/modules.xml
8 | .idea/jarRepositories.xml
9 | .idea/compiler.xml
10 | .idea/libraries/
11 | *.iws
12 | *.iml
13 | *.ipr
14 |
15 | ### Eclipse ###
16 | .apt_generated
17 | .classpath
18 | .factorypath
19 | .project
20 | .settings
21 | .springBeans
22 | .sts4-cache
23 |
24 | ### NetBeans ###
25 | /nbproject/private/
26 | /nbbuild/
27 | /dist/
28 | /nbdist/
29 | /.nb-gradle/
30 | build/
31 | !**/src/main/**/build/
32 | !**/src/test/**/build/
33 |
34 | ### VS Code ###
35 | .vscode/
36 |
37 | ### Mac OS ###
38 | .DS_Store
--------------------------------------------------------------------------------
/webflux-part-event-api-example/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
6 | 4.0.0
7 |
8 | com.howtodoinjava
9 | webflux-part-event-api-example
10 | 1.0-SNAPSHOT
11 |
12 | webflux-part-event-api-example
13 | https://howtodoinjava.com
14 |
15 |
16 | com.howtodoinjava
17 | Spring-Boot3-Demos
18 | 1.0-SNAPSHOT
19 |
20 |
21 |
22 |
23 | org.springframework.boot
24 | spring-boot-starter-web
25 |
26 |
27 | org.springframework.boot
28 | spring-boot-starter-webflux
29 |
30 |
31 | org.springframework.boot
32 | spring-boot-starter-reactor-netty
33 |
34 |
35 | org.springframework.boot
36 | spring-boot-devtools
37 | runtime
38 | true
39 |
40 |
41 | io.projectreactor
42 | reactor-test
43 | test
44 |
45 |
46 | org.mockito
47 | mockito-junit-jupiter
48 | test
49 |
50 |
51 |
52 |
--------------------------------------------------------------------------------
/webflux-part-event-api-example/src/main/java/com/howtodoinjava/App.java:
--------------------------------------------------------------------------------
1 | package com.howtodoinjava;
2 |
3 | import org.springframework.boot.SpringApplication;
4 | import org.springframework.boot.autoconfigure.SpringBootApplication;
5 |
6 | @SpringBootApplication
7 | public class App
8 | {
9 | public static void main(String[] args) {
10 | SpringApplication.run(App.class, args);
11 | }
12 | }
--------------------------------------------------------------------------------
/webflux-part-event-api-example/src/main/java/com/howtodoinjava/app/config/WebFuxConfig.java:
--------------------------------------------------------------------------------
1 | package com.howtodoinjava.app.config;
2 |
3 | import com.fasterxml.jackson.databind.ObjectMapper;
4 | import org.springframework.beans.factory.annotation.Autowired;
5 | import org.springframework.context.annotation.Configuration;
6 | import org.springframework.http.MediaType;
7 | import org.springframework.http.codec.ServerCodecConfigurer;
8 | import org.springframework.http.codec.json.Jackson2JsonDecoder;
9 | import org.springframework.http.codec.json.Jackson2JsonEncoder;
10 | import org.springframework.web.reactive.config.WebFluxConfigurer;
11 |
12 | @Configuration
13 | public class WebFuxConfig implements WebFluxConfigurer {
14 |
15 | }
--------------------------------------------------------------------------------
/webflux-part-event-api-example/src/main/java/com/howtodoinjava/app/model/FileUploadCommand.java:
--------------------------------------------------------------------------------
1 | package com.howtodoinjava.app.model;
2 |
3 | import lombok.AllArgsConstructor;
4 | import lombok.Data;
5 | import lombok.NoArgsConstructor;
6 | import org.springframework.http.codec.multipart.FilePart;
7 |
8 | @Data
9 | @AllArgsConstructor
10 | @NoArgsConstructor
11 | public class FileUploadCommand {
12 | private String name;
13 | private FilePart file;
14 | }
15 |
--------------------------------------------------------------------------------
/webflux-part-event-api-example/src/main/resources/application.properties:
--------------------------------------------------------------------------------
1 | spring.servlet.multipart.max-file-size=50MB
2 | spring.servlet.multipart.max-request-size=50MB
--------------------------------------------------------------------------------
/webflux-part-event-api-example/src/main/resources/spring.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/lokeshgupta1981/Spring-Boot3-Demos/6974e42d87a604213d2cdb2c0b2a1e26418e172c/webflux-part-event-api-example/src/main/resources/spring.png
--------------------------------------------------------------------------------
/webflux-part-event-api-example/src/test/java/com/howtodoinjava/AppTest.java:
--------------------------------------------------------------------------------
1 | package com.howtodoinjava;
2 |
3 | import org.junit.jupiter.api.Test;
4 | import org.springframework.boot.test.context.SpringBootTest;
5 |
6 | @SpringBootTest
7 | public class AppTest
8 | {
9 | @Test
10 | void contextLoads() {
11 | }
12 | }
13 |
--------------------------------------------------------------------------------