Almost every programmer knows the rule to “avoid reinventing the wheel” — use a good third-party framework or library whenever possible, but when it comes to actual development, I often find that they sometimes don’t know where the wheel is. Recently, I was working on a small business project with a couple of young programmers in my spare time, and while working on it together, I saw the following code almost everywhere I needed to determine if a string was empty:

if(inputString == null || inputString.length == 0){...... }Copy the code

In addition to string nullness, there are many string manipulation and other data type methods that inexperienced programmers often try to write on their own. There is nothing wrong with this code, of course, but we should try to leverage well-established third-party libraries to solve common problems in a more standard way and improve development efficiency.

Here are some of the best JAVA third-party libraries I’ve used in most projects:

JAVA Core Extensions

As mentioned earlier in the string determination example, the JAVA standard library provides the most basic methods of manipulating data types, but still lacks useful utility classes for some common requirement scenarios. While others are the JAVA standard library itself is not perfect, need to be supplemented by third-party libraries.

Apache Commons Lang

Apache Commons Lang is the most famous Apache JAVA library (GitHub code library). It is a great extension to java.lang. It contains a large number of very useful utility classes, including StringUtils, DateUtils, NumberUtils, etc. Using StringUtils, the previously mentioned code can be rewritten as:

if(StringUtils.isBlank(inputString)){... }Copy the code

In addition to Apache Commons Lang, there are several other Apache libraries that are good additions to JAVA itself, such as Apache Commons Collection, Apache Commons IO, Apache Commons Math


Add the Apache Commons Lang library to your Maven project:

Mons < dependency > < groupId > org.apache.com < / groupId > < artifactId > Commons - lang3 < / artifactId > < version > 3.4 < / version > </dependency>Copy the code

Google Guava

Google Guava is rarely used in domestic projects, but most of the foreign JAVA engineers I have worked with recommend it. It contains some of the core JAVA libraries that Google uses in its Own JAVA projects. Includes support for collections, caching, concurrent libraries, string processing, I/O, and more. In addition, Libraries developed by Google have always been known for their performance.

Add the following reference to add the library to your Maven project

<dependency> <groupId>com.google.guava</groupId> <artifactId> <version>19.0</version> </dependency>Copy the code

Joda-Time

Date support in Java versions prior to Java SE 8 was poor, and joda-time was often used to replace the old date system. It supported more calendar systems, provided a lot of convenient date handling methods, and performed very well. (GitHub codebase)

Add the following reference to add the library to your Maven project

<dependency>
  <groupId>joda-time</groupId>
  <artifactId>joda-time</artifactId>
  <version>2.9.3</version>
</dependency>
Copy the code

Web framework

Web frameworks are a central part of an application, so I always recommend using the most standard, community-supported frameworks, such as Spring and Struts.

Spring

Spring is an open source application framework, it contains many sub-projects such as Spring MVC, Spring Security, Spring Data, Sping Boot and so on, can meet almost all your needs on the project. It is also my preferred back-end framework for developing Web projects. (GitHub codebase)


Add the following reference to add the library to your Spring MVC project (only Spring Core support is introduced here)

<dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> < version > 4.2.5. RELEASE < / version > < / dependency >Copy the code

Struts 2

Struts 2, Apache’s best known Web framework, is also a free open source MVC framework. Struts also supports the latest technologies like REST, SOAP, and AJAX well. (GitHub codebase)


Add the following reference to add the library to your Maven project

< the dependency > < groupId > org. Apache. The struts < / groupId > < artifactId > struts 2 – core < / artifactId > < version > 2.3.28 < / version > </ Dependency > Besides the two longest Web frameworks mentioned above, there are also great ones like Google Web Toolkit, Tapestry, Strips and more to choose from.

Database (Persistence layer)

The choice of persistence layer framework is also critical to the success or failure of a project, which will directly affect the performance, quality, security and stability of the system.

MyBatis

