diff --git a/pom.xml b/pom.xml index 932b7d7..5cd8cab 100644 --- a/pom.xml +++ b/pom.xml @@ -24,19 +24,13 @@ org.springframework.boot spring-boot-starter-web - + - - - + + + org.projectlombok @@ -54,6 +48,15 @@ + + org.springframework.boot + spring-boot-starter-web + + + + org.springframework.boot + spring-boot-starter-thymeleaf + @@ -65,4 +68,18 @@ + diff --git a/src/main/java/com/javaprogram/api/RequestHeadersAPI.java b/src/main/java/com/javaprogram/api/RequestHeadersAPI.java index 4e5977a..434c3a7 100644 --- a/src/main/java/com/javaprogram/api/RequestHeadersAPI.java +++ b/src/main/java/com/javaprogram/api/RequestHeadersAPI.java @@ -17,7 +17,7 @@ import org.springframework.web.bind.annotation.RestController; @RestController -@RequestMapping("request/headers") +@RequestMapping("headers") public class RequestHeadersAPI { Logger logger = LoggerFactory.getLogger(RequestHeadersAPI.class); diff --git a/src/main/java/com/javaprogram/auto/configurations/AutoConfigureApplication.java b/src/main/java/com/javaprogram/auto/configurations/AutoConfigureApplication.java new file mode 100644 index 0000000..4f537f3 --- /dev/null +++ b/src/main/java/com/javaprogram/auto/configurations/AutoConfigureApplication.java @@ -0,0 +1,57 @@ +package com.javaprogram.auto.configurations; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.EnableAutoConfiguration; +import org.springframework.boot.autoconfigure.jms.JmsAutoConfiguration; +import org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration; +import org.springframework.context.annotation.Bean; +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.PostMapping; + +@EnableAutoConfiguration(exclude = SecurityAutoConfiguration.class, excludeName = "SecurityAutoConfiguration") +public class AutoConfigureApplication { + + @Bean + public Message getMessage() { + return new Message(); + } + + @Bean + public MessageController getMessageController() { + return new MessageController(); + } + + public static void main(String[] args) { + SpringApplication.run(AutoConfigureApplication.class, args); + + } + +} + +class Message { + + public String getMessage() { + return "New message generated"; + } + +} + +@Controller +class MessageController { + Logger logger = LoggerFactory.getLogger(getClass()); + + @Autowired + private Message msg; + + @PostMapping("/send") + public void messageSender() { + + String messag = msg.getMessage(); + logger.info("Message : " + messag); + logger.info("Message sent...."); + + } +} \ No newline at end of file diff --git a/src/main/java/com/javaprogram/config/EmbedServerCustomConfigration.java b/src/main/java/com/javaprogram/config/EmbedServerCustomConfigration.java new file mode 100644 index 0000000..998b447 --- /dev/null +++ b/src/main/java/com/javaprogram/config/EmbedServerCustomConfigration.java @@ -0,0 +1,21 @@ +package com.javaprogram.config; + +import org.springframework.boot.web.server.WebServerFactoryCustomizer; +import org.springframework.boot.web.servlet.server.ConfigurableServletWebServerFactory; +import org.springframework.stereotype.Component; + +//@Component +public class EmbedServerCustomConfigration implements WebServerFactoryCustomizer { + + @Override + public void customize(ConfigurableServletWebServerFactory factory) { + + factory.setPort(9009); + factory.setDisplayName("JavaProgramTo.com"); + factory.setServerHeader("Server header"); + + factory.setContextPath("/api/v3"); + + } + +} diff --git a/src/main/java/com/javaprogram/config/SetBasePath.java b/src/main/java/com/javaprogram/config/SetBasePath.java new file mode 100644 index 0000000..9af080b --- /dev/null +++ b/src/main/java/com/javaprogram/config/SetBasePath.java @@ -0,0 +1,23 @@ +package com.javaprogram.config; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.boot.web.server.WebServerFactoryCustomizer; +import org.springframework.boot.web.servlet.server.ConfigurableServletWebServerFactory; +import org.springframework.stereotype.Component; + +// now this is disabled. To enable, just uncomment @Bean annotation. +@Component +public class SetBasePath { + + private Logger logger = LoggerFactory.getLogger(getClass()); + + // @Bean + public WebServerFactoryCustomizer webServerFactoryCustomizer() { + WebServerFactoryCustomizer customizer = factory -> factory + .setContextPath("/api/v2"); + + logger.info("Setting up the custom base path "); + return customizer; + } +} diff --git a/src/main/java/com/javaprogram/config/TomcatEmbedServerCustomConfigration.java b/src/main/java/com/javaprogram/config/TomcatEmbedServerCustomConfigration.java new file mode 100644 index 0000000..94e6e4a --- /dev/null +++ b/src/main/java/com/javaprogram/config/TomcatEmbedServerCustomConfigration.java @@ -0,0 +1,25 @@ +package com.javaprogram.config; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory; +import org.springframework.boot.web.server.WebServerFactoryCustomizer; +import org.springframework.stereotype.Component; + +//@Component +public class TomcatEmbedServerCustomConfigration implements WebServerFactoryCustomizer { + + private Logger logger = LoggerFactory.getLogger(getClass()); + + @Override + public void customize(TomcatServletWebServerFactory factory) { + logger.info("Setting the Tomcat specific configurations. started"); + factory.setPort(9009); + factory.setDisplayName("JavaProgramTo.com"); + factory.setServerHeader("Server header of tomcat"); + + factory.setContextPath("/api/v4"); + logger.info("Setting the Tomcat specific configurations. ended"); + } + +} diff --git a/src/main/java/com/javaprogram/custom/errors/CustomErrorApplication.java b/src/main/java/com/javaprogram/custom/errors/CustomErrorApplication.java new file mode 100644 index 0000000..65d2ed7 --- /dev/null +++ b/src/main/java/com/javaprogram/custom/errors/CustomErrorApplication.java @@ -0,0 +1,23 @@ +package com.javaprogram.custom.errors; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.EnableAutoConfiguration; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.boot.autoconfigure.web.servlet.error.ErrorMvcAutoConfiguration; + +import com.javaprogram.springbootapp.SpringBootAppApplication; + +@SpringBootApplication(scanBasePackages = "com.javaprogram.custom.errors") +@EnableAutoConfiguration(exclude = {ErrorMvcAutoConfiguration.class}) +public class CustomErrorApplication { + + private static Logger logger = LoggerFactory.getLogger(CustomErrorApplication.class); + + public static void main(String[] args) { + logger.info("Container started....."); + SpringApplication.run(SpringBootAppApplication.class, args); + } + +} diff --git a/src/main/java/com/javaprogram/custom/errors/api/CustomErrorController.java b/src/main/java/com/javaprogram/custom/errors/api/CustomErrorController.java new file mode 100644 index 0000000..0163465 --- /dev/null +++ b/src/main/java/com/javaprogram/custom/errors/api/CustomErrorController.java @@ -0,0 +1,58 @@ +package com.javaprogram.custom.errors.api; + +import javax.servlet.RequestDispatcher; +import javax.servlet.http.HttpServletRequest; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.boot.web.servlet.error.ErrorController; +import org.springframework.http.HttpStatus; +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestHeader; + +import com.javaprogram.api.RequestHeadersAPI; + +@Controller +public class CustomErrorController implements ErrorController { + + Logger logger = LoggerFactory.getLogger(RequestHeadersAPI.class); + + @GetMapping("/custom/error") + public String getCustomError(@RequestHeader(name = "code") String errorCode) { + + logger.info("error code : " + errorCode); + if ("400".equals(errorCode)) { + return "400"; + } else if ("404".equals(errorCode)) { + return "404"; + } + + return "error"; + } + + @GetMapping(value = "/custom/errors") + public String handleError(HttpServletRequest request) { + + Object status = request.getAttribute(RequestDispatcher.ERROR_STATUS_CODE); + + if (status != null) { + + Integer statusCode = Integer.valueOf(status.toString()); + + if (statusCode == HttpStatus.NOT_FOUND.value()) { + return "404"; + } else if (statusCode == HttpStatus.INTERNAL_SERVER_ERROR.value()) { + return "500"; + } + } + return "error"; + } + + @Override + public String getErrorPath() { + logger.info("setting up the path for error pages."); + return "/error"; + } + +} diff --git a/src/main/java/com/javaprogram/custom/errors/api/DefaultErrorsAPI.java b/src/main/java/com/javaprogram/custom/errors/api/DefaultErrorsAPI.java new file mode 100644 index 0000000..ef723b9 --- /dev/null +++ b/src/main/java/com/javaprogram/custom/errors/api/DefaultErrorsAPI.java @@ -0,0 +1,22 @@ +package com.javaprogram.custom.errors.api; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestHeader; + +import com.javaprogram.api.RequestHeadersAPI; + +@Controller +public class DefaultErrorsAPI { + + Logger logger = LoggerFactory.getLogger(RequestHeadersAPI.class); + + @GetMapping("/error") + public String getDefaultPage(@RequestHeader(name = "errorcode") String errorCode) { + logger.info("error code : " + errorCode); + return "error"; + } + +} diff --git a/src/main/java/com/javaprogram/springbootapp/SpringBootAppApplication.java b/src/main/java/com/javaprogram/springbootapp/SpringBootAppApplication.java index ed9c8be..cbcf7a5 100644 --- a/src/main/java/com/javaprogram/springbootapp/SpringBootAppApplication.java +++ b/src/main/java/com/javaprogram/springbootapp/SpringBootAppApplication.java @@ -1,5 +1,7 @@ package com.javaprogram.springbootapp; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.annotation.ComponentScan; @@ -8,6 +10,8 @@ @SpringBootApplication public class SpringBootAppApplication { + private Logger logger = LoggerFactory.getLogger(getClass()); + public static void main(String[] args) { SpringApplication.run(SpringBootAppApplication.class, args); } @@ -28,4 +32,5 @@ public static void main(String[] args) { // factory.setPort(9000); factory.setContextPath("/myapp"); // factory.addErrorPages(new ErrorPage(HttpStatus.NOT_FOUND, "/notfound.html")); // return factory; } + } diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index fdf9fe9..e38d828 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -43,3 +43,9 @@ ## Number of selector threads to use. When the value is -1. #server.jetty.selectors=2 # + +server.servlet.context-path=/api/v1 +#spring.data.rest.basePath=/api/v1 + +#server.error.whitelabel.enabled=false +#spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.web.ErrorMvcAutoConfiguration diff --git a/src/main/resources/templates/400.html b/src/main/resources/templates/400.html new file mode 100644 index 0000000..f1c00d3 --- /dev/null +++ b/src/main/resources/templates/400.html @@ -0,0 +1,11 @@ + + + +

Sorry, Unexpected high volume.

+ +

Please give try after sometime.

+

+ Go Home +

+ + \ No newline at end of file diff --git a/src/main/resources/templates/404.html b/src/main/resources/templates/404.html new file mode 100644 index 0000000..d0359e0 --- /dev/null +++ b/src/main/resources/templates/404.html @@ -0,0 +1,9 @@ + + + +

You are looking for a page is not found

+

+ Go Home +

+ + \ No newline at end of file diff --git a/src/main/resources/templates/error.html b/src/main/resources/templates/error.html new file mode 100644 index 0000000..105041b --- /dev/null +++ b/src/main/resources/templates/error.html @@ -0,0 +1,12 @@ + + + +
+

Maintenance is in progress

+

Our Support team is working on it.

+

+ Please visit soon. +

+
+ + diff --git a/src/main/resources/templates/index.html b/src/main/resources/templates/index.html new file mode 100644 index 0000000..95c68c8 --- /dev/null +++ b/src/main/resources/templates/index.html @@ -0,0 +1,6 @@ + + +

Welcome Home

+

Success! It is working as we expected.

+ + diff --git a/src/main/resources/templates/send.html b/src/main/resources/templates/send.html new file mode 100644 index 0000000..cbc300c --- /dev/null +++ b/src/main/resources/templates/send.html @@ -0,0 +1,10 @@ + + + + +

Message has been sent to subscriber.

+ +