⚡️ Preconfigured components to speedup Spring Boot development
Boilerplate components for Spring Boot.
<dependency>
<groupId>com.jpomykala</groupId>
<artifactId>spring-higher-order-components</artifactId>
<version>1.0.11</version>
</dependency>
Check version in maven repository
source: https://nickjanetakis.com
@EnableEmailSending
annotationThis component gives you simple API to send emails using Amazon SES service. Spring HOC will automatically create for you Amazon SES component if bean doesn't exist.
spring-hoc.mail.sender-email-address
spring-hoc.aws.access-key
, spring-hoc.aws.secret-key
, spring-hoc.aws.region
application.yml
configuration for e-mail sendingspring-hoc:
aws:
access-key: xxxxxxxx
secret-key: xxxxxxxx
region: eu-west-1
mail:
sender-email-address: "[email protected]"
This properties are required.
You can put My Company Name <[email protected]>
in sender-email-address
property to show "My Company Name" in e-mail apps instead plain e-mail.
Reference: https://docs.aws.amazon.com/ses/latest/DeveloperGuide/send-email-concepts-email-format.html
Use EmailRequest
step builder to create request.
EmailRequest.builder()
.to("[email protected]")
.subject("Hey, I just met you and this is crazy")
.body("But here's my number, so call me maybe")
.build();
Now it's time to send email. You have 2 options here.
@Autowire MailService
and invoke sendEmail(EmailRequest)
.EmailRequest
using ApplicationEventPublisher
That's all!
@SpringBootApplication
@EnableEmailSending
public class MySpringBootApplication {
public static void main(String[] args) {
SpringApplication.run(MySpringBootApplication.class, args);
}
// Send e-mail by event publishing, Spring HOC will listen to EmailRequest objects
@Autowired
private ApplicationEventPublisher eventPublisher;
@GetMapping("/send-email-by-event-publishing")
public void sendEmailByEventPublishing(){
EmailRequest emailRequest = EmailRequest.builder()
.to("[email protected]")
.subject("Hey, I just met you and this is crazy [event publishing]")
.body("But here's my number, so call me maybe")
.build();
eventPublisher.publishEvent(emailRequest);
}
// Send e-mail by mail service provided by Spring HOC and @EnableEmailSending annotation
@Autowired
private MailService mailService;
@GetMapping("/send-email-by-mail-service")
public void sendEmailByMailService(){
EmailRequest emailRequest = EmailRequest.builder()
.to("[email protected]")
.subject("Hey, I just met you and this is crazy [mail service]")
.body("But here's my number, so call me maybe")
.build();
mailService.send(emailRequest);
}
}
@EnableRequestLogging
annotationAdds logging requests, populate MDC with:
@SpringBootApplication
@EnableRequestLogging
public class MySpringBootApplication {
public static void main(String[] args) {
SpringApplication.run(MySpringBootApplication.class, args);
}
@Autowired
private MyUserService userService;
// [OPTIONAL] customize configuration
@Bean
public LoggingFilter loggingFilter(LoggingFilterFactory loggingFilterFactory) {
return loggingFilterFactory
.withPrincipalProvider(new PrincipalProvider() {
@Override
public String getPrincipal(HttpServletRequest request) {
return userService.findUserName(request);
}
})
.createFilter();
}
}
@Bean
public LoggingFilter loggingFilter(LoggingFilterFactory loggingFilterFactory){
return loggingFilterFactory
.withPrincipalProvider() // [optional] PrincipalProvider implementation
.withRequestIdProvider() // [optional] RequestIdProvider implementation
.withCustomMdc("user", "[u:%s][rid:%s]") // [optional] MDC key, String.format()
.createFilter();
}
@EnableFileUploading
annotationThis annotation autoconfigures Amazon S3 component if bean doesn't exit.
@Autowire UploadService
gives you ability to upload files using overloaded methods:
void upload(@NotNull UploadRequest uploadRequest)
void upload(@NotNull MultipartFile file)
void upload(@NotNull MultipartFile file, @NotNull String path)
void upload(byte[] bytes, String fileKey)
void upload(byte[] bytes, String fileKey, ObjectMetadata metadata)
String upload(byte[] bytes)
// path is autogenerated (sha256 hash)application.yml
configuration for file uploadingspring-hoc:
aws:
access-key: xxxxxxxx
secret-key: xxxxxxxx
region: eu-west-1
s3:
bucket-name: my-bucket
This properties are required.*
@SpringBootApplication
@EnableFileUploading
public class MySpringBootApplication {
public static void main(String[] args) {
SpringApplication.run(MySpringBootApplication.class, args);
}
@Autowired
private UploadService uploadService;
@GetMapping("/upload-file")
public String uploadFile(@RequestBody MultipartFile multipartFile) throws IOException {
String s3DownloadUrl = uploadService.upload(multipartFile);
return s3DownloadUrl;
}
}
@EnableResponseWrapping
annotationEvery @RestController
output will be wrapped into RestResponse<T>
object for JSON it will look like as follows:
{
msg: "OK"
status: 200
data: <your data>
pageDetails: <page details if you return Page from controller>
}
RestResponse
static contructors:
RestResponse ok(Object body)
RestResponse ok(Object body, PageDetails pageDetails)
RestResponse empty(String message, HttpStatus status)
RestResponse of(String message, HttpStatus status, Object data)
RestResponse of(String message, HttpStatus status, Object data, PageDetails pageDetails)
Every output will be wrapped into RestResponse
see this issue
Response wrapping can be disabled for specific endpoinds by using @DisableWrapping
annotation on method.
@SpringBootApplication
@EnableResponseWrapping
public class MySpringBootApplication {
public static void main(String[] args) {
SpringApplication.run(MySpringBootApplication.class, args);
}
@GetMapping("/wrap-pojo")
public MyPojo wrapResponse() {
MySpringBootApplication.MyPojo myPojo = new MyPojo("Jakub", "Pomykala");
return myPojo;
}
@Autowired
private MyPojoRepository myPojoRepository;
@GetMapping("/wrap-pojo-page")
public Page<MyPojo> wrapPageResponse() {
Page<MyPojo> myPojos = myPojoRepository.findAll();
return myPojos;
}
public class MyPojo {
private String firstName;
private String lastName;
// getters and setters
}
}
@EnableCORS
annotationThis annotation adds filter which handles CORS requests.
application.yml
configuration for CORSspring-hoc:
cors:
allow-credentials: true
allowed-origins:
- "https://my-frontend-application.com"
- "https://jpomykala.com"
allowed-methods:
- GET
- POST
- PATCH
- DELETE
This properties are optional.
By default CORS will accept all origins, all HTTP methods and all popular headers.
@SpringBootApplication
@EnableCORS
public class MySpringBootApplication {
public static void main(String[] args) {
SpringApplication.run(MySpringBootApplication.class, args);
}
}
Would you like to add something or improve source? Create new issue, let's discuss it
Source: https://github.com/jOOQ/jOOQ
GNU General Public License v3.0