MyBatis is my favorite database (persistence layer) framework because it is based entirely on SQL statements (extracting data through SQL and automatically mapping it to desired data objects) and gives me plenty of flexibility. (GitHub codebase)


Add the following reference to add the library to your Maven project (select the corresponding Maven library if you want to use it with Spring)

<dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.4.0</version>Copy the code

Spring JDBC / Spring Data

Spring JDBC is not a stand-alone Spring subproject, but rather a module integrated within the Spring core library that provides basic encapsulation handling for JDBC operations. After simple configuration, the result can be obtained by calling the jdbcTemplate in the Context.

String SQL = "select name from Student where id = ?";
String name = jdbcTemplateObject.queryForObject(SQL, new Object[]{10}, String.class);
Copy the code

Spring Data is a subproject of Spring that provides more powerful persistence layer functionality encapsulation and object mapping capabilities. It integrates well with Spring MVC. You can use JPA and CrudRepository to greatly simplify persistence layer development.

public interface EmployeeRepository extends CrudRepository<Employee, Long> { Employee findByFirstName(String firstName);  List<Employee> findByLastName(String lastName); }Copy the code



Hibernate

Probably the most widely used persistence layer framework in the country, it is very powerful, but it is not easy to use, you need to understand its internals, or you can have some unforeseen performance issues, especially when the data volume is very large. (GitHub codebase)


Add the following reference to add the library to your Maven project

< the dependency > < groupId > org. Hibernate < / groupId > < artifactId > hibernate - core < / artifactId > < version > 5.1.0. The Final < / version > </dependency>Copy the code

In addition to some of the most commonly used persistence layer libraries above, there are several excellent ones, such as JDO, JOOQ, Apache DbUtils, etc

The log

JAVA also includes logging functions, but it is not good at dealing with log classification, log storage, and log backup and archiving, so we usually use third-party log libraries to process logs in our projects.

SLF4J- Simple Logging Facade for Java (SLF4J)

SLF4J provides an abstraction layer for logging services based on which you can choose from different logging implementations such as java.util.logging, logback, and log4j. When you need to change the logging implementation component, you don’t need to change any code, just the corresponding configuration. (GitHub codebase)

Add the following reference to add the library to your Maven project

<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-api</artifactId>
    <version>1.7.21</version>
</dependency>
Copy the code

Apache Log4j

Log4j is the best known logging component that can be easily configured to log at all levels in a program, and its log files can be named and archived according to different rules.


Add the following reference to add the library to your Maven project

<dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4 j - core < / artifactId > < version > 2.5 < / version > < / dependency >Copy the code

Logback

Logback is newer than Log4j and is seen as an alternative to Log4j. It performs better than Log4j (log4j 2 probably performs better than Logback), more fully implements SLF4J’s interface, and comes with more features such as auto-compressed logging, more filters, and so on.

Add the following reference to add the library to your Maven project

< the dependency > < groupId > ch. Qos. Logback < / groupId > < artifactId > logback - core < / artifactId > < version > 1.1.7 < / version > </dependency>Copy the code

JSON

JSON has become the most widely used data transfer format, and as a result, processing of JSON in programs is becoming more and more common. Here are some JSON processing libraries I recommend:

Jackson

Jackson is a versatile Java library for working with JSON data. It makes it easy to convert between JSON data and Java objects. (GitHub codebase)

   ObjectMapper mapper = new ObjectMapper(); // can reuse, share globally
   User user = mapper.readValue(new File("user.json"), User.class);
Copy the code

Google Gson

JSON library developed by Google, which can realize the conversion between JSON string and JAVA objects, is also very convenient to use.

   Gson gson = new Gson();
   String[] strings = {"abc"."def"."ghi"}; gson.toJson(strings); / / = = > ["abc"."def"."ghi"]
Copy the code

The chart

JFreeChart

Can generate various types of charts for you, and support a variety of output formats, including PNG and JPEG image formats, as well as to PDF,EPS,SVG and other vector images.


