PoshJosh's Blog

Some common mistakes when developing java web applications

February 05, 2021

  • Fix probable synchronization issues with shared resource. Objects which may be accessed by multiple thread, need to be properly synchronized. Servlets, Spring Controllers/Services are examples of such objects.

  • Add support for multiple environments. Add support for development, staging and production environments. This will help improve security and productivity (flexibility in development and staging). Spring Profiles provide a way to segregate parts of your application configuration and make it only available in certain environments.

  • Add constraints to models. In the Java Persistence API (JPA), constraints are a way to enforce and document database rules. For example, if you want Pizza names to be unique to each record, then unique constraints should be added to the corresponding model object field. For example:

@Entity
public class Employee{
  @Column(unique = true)
  private String username;
}
  • Use BigDecimal for money related variables. If the intention is to store the exact value of money related variables, then use java.math.BigDecimal and not floating point numbers to represent such variables. This is because, under certain conditions floating point numbers suffer loss of significance leading to inaccurate scale (scale refers to digits after the decimal point). java.math.BigDecimal should thus be used for money related fields.

  • Use Spring Boot derived query methods. Prefer derived query methods to hardcoded SQL queries. Whereas hardcoded SQL queries may be faster, derived query methods are standard, easier to maintain and implicitly documented. To use derived query methods in Spring Data JPA you simply follow the method naming convention and the query is derived for you.

  • Add support for pagination and sorting. Support should be added for paging and sorting to ensure results are returned in pages. This way results containing many records will be returned incrementally. Spring Data JPA supports pagination and sorting out of the box and it is quite easy to set up.

  • Standardize the API.

    • Version the API. Versioning the API enables us have different versions of our API if we’re making any changes to them that may break clients. This way, we can gradually phase out old endpoints instead of forcing everyone to move to the new API at the same time. To version the API, add a version number to its links for example use /v1/books/1 rather than /books/1.

    • Standardize API links. No need to use verbs (e.g /createBook) in endpoint paths. Rather, make use of the existing REST verbs. For example to:

      • Create a new pizza - POST /v1/pizzas
      • Edit an existing pizza (having id = 1) - PUT /v1/pizzas/1
      • Delete an existing pizza (having id = 1) - DELETE /v1/pizzas/1
      • Get an existing pizza (having id = 1) - GET /v1/pizzas/1
      • Get all pizzas having topping - GET /v1/pizzas?topping=???
  • Use a data transfer layer between models and client. Use Data Transfer Objects (DTO) to define the data you want transferred. This is especially necessary when some domain objects load associated objects eagerly (as indicated by FetchType.EAGER). Using DTOs will prevent unnecessary data being transferred from the business layer to the client.

  • Use properties where necessary. Prefer properties to literal values. This makes code configurable and maintainable.

  • Use boxed types for Entity Ids. Prefer boxed types to primitives. Primitives have a default value of zero. This zero - value may lead to conflicts. For example, an entity with id of zero may be seen, as already existing.

  • Use proper generation type for auto generated values. Using GenerationType.AUTO leaves it to the JPA implementation to decide which generation strategy to use. Consider explicitly specifying a generation strategy.

  • Persist detached entities before using in relationships. When saving an entity to the database, make sure any of its detached relations are first persisted. If not org.hibernate.PersistentObjectException will be thrown. For example, an entity created with data passed from the client will be detached from the database. To ensure such entity is not detached:

    • If the pizza has an id which refers to an existing entity, then load that entity from the database.

    • If the pizza does not have an id, then save the pizza to the database and use the return value from the save method.

  • Use Optional when null may be returned from a method. Consider wrapping the return value in an Optional for methods whose return value may be null.

  • Use development database. Consider using a development database. For example, H2 is convenient to use during development and even has in-memory database mode.


Written byChinomso IkwuagwuExcélsior

Limited conversations with distributed systems.

Modifying legacy applications using domain driven design (DDD)

Gherkin Best Practices

Code Review Best Practices

Hacking Cypress in 9 minutes

Some common mistakes when developing java web applications

How to make a Spring Boot application production ready

SQL JOINS - A Refresher

Add Elasticsearch to Spring Boot Application

Add entities/tables to an existing Jhipster based project

