web application – Devstyler.io https://devstyler.io News for developers from tech to lifestyle Mon, 14 Jun 2021 09:25:04 +0000 en-US hourly 1 https://wordpress.org/?v=6.8.5 6 Tips For Becoming A Better Java Developer https://devstyler.io/blog/2021/06/14/6-tips-for-becoming-a-better-java-developer/ Mon, 14 Jun 2021 09:25:04 +0000 https://devstyler.io/?p=54983 ...]]> 1. Learn JVM Internals and Java Performance Tuning

If you are serious about becoming a Rockstar Java Developer then you must first spend the time to learn JVM internals like what are different parts of JVM, how they work, JIT, JVM options, Garbage collections, and collectors, etc.

If you know the JVM well you can write both robust and high-performance Java application and that’s what Rockstar Java developers do. As part of this, you should also learn how to profile your Java application, how to find performance bottlenecks like which objects are taking most of your memories, and eating CPUs.

2. Learn Microservices and Cloud

Architecture is changing constantly and many companies are moving from monolithic applications to microservices.
It’s high time for Java developers to learn Microservice architecture and how to create Microservices in Java to take advantage of this recent wave.

Fortunately, the Spring framework provides Spring Cloud and Spring Boot which greatly simplify microservice development in Java. If you are looking for a course then Master Microservices with Spring Boot and Spring Cloud is a good one to start with.

3. Learn Spring Framework (Spring Boot)

It’s almost imperative nowadays for a Java developer to learn Spring framework as most of the companies prefer to do development using Spring frameworks like Spring MVC, Spring Boot, and Spring Cloud for developing a web application, REST APIs, and Microservices.

It also promotes best practices like dependency injection and makes your application more testable which is a key requirement for modern-day software.

4. Learn Java APIs and Libraries

If you have worked with great Java developers you might have noticed their overall knowledge of the Java Ecosystem and APIs forms a major part of it.

Java is the world’s most popular and mature programing language and there are tons of libraries and APIs available for doing almost everything possible.

Of course, you are not expected to know all of them but you should be familiar with some key APIs like JSON processing APIs like Jackson and Gson, XML processing APIs like JAXB and Xerces, Unit testing libraries like Mockito and JUnit, etc.

5. Learn Java 8+ (Java 13)

This is the most important thing for a Java developer right now. It’s good 4 years old and even Java 13 has been released 6 months back.

Almost all Java development jobs now required Java 8 skills and if you don’t have them, it would be very difficult to do well and perform well in your Java interviews.

6. Learn Design Patterns and Coding Best Practices

If you are writing a Java application from scratch then most of the time you are writing object-oriented code and design patterns are tried and tested solutions of common problems.

By knowing and incorporating them into your code you make your application more flexible and easier to change in the future.

]]>
Make Java REST development easier with the Jareto library https://devstyler.io/blog/2021/05/28/make-java-rest-development-easier-with-the-jareto-library/ Fri, 28 May 2021 15:42:37 +0000 https://devstyler.io/?p=52579 ...]]> The Java ecosystem makes it easy to implement REST services. Using Java EE, Jakarta EE, JAX-RS, and JSON-B, the source code of a REST service looks like a plain Java program, enhanced with a few easy-to-understand annotations.

With the MicroProfile REST client, the same has become true for implementing the invoking side.
You don’t have to manually translate between the Java domain objects and their on-the-wire representation, since this is done automatically by JSON-B (as opposed to JSON-P).

However, there are several common use cases that still require additional boilerplate code if you want to remain on this level of abstraction. Jareto is a small Java library that provides useful features in an easy-to-use way, for both server- and client-side development.

For mapping Java exceptions: On the server side, Jareto helps with serialization to HTTP wire data (JSON); these JSON exceptions can also be parsed by non-Java clients and are customizable and allow transport of structured data. On the client side, Jareto helps with client deserialization from HTTP wire data (JSON).

For transporting HTTP metadata: Jareto helps with HTTP status codes and headers.

Depending on your requirements, you can use the server-side part of Jareto, or the client-side part, or both.
Jareto, created by my company, SVC, is available on GitHub under the Apache License Version 2.0 License. There, you can also find a demo project with a sample web application and JUnit tests.

Jareto setup

Jareto’s features are implemented using JAX-RS providers. To prevent accidental activation of certain providers that you don’t need or want (with future extensions in mind), Jareto politely abstains from autoregistration of providers. Likewise, if you want to use Jareto in a REST client, you must register the Jareto provider classes during client construction.

Mapping Java exceptions

Without additional measures, throwing a runtime or checked exception from your service usually results in a stack trace being returned to the invoker. Apart from the security implications, this prevents clients from identifying and handling exceptional cases in an automated way, so this is usually not what you want.

JAX-RS specifies an unchecked WebApplicationException that allows you to customize both the HTTP response status and the returned payload. However, you would still have to

Provide the payload using a lower-level JAX-RS response

Take care of unexpected exceptions such as NullPointerException (by means of a global exception handler)
Jareto provides both a checked AppException and an unchecked AppRuntimeException that accept the following data during construction:

  • Error message
  • Error code
  • Error detail code (optional)
  • HTTP status code (optional; defaults to 500)

In theory, transporting exception data would also be possible via HTTP headers (instead of JSON inside the HTTP response body). Even though this alternative might seem appealing at first glance, it does not scale to advanced use cases where exceptions will contain more-complex, structured data. In this respect, Jareto’s exception handling resembles that of GraphQL, which is also capable of returning arbitrary error data inside the response payload.

By contrast, a MicroProfile REST client equipped with Jareto can catch and handle these exceptions.

Transporting HTTP metadata

Although a MicroProfile REST client greatly simplifies invocations by (re-)using the service’s Java interface, it does not offer any direct access to HTTP request headers or to HTTP response status and headers. To gain access to these, you would have to write additional ResponseExceptionMapper and ClientRequestFilter code.

The client-side API is deliberately designed to be usable also in standalone Java environments without Contexts and Dependency Injection (CDI). This way, running Java-based system tests against your REST service is quick and easy, as it does not require any CDI setup.

On the server side, simple access to the HTTP headers does not require anything other than HttpServletRequest and HttpServletResponse from Jakarta EE.

Alternatively, reading HTTP request headers is also possible by using the JAX-RS annotation HeaderParam.

In case you would like to add a static HTTP header to your responses, you can do so by using Jareto’s @Header annotation at the class or method level of your REST service interface or implementation.

]]>