JasperReports

JasperReports provides a complete reporting solution to help users develop reporting applications in the Java language. JasperReports’ template uses XML to extract data from databases and generate reports in PDF, HTML, XLS, CSV, and XML formats. One of its major advantages is its ability to handle reports with large data volumes.


test

JUnit

JUnit is the most widely used JAVA unit testing library so you can easily write your own unit test code and automate your tests. (GitHub codebase)


    @Test
    public void lookupEmailAddresses() {
        assertThat(new CartoonCharacterEmailLookupService().getResults("looney"), allOf(
            not(empty()),
            containsInAnyOrder(
                allOf(instanceOf(Map.class), hasEntry("id"."56"), hasEntry("email"."[email protected]")),
                allOf(instanceOf(Map.class), hasEntry("id"."76"), hasEntry("email"."[email protected]"))))); }Copy the code

Add the following reference to add the library to your Maven project

<dependency> <groupId> <artifactId>junit</artifactId> <version>4.12</version> </dependency>Copy the code

Office Document Processing

Apache POI

Apache POI is a free open source library for working with Microsoft Office documents. It can use Java to read and create, modify MS Excel files, MS Word and MS Spowerpoint files. (GitHub codebase)

Add the following reference to add the library to your Maven project

<dependency> <groupId>org.apache. Poi </groupId> <artifactId>poi</artifactId> <version>3.14</version> </dependency>Copy the code

docx4j

Docx4j is another set of JAXB-based Office document processing libraries (DOCX, PPTX, XLSX). (GitHub codebase)

Add the following reference to add the library to your Maven project

Docx4j </groupId> <artifactId>docx4j</artifactId> <version>3.3.0</version> </dependency>Copy the code

XML parsing

JDOM

JDOM is an open source project that uses pure JAVA technology to parse, generate, serialize, and manipulate XML documents based on a tree structure. In JDOM, XML elements are represented by elements, XML attributes are represented by attributes, and XML documents themselves are represented by Documents. So these apis are very intuitive and easy to use. (GitHub codebase)

Add the following reference to add the library to your Maven project

<dependency> <groupId>org.jdom</ artifactId>jdom</artifactId> <version>2.0.2</version> </dependency>Copy the code

DOM4J

DOM4J is an open source framework for processing XML that integrates XPath and fully supports DOM, SAX, JAXP, and other technologies.

Add the following reference to add the library to your Maven project

<dependency>
    <groupId>dom4j</groupId>
    <artifactId>dom4j</artifactId>
    <version>1.6.1</version>
</dependency>
Copy the code

xerces

Xerces is an open source XML parser. Since JDK1.5, Xerces has been the XML default implementation of the JDK

Add the following reference to add the library to your Maven project

<dependency>
    <groupId>xerces</groupId>
    <artifactId>xercesImpl</artifactId>
    <version>2.11.0</version>
</dependency>
Copy the code

Other code bases worth watching

jSOUP

JSOUP provides a set of apis for interacting with web pages (HTML) on the external Internet, making it very easy for users to parse HTML pages using CSS selectors to get the content they need.

Document doc = Jsoup.connect("http://en.wikipedia.org/").get();
Elements newsHeadlines = doc.select("#mp-itn b a");
Copy the code

Lomobok

Lombok is a Java utility that helps developers eliminate Java verbosity, especially for simple Java objects (POJOs). It does this through annotations. By adding Lombok to the IDE, developers can save a lot of time building methods like hashCode() and equals() and sorting various accessors and mutators.


Netty

Netty is a Java open source framework provided by JBOSS. Netty provides an asynchronous, event-driven network application framework and tools to rapidly develop high-performance and reliable network server and client programs. In other words, Netty is a niO-based client, server-side programming framework. Using Netty ensures that you can quickly and easily develop a network application, such as a client, server application that implements a certain protocol.


If you find other good third party libraries, please share them in the comments below, and I will continue to refine this list to make it more referential