CSS 3 Media Queries - All over again

Maven Dependency Convergence - quick reference

Amazon SNS Quick Reference

AWS API Gateway Quick Reference

Amazon SQS Quick Reference

AWS API Gateway Quick Reference

AWS Lambda Quick Reference

Amazon DynamoDB - Quick Reference

Amazon Aurora

Amazon Relational Database Service

AWS Database Services

AWS Security Essentials

Amazon Virtual Private Cloud Connectivity Options

Summary of AWS Services

AWS Certified Solutions Architect - Quick Reference

AWS CloudFront FAQs - Curated

AWS VPC FAQs - Curated

AWS EC2 FAQs - Curated

AWS Achritect 5 - Architecting for Cost Optimization

AWS Achritect 4 - Architecting for Performance Efficiency

AWS Achritect - 6 - Passing the Certification Exam

AWS Achitect 3 - Architecting for Operational Excellence

AWS Achitect 2 - Architecting for Security

AWS Achitect 1 - Architecting for Reliability

Amazon DynamoDB Accelerator (DAX)

Questions and Answers - AWS Certified Cloud Architect Associate

Questions and Answers - AWS Certified Cloud Architect Associate

AWS Connectivity - PrivateLink, VPC-Peering, Transit-gateway and Direct-connect

AWS - VPC peering vs PrivateLink

Designing Low Latency Systems

AWS EFS vs FSx

AWS Regions, Availability Zones and Local Zones

AWS VPC Endpoints and VPC Endpoint Services (AWS Private Link)

AWS - IP Addresses

AWS Elastic Network Interfaces

AWS Titbits

Jenkins on AWS - Automation

Jenkins on AWS - Setup

Jenkins on AWS - Best practices

Introduction to CIDR Blocks

AWS Lamda - Limitations and Use Cases

AWS Certified Solutions Architect Associate - Part 10 - Services and design scenarios

AWS Certified Solutions Architect Associate - Part 9 - Databases

AWS Certified Solutions Architect Associate - Part - 8 Application deployment

AWS Certified Solutions Architect Associate - Part 7 - Autoscaling and virtual network services

AWS Certified Solutions Architect Associate - Part 6 - Identity and access management

AWS Certified Solutions Architect Associate - Part 5 - Compute services design

AWS Certified Solutions Architect Associate - Part 4 - Virtual Private Cloud

AWS Certified Solutions Architect Associate - Part 3 - Storage services

AWS Certified Solutions Architect Associate - Part 2 - Introduction to Security

AWS Certified Solutions Architect Associate - Part 1 - Key services relating to the Exam

AWS Certifications - Part 1 - Certified solutions architect associate

AWS Virtual Private Cloud (VPC) Examples

Curated info on AWS Virtual Private Cloud (VPC)

Notes on Amazon Web Services 8 - Command Line Interface (CLI)

Notes on Amazon Web Services 7 - Elastic Beanstalk

Notes on Amazon Web Services 6 - Developer, Media, Migration, Productivity, IoT and Gaming

Notes on Amazon Web Services 5 - Security, Identity and Compliance

Notes on Amazon Web Services 4 - Analytics and Machine Learning

Notes on Amazon Web Services 3 - Managment Tools, App Integration and Customer Engagement

Notes on Amazon Web Services 2 - Storages databases compute and content delivery

Notes on Amazon Web Services 1 - Introduction

AWS Auto Scaling - All you need to know

AWS Load Balancers - How they work and differences between them

AWS EC2 Instance Types - Curated

Amazon Web Services - Identity and Access Management Primer

Amazon Web Services - Create IAM User

Preparing Jenkins after Installation

Jenkins titbits, and then some

Docker Titbits

How to Add Chat Functionality to a Maven Java Web App

Packer - an introduction

Terraform - an introduction

Versioning REST Resources with Spring Data REST

Installing and running Jenkins in Docker

Automate deployment of Jenkins to AWS - Part 2 - Full automation - Single EC2 instance

Automate deployment of Jenkins to AWS - Part 1 - Semi automation - Single EC2 instance

Introduction to Jenkins

Software Engineers Reference - Dictionary, Encyclopedia or Wiki - For Software Engineers