Convenient packages that people who have decided to write Java should know

Introduction

I usually do web development in Go language, but recently I started web development with Java Spring, ** "This is too convenient !!" ** There were several packages, so I would like to introduce them.

lombok lombok can be omitted by simply annotating the boilerplate code of constructor, Getter, Setter, Builder, etc. This may be quite famous, but for me, who has only touched Java in university lectures, I was shocked to be able to omit redundant code considerably. This alone will boost development efficiency.

Example

Normal

class Sample {
  private String name; 

  public Sample() {}

  public Sample(String name) {
     this.name = name;
  }

  public String getName(){
    return name;
  }

  public void setName(String name) {
     this.name = name;
  }
}

↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓

Use lombok

@Getter 
@Setter
@AllArgsConstructor
@NoArgsConstructor
class Sample {
  private String name; 
}

That's all there is to it! Annotation ... It's pretty convenient!

ParameterizedTest ParameterizedTest is an annotation that can be used in a test package called JUnit5. By using this annotation, you can easily implement Table Driven Test, which is often used in Go language.

/*
* public String hogehoge(String str);
* append "hoge" str
*/
@ParameterizedTest
@MethodSource("hogeProvider")
void hogehogeTest(String input ,String expect) {
  assetEquals(hogehoge(input),expect);
}

static Stream<Arguments> hogeProvider() {
  return Stream.of(
    arguments("hogehoge","hogehogehoge"),//input,expect
    arguments("test","testhoge")
  );
}

You can easily put in many test cases.

Annotation ... ** I like it! ** **

Spring Sleuth Spring Sleuth is a tool that allows you to easily log. Just add it to your package!

Basically, for one request, the trace ID is always displayed at the same time as the log output. Therefore, at the time of log investigation,

$ xx yy.log |grep <Trace ID>


 You can check the log output in one request from above.

 Spring Sleuth isn't the only one! !!
 It is possible to propagate the trace ID!


 Recently, microservices have become mainstream, and I think that multiple systems and applications will be passed through Web API etc. in one process.
 So, the very annoying thing is to follow the log.

 For example, let's say you have a system called A-B-C connected. If a user makes a request to system A and an internal error occurs in system A, B and C may need to be investigated.
 However, since the systems are separated, when examining the log, it is necessary for each system to log in advance an ID that can be uniquely identified for one request flow.


 Trace ID can be automatically propagated between systems that have this Spring Sleuth installed. Therefore, the log can be investigated with the same trace ID, and data extraction becomes smooth. If you have introduced a cross-team system, I think it is possible to ask for a log survey just by telling the trace ID.

 ** Too convenient! !! !! ** **

## Summary
 Spring is really convenient, and most of it can be supplemented with existing packages, and Java is surprisingly cool for creating decent apps (implementations, tests, etc.)! I understood that.

 I want to get angry with my past self who hated Java!


 I haven't talked about DI and DI containers this time, but this function is also quite convenient, so I hope to introduce it somewhere again.

 Well then.


Recommended Posts

Convenient packages that people who have decided to write Java should know
Java 14 new features that could be used to write code
How to write java comments