This is a reference spring project showing different functionalities. The idea is to use this as a reference for copying out snippets and functionalities.
Imp VM Parameters:
- -Dfile.encoding=UTF-8 ( to read sql initialization script properly )
- -Dsecret.api.value=secretValue
- -Dspring.profiles.active=local ( windows dev )
- -Dspring.profiles.active=dev ( from unix/deployed instance )
-Xmx512m -Xms512m
-Xlog:class+load=info:file=classload.log -Xlog:gc*:file=C:\_TEMP\Logs\GCLogs\gc.log
-Xmx512m -Dfile.encoding=UTF-8 -Dsecret.api.value=secretValue -Dspring.profiles.active=local
Local endpoints active are below - all end points are not mapped here. This is to give the user starting point. Please use the swagger for comprehensive list of endpoints.
- http://localhost:60501/swagger-ui/index.html
- http://localhost:60501/v3/api-docs
- http://localhost:60501/status
- http://localhost:60501/h2-console
The following can be used to set application/package/class level logging
logging.level.root=INFO
logging.level.com.java.springboot.controllers.StatusController=TRACE
Spring Logging Documentation :
- Standard Spring Logging Doc https://docs.spring.io/spring-boot/how-to/logging.html
- https://docs.spring.io/spring-boot/reference/features/logging.html#features.logging.custom-log-configuration
- https://logback.qos.ch/manual/appenders.html
- by default spring uses logback, which can be configured by logback.xml
- use
logback-spring.xml
instead oflogback.xml
as it allows spring to load custom configs - profile specific config can be loaded with
<springProfile name="dev">
- understanding how
maxHistory
works - https://pauldally.medium.com/why-is-my-logback-rolling-policy-keeping-more-than-maxhistory-files-66fbc865f38 maxHistory
changes behaviour based on policyType- use if you want to use the default spring config in your logback along with your own config
<include resource="org/springframework/boot/logging/logback/base.xml" />
- Spring Boot Logging Documentation
- for logback it will create log files in
C:\_TEMP\Logs\SpringBootLogs\CustomFileAppender\customLog.log
- Functionalities ( Console logging, Rolling file logging, Specific java package based logging )
- Console logging is inherited from
<include resource="org/springframework/boot/logging/logback/base.xml" />
- Additional file logging is done using
myRollingFileAppender
- Specific package based file logging is done with
myCustomFileAppender
using<logger>
- LogBook library can be used to log HTTP request/responses
- enabled by
logging.level.org.zalando.logbook.Logbook=TRACE
- https://www.baeldung.com/spring-logbook-http-logging
- enabled by
- Structured logging added with logstash encoder and
jsonConsoleAppender
appender
- add log4j2 dependency and exclude default logging dependency
implementation 'org.springframework.boot:spring-boot-starter-log4j2:3.3.4
exclude group: "org.springframework.boot", module:"spring-boot-starter-logging"
- comment log4j2 dependency and remove spring logging exclude
- comment
implementation 'org.springframework.boot:spring-boot-starter-log4j2:3.3.4
- comment
exclude group: "org.springframework.boot", module:"spring-boot-starter-logging"
monitorInterval="10"
works, but you must update thelog4j2.xml
inbuild\resources
- console, file-based and rolling file based implementation added for log4j2
- for envt specific log4j2 files refer - https://www.baeldung.com/spring-log4j2-config-per-profile
References :
- [logback] Understanding duplicate commons-logging issue in spring-boot - spring-projects/spring-framework#20611
- the same class in
commons-logging
is implemented byspring-jcl
as a fallback in case both are present in classpath org.apache.commons.logging.LogFactory
included incommons-logging
is given a dummy childLogFactoryService.java
- this causes the message to show when it gets initialized
Standard Commons Logging discovery in action with spring-jcl
org.apache.commons.logging.LogFactory
- spring-jcl sub-class
- Problems with Apache Commons Logging ( JCL ) - https://stackoverflow.com/questions/3222895/what-is-the-issue-with-the-runtime-discovery-algorithm-of-apache-commons-logging/
- Commons logging was my fault blog - https://radio-weblogs.com/0122027/2003/08/15.html
- the same class in
- [log4j2] Disadvantages of MemoryMappedAppender - https://stackoverflow.com/questions/22630547/disadvantages-of-using-memory-mapped-files
- [logback] logback conversion words - https://logback.qos.ch/manual/layouts.html#conversionWord
- [logback] baeldung logback articles - https://www.baeldung.com/tag/logback
- Mapped Dynamic Contexts (MDCs) are used for thread-specific logging - https://www.baeldung.com/mdc-in-log4j-2-logback
- Nested Dignostic Context ( NDC - not supported in SLF4J ) - https://www.baeldung.com/java-logging-ndc-log4j
- [log4j2] How to send logging into to Syslog Server - https://www.baeldung.com/log4j-to-syslog
Env Controller
View endpoints are as follows
- http://localhost:60501/
- http://localhost:60501/home
- http://localhost:60501/countries
- http://localhost:60501/starter
- http://localhost:60501/starter-blank
Csv endpoints as follows ( i18n handled by jvm param )
- http://localhost:60501/api/v1/csv/file/users
- http://localhost:60501/api/v1/csv/download/users
- http://localhost:60501/api/v1/csv/read/users ( read csv file )
Upload (Multipart) and Download File
- http://localhost:60501/api/v1/csv/upload/labels?overWrite=true
- http://localhost:60501/api/v1/csv/download/labels
Json endpoints as follows :
PDF Controller Using PDF Box : ( i18n achieved with dynamic font loading )
- http://localhost:60501/api/v1/pdf/file/users
- http://localhost:60501/api/v1/pdf/download/users
- http://localhost:60501/api/v1/pdf/download/password/users
Excel Controller using apache poi :
- http://localhost:60501/api/v1/excel/read/users
- http://localhost:60501/api/v1/excel/download/users
- http://localhost:60501/api/v1/excel/download/sample
XML Controller using JAXB :
- http://localhost:60501/api/v1/xml/read/users
- http://localhost:60501/api/v1/xml/file/users
- http://localhost:60501/api/v1/xml/download/users
Xpath Controller :
XSLT Controller :
- http://localhost:60501/api/v1/xslt/transform/users
- http://localhost:60501/api/v1/xslt/transform/download/users
Protocol Buffer Controller ( save/read from disk ) :
Global Custom Error Handling Page :
Caching and Scheduling, Conditional On Property ( LabelController ) :
- @Cacheable
- @Mockito
- Java File IO
password is 'password'
JVM Params :
- -Duser.timezone=UTC -Dfile.encoding=utf-8
###Features
- Internationalization added for file encodings
- save to csv locally & download
###References
- String Functions in Free Marker :
- Data Types in H2 Database :
- Free Unicode Font
- Json Parsing Tutorials
- Base64 Tutorial
- https://www.baeldung.com/java-base64-encode-and-decode
- URL and Filename safe Alphabet - https://www.ietf.org/rfc/rfc4648.txt
- XSLT Cheat Sheet
- Protocol Buffer :
- https://www.baeldung.com/google-protocol-buffer
- https://www.baeldung.com/spring-rest-api-with-protocol-buffers#overview
- https://github.com/protocolbuffers/protobuf/releases
- $ protoc --java_out=${OUTPUT_DIR} path/to/your/proto/file
- ..\src\main>protoc --java_out=java resources\protobuf\baeldung.proto
- Global Error Handler :
- Country language Codes ( en, ja, zh ) : ISO 639-1 Code
- General Spring Related Info
- Cron Expressions
- SPEL Support in JPA
- Hibernate Logging Properties
- Using Environment Variables
For further reference, please consider the following sections:
- Official Gradle documentation
- Spring Boot Gradle Plugin Reference Guide
- Create an OCI image
- Spring Boot DevTools
- Spring Web
- Apache Freemarker
The following guides illustrate how to use some features concretely:
- Building a RESTful Web Service
- Serving Web Content with Spring MVC
- Building REST services with Spring
These additional references should also help you: