All Projects → jpomykala → Spring Higher Order Components

jpomykala / Spring Higher Order Components

⚡️ Preconfigured components to speedup Spring Boot development

Programming Languages

java
68154 projects - #9 most used programming language

Projects that are alternatives of or similar to Spring Higher Order Components

Wicket Spring Boot
Spring Boot starter for Apache Wicket
Stars: ✭ 117 (+80%)
Mutual labels:  spring-boot, spring, spring-boot-starter
Disconf Spring Boot Starter
disconf-spring-boot-starter 让你可以使用spring-boot的方式开发依赖disconf的程序 只需要关心disconfi的配置文件和配置项,省略了编写xml的麻烦
Stars: ✭ 44 (-32.31%)
Mutual labels:  spring-boot, spring, spring-boot-starter
Telegram Spring Boot Starter
Telegram Bot API Spring Boot Starter
Stars: ✭ 79 (+21.54%)
Mutual labels:  spring-boot, spring, spring-boot-starter
Spring Thrift Api Gateway
Gateway for Apache Thrift requests processing that is built on Spring Cloud stack
Stars: ✭ 38 (-41.54%)
Mutual labels:  spring-boot, spring, spring-boot-starter
Grpc Spring Boot Starter
Spring Boot starter module for gRPC framework.
Stars: ✭ 1,829 (+2713.85%)
Mutual labels:  spring-boot, spring, spring-boot-starter
Bucket4j Spring Boot Starter
Spring Boot Starter for Bucket4j
Stars: ✭ 127 (+95.38%)
Mutual labels:  spring-boot, spring, spring-boot-starter
Logback Access Spring Boot Starter
Spring Boot Starter for Logback-access
Stars: ✭ 118 (+81.54%)
Mutual labels:  spring-boot, spring-boot-starter, logging
Cas Client Autoconfig Support
Annotation-based configuration support for Apereo CAS Java clients
Stars: ✭ 153 (+135.38%)
Mutual labels:  spring-boot, spring, spring-boot-starter
Spring Backend Boilerplate
The modularized backend boilerplate based on Spring Boot Framework, easy to get started and add your business part.
Stars: ✭ 134 (+106.15%)
Mutual labels:  spring-boot, spring-boot-starter, boilerplate
Jasypt Spring Boot
Jasypt integration for Spring boot
Stars: ✭ 1,948 (+2896.92%)
Mutual labels:  spring-boot, spring, spring-boot-starter
Grpc Spring Boot Starter
Spring Boot starter module for gRPC framework.
Stars: ✭ 2,190 (+3269.23%)
Mutual labels:  spring-boot, spring, spring-boot-starter
Logbook
An extensible Java library for HTTP request and response logging
Stars: ✭ 822 (+1164.62%)
Mutual labels:  spring-boot, spring-boot-starter, logging
Spring Boot
spring-boot 项目实践总结
Stars: ✭ 989 (+1421.54%)
Mutual labels:  spring-boot, spring
Botwall4j
A botwall for Java web applications
Stars: ✭ 41 (-36.92%)
Mutual labels:  spring-boot, spring
Dynamic Data Source Demo
基于事务的读写分离
Stars: ✭ 43 (-33.85%)
Mutual labels:  spring-boot, spring
Devops Service
DevOps Service is the core service of Choerodon. It integrated several open source tools to automate the DevOps process of planning, coding, building, testing, and deployment, operation, monitoring.
Stars: ✭ 36 (-44.62%)
Mutual labels:  spring-boot, spring
Mica Example
mica 演示项目
Stars: ✭ 42 (-35.38%)
Mutual labels:  spring-boot, spring
Ssh Shell Spring Boot
Spring shell in spring boot application over ssh
Stars: ✭ 43 (-33.85%)
Mutual labels:  spring-boot, spring-boot-starter
Jbone
jbone基于Spring Cloud框架开发,旨在为中小企业提供稳定的微服务解决方案,为开发人员提供基础开发骨架,jbone包含微服务中所有常用组件,例如注册中心、服务管理、服务监控、JVM监控、内存分析、调用链跟踪、API网关等等。业务功能包括系统权限的统一管理、单点登录、CMS、电商平台、工作流平台、支付平台等等。
Stars: ✭ 961 (+1378.46%)
Mutual labels:  spring-boot, spring
Mini Platform
Mini-Platform致力于更简洁易用的轻量级微服务治理平台。
Stars: ✭ 45 (-30.77%)
Mutual labels:  spring-boot, spring

spring-hoc

Build Status Maven Central codecov Maintainability

Boilerplate components for Spring Boot.

Installation

<dependency>
  <groupId>com.jpomykala</groupId>
  <artifactId>spring-higher-order-components</artifactId>
  <version>1.0.11/version>
</dependency>

Check version in maven repository

Motivation

  • Write inline code
  • Duplicate code a few times in different spots
  • Extract duplicate code into methods
  • Use your abstractions for a while
  • See how that code interacts with other code
  • Extract common functionality into internal library
  • Use internal library for extended periods of time
  • Really understand how all of the pieces come together
  • Create external open source library (we are here now)

source: https://nickjanetakis.com

Used by

simplelocalize


@EnableEmailSending annotation

This 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.

Configuration

  • Provide verified sender email address spring-hoc.mail.sender-email-address
  • Provide AWS credentials spring-hoc.aws.access-key, spring-hoc.aws.secret-key, spring-hoc.aws.region

Example application.yml configuration for e-mail sending

spring-hoc:
  aws:
    access-key: xxxxxxxx
    secret-key: xxxxxxxx
    region: eu-west-1
  mail:
    sender-email-address: "[email protected]"    

This properties are required.

Tip

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

How to send e-mail?

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).
  • Publish EmailRequest using ApplicationEventPublisher

That's all!

Example application with sending email (SES)

@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 annotation

Adds logging requests, populate MDC with:

  • user (IP address by default)
  • requestId (UUID by default).

Example application with request logging

@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();
  }
}

Customization of request logging

@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 annotation

This 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)

Example application.yml configuration for file uploading

spring-hoc:
  aws:
    access-key: xxxxxxxx
    secret-key: xxxxxxxx
    region: eu-west-1
  s3:
    bucket-name: my-bucket

This properties are required.*

Example application with files uploading

@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 annotation

Every @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.

Example application with response wrapping

@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 annotation

This annotation adds filter which handles CORS requests.

Example application.yml configuration for CORS

spring-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.

Example application with CORS filter

@SpringBootApplication
@EnableCORS
public class MySpringBootApplication {

  public static void main(String[] args) {
    SpringApplication.run(MySpringBootApplication.class, args);
  }

}

Contribution

Would you like to add something or improve source? Create new issue, let's discuss it

  • If in doubt, please discuss your ideas first before providing a pull request. This often helps avoid a lot of unnecessary work. In particular, we might prefer not to prioritise a particular feature for another while.
  • Fork the repository.
  • The commit message should reference the issue number.
  • Check out and work on your own fork.
  • Try to make your commits as atomic as possible. Related changes to three files should be committed in one commit.
  • Try not to modify anything unrelated.

Source: https://github.com/jOOQ/jOOQ

More

License

GNU General Public License v3.0

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].