DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Cross-Platform Mobile Application Development: Evaluating Flutter, React Native, HTML5, Xamarin, and Other Frameworks
  • The Role of JavaScript Frameworks in Modern Web Development
  • Next.js vs. React: The Ultimate Guide To Choosing the Right Framework
  • Reasons to Use Tailwind CSS in React Native Development Projects

Trending

  • From 24 Hours to 2 Hours: How We Fixed a Broken BI System With Apache Airflow
  • Architecting Zero-Trust AI Agents: How to Handle Data Safely
  • How SaaS Architectures Break at Scale — and the Engineering Decisions That Prevent It
  • Mocking Kafka for Local Spring Development
  1. DZone
  2. Coding
  3. Frameworks
  4. Spring Boot and React in Harmony

Spring Boot and React in Harmony

Develop clean and maintainable business apps on top of Spring Boot and React, and do it faster using the Hilla framework.

By 
Tarek Oraby user avatar
Tarek Oraby
·
Sep. 28, 23 · Tutorial
Likes (5)
Comment
Save
Tweet
Share
7.2K Views

Join the DZone community and get the full member experience.

Join For Free

For many full-stack developers, the combination of Spring Boot and React has become a staple in building dynamic business applications. Yet, while powerful, this pairing has its set of challenges. From type-related errors to collaboration hurdles, developers often find themselves navigating a maze of everyday issues.

Enter Hilla, a framework that aims to simplify this landscape. If Hilla hasn't crossed your radar yet, this article will provide an overview of what it offers and how it can potentially streamline your development process when working with Spring Boot and React.

Spring Boot, React, and Hilla

For full-stack developers, the combination of Java on the backend and React (with TypeScript) on the frontend offers a compelling blend of reliability and dynamism. Java, renowned for its robust type system, ensures data behaves predictably, catching potential errors at compile-time. Meanwhile, TypeScript brings a similar layer of type safety to the JavaScript world, enhancing React's capabilities and ensuring components handle data as expected.

However, while both Java and TypeScript offer individual type-safe havens, there's often a missing link: ensuring that this type-safety is consistent from the backend all the way to the frontend. This is where the benefits of Hilla shine, enabling

  1. End-to-End Type Safety
  2. Direct Communication Between React and Spring Services
  3. Consistent Data Validation and Type Safety

End-To-End Type Safety

Hilla takes type safety a step further by ensuring it spans the entire development spectrum. Developers spend less time perusing API documentation and more time coding. With automatically generated TypeScript services and data types, Hilla allows developers to explore APIs directly within their IDE. This seamless integration means that if any code is altered, whether on the frontend or backend, any inconsistencies will trigger a compile-time error, ensuring that issues are caught early and rectified. 

End-To-End Type Safety

Direct Communication Between React and Spring Services

With Hilla, the cumbersome process of managing endpoints or deciphering complex queries becomes a thing of the past. Developers can directly call Spring Boot services from their React client, receiving precisely what's needed. This is achieved by making a Spring @Service available to the browser using Hilla's @BrowserCallable annotation. This direct communication streamlines data exchange, ensuring that the frontend gets exactly what it expects without any unnecessary overhead.

Here's how it works. First, you add @BrowserCallable annotation to your Spring Service:

Java
 
@BrowserCallable 
@Service
public class CustomerService {
    public List<Customer> getCustomers() {
        // Fetch customers from DB or API
    }
}


Based on this annotation, Hilla auto-generates TypeScript types and clients that enable calling the Java backend in a type-checkable way from the frontend (no need to declare any REST endpoints):

TypeScript
 
function CustomerList() {
    // Customer type is automatically generated by Hilla
    const [customers, setCustomers] = useState<Customer[]>([]);

    useEffect(() => {
      CustomerService.getCustomers().then(setCustomers);
    }, []);

    return (
      <ComboBox items={customers} ></ComboBox>
    )
}


Consistent Data Validation and Type Safety

One of the standout features of Hilla is its ability to maintain data validation consistency across the stack. By defining data validation rules once on the backend, Hilla auto-generates TypeScript validations for the frontend. This not only enhances developer productivity but also ensures that data remains consistent, regardless of where it's being processed. 

For instance, if a field is marked as @NotBlank in Java, Hilla ensures that the same validation is applied when this data is processed in the React frontend.

Java
 
public class Customer {

    @NotBlank(message = "Name is mandatory")
    private String name;

    @NotBlank(message = "Email is mandatory")
    @Email
    private String email;
  
    // Getters and setters

}


The Hilla useForm hook uses the generated TypeScript model to apply the validation rules to the form fields.

TypeScript
 
function CustomerForm() {

    const {model, field, read, submit} = useForm(CustomerModel, {
        onSubmit: CustomerService.saveCustomer
    });

    return (
    <div>
      <TextField label="Name" {...field(model.name)} />
      <EmailField label="Email" {...field(model.email)} />
      <Button onClick={submit}>Save</Button>
    </div>
    )
}


Batteries and Guardrails Included

Hilla streamlines full-stack development by offering pre-built tools, enhancing real-time capabilities, prioritizing security, and ensuring long-term adaptability.

The framework provides a set of pre-built UI components designed specifically for data-intensive applications. These components range from data grids and forms to various select components and editors. Moreover, for those looking to implement real-time features, Hilla simplifies the process with its support for reactive endpoints, removing the need for manual WebSocket management. Another notable aspect of Hilla is its security integration. By default, it's connected with Spring Security, offering robust access control mechanisms to safeguard data exchanges. The framework's stateless design ensures that as user demands increase, the application remains efficient. 

Hilla's design approach not only streamlines the current development process but also future-proofs your app. It ensures that all components integrate seamlessly, making updates, especially transitioning from one version to another, straightforward and hassle-free.

In Conclusion

Navigating the complexities of full-stack development in Spring Boot and React can be complex. This article highlighted how Hilla can alleviate many of these challenges. From ensuring seamless type safety to simplifying real-time integrations and bolstering security, Hilla stands out as a comprehensive solution. Its forward-thinking design ensures that as the tech landscape evolves, your applications remain adaptable and updates remain straightforward. 

For those immersed in the world of Spring Boot and React, considering Hilla might be a step in the right direction. It's more than just a framework; it's a pathway to streamlined and future-ready development.

JavaScript React (JavaScript library) Spring Boot Framework

Opinions expressed by DZone contributors are their own.

Related

  • Cross-Platform Mobile Application Development: Evaluating Flutter, React Native, HTML5, Xamarin, and Other Frameworks
  • The Role of JavaScript Frameworks in Modern Web Development
  • Next.js vs. React: The Ultimate Guide To Choosing the Right Framework
  • Reasons to Use Tailwind CSS in React Native Development Projects

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook