React.js and Spring Data REST: Part 1 - Basic Features
To see updates to this code, visit our React.js and Spring Data REST tutorial. |
Welcome Spring community,
This is the first of several blog entries. In this session, you will see how to get a bare-bones Spring Data REST application up and running quickly. Then you will build a simple UI on top of it using Facebook’s React.js toolset.
Step 0 - Setting up your environment
Feel free to grab the code from this repository and follow along.
If you want to do it yourself, visit http://start.spring.io and pick these items:
- Rest Repositories
- Thymeleaf
- JPA
- H2
This demo uses Java 8, Maven Project, and the latest stable release of Spring Boot. This will give you a clean, empty project. From there, you can add the various files shown explicitly in this session, and/or borrow from the repository listed above.
In the beginning…
In the beginning there was data. And it was good. But then people wanted to access the data through various means. Over the years, people cobbled together lots of MVC controllers, many using Spring’s powerful REST support. But doing over and over cost a lot of time.
Spring Data REST addresses how simple this problem can be if some assumptions are made:
- The developer uses a Spring Data project that supports the repository model.
- The system uses well accepted, industry standard protocols, like HTTP verbs, standardized media types, and IANA-approved link names.
Declaring your domain
The cornerstone of any Spring Data REST-based application are the domain objects. For this session, you will build an application to track the employees for a company. Kick that off by creating a data type like this:
@Data
@Entity
public class Employee {
private @Id @GeneratedValue Long id;
private String firstName;
private String lastName;
private String description;
private Employee() {}
public Employee(String firstName, String lastName, String description) {
this.firstName = firstName;
this.lastName = lastName;
this.description = description;
}
}
-
@Entity
is a JPA annotation that denotes the whole class for storage in a relational table. -
@Id
and@GeneratedValue
are JPA annotation to note the primary key and that is generated automatically when needed. -
@Data
and@RequiredArgsConstructor
are Project Lombok annotations to autogenerate getters, setters, constructors, toString, hash, equals, and other things. It cuts down on the boilerplate.
This entity is used to track employee information. In this case, their name and job description.
Note
|
Spring Data REST isn’t confined to JPA. It supports many NoSQL data stores, but you won’t be covering those here. |
Defining the repository
Another key piece of a Spring Data REST application is to create a corresponding repository definition.
public interface EmployeeRepository extends CrudRepository<Employee, Long> {
}
-
The repository extends Spring Data Commons'
CrudRepository
and plugs in the type of the domain object and its primary key
That is all that is needed! In fact, you don’t even have to annotate this invisible if its top-level and visible. If you use your IDE and open up CrudRepository
, you’ll find a fist full of pre-built methods already defined.
Note
|
You can define your own repository if you wish. Spring Data REST supports that as well. |
Pre-loading the demo
To work this this application, you need to pre-load it with some data like this:
@Component
public class DatabaseLoader implements CommandLineRunner {
private final EmployeeRepository repository;
@Autowired
public DatabaseLoader(EmployeeRepository repository) {
this.repository = repository;
}
@Override
public void run(String... strings) throws Exception {
this…
This Week in Spring - September 1, 2015
Welcome to another installment of This Week in Spring! The Spring team is hard at work on all the latest and greatest ahead of the SpringOne2GX event in Washington DC! Time is really flying! I can't beleive what we're staring down September already! This week I'm in Tokyo, Japan, participating in the Spring User Group's huge Spring in Summer event where I gave a keynote and two talks, on Spring Boot and Spring Cloud. The one day event attracted some of the company's largest websites and was a lot of fun!
Anyway, without further ado, let's get…
This Week in Spring - August 25th, 2015
Welcome to another installment of This Week in Spring! This week I'm in Rio de Janeiro, Brazil for the lovely QCon Rio conference, and then I'm off to Tokyo, Japan for a Spring in Summer conference for local Spring users!
The team is abuzz with excitement leading up to this year's SpringOne2GX 2016, the biggest and best SpringOne2GX, ever! This year, you're going to see us do WAY more with WAY less and get it to production, to boot! This is exciting for everyone of us and it will be for audience members, as well!
- our pal Kenny Bastani put together a followup post in his series on Spring Cloud about polyglot persistence with Spring Cloud
- Spring for Hadoop lead Thomas Risberg just announced 2.3M2, complete with support for running batch tasklets, improved Spring Boot integration, and improved YARN support
- Singaporean Guest bloggers Han Lim and Tony Nguyen have written up a very cool blog on migrating from
.jsp
-based applications to Angular.js - John Thompson has a nice post on using Spring Boot with ActiveMQ over on DZone
- Saravanan Subramanian has a …
Migrating a Spring Web MVC application from JSP to AngularJS
Note on authors
This post is a guest post by Han Lim and Tony Nguyen. Han and Tony have done a great presentation at our Singapore Spring User Group on Spring + Angular JS. This blog is based on their presentation.
Abstract
In this article, we try to describe our experiences moving from server-side rendering view technologies like JSP, Struts and Velocity to client-side rendering view technologies using AngularJS, a popular Javascript framework for modern browsers. We will talk about some of the things to look out for when you are making this…
This Week in Spring - August 18, 2015
Welcome to another installment of This Week in Spring! As usual, we've got a lot to cover so let's get to it! This week I'm back in São Paulo, Brazil, to visit customers, then it's off to Rio for QCon Rio! If you're in either city, message me on Twitter and let's grab a coffee!
- Today, Pivotal CEO Paul Maritz stepped down [and welcomed Pivotal Labs founder Rob Mee as the new CEO. There's a post by Paul and by Rob. Thank you, Paul, and welcome Rob!
- the amazing Scott Frederick just announced the general availability of Spring Cloud Connectors 1.2.0! - check this out!
- Spring ninja Stéphane Nicoll just announced Spring Boot 1.3M4, with fixes and improvements aplenty!
- Spring Integration lead Gary Russell just announced a CRAZY packed new Spring Integration 4.2 release including, but not limited to, support for security Context Propagation, STOMP client channel adapters, metrics, new Spring Framework 4.2 event channel adapters, a process barrier component, last modified file list filters, codec, JMS shared subscriptions, (S)FTP improvements, SOAP Action Propagation, …
This Week in Spring - August 11, 2015
Welcome to another installation of This Week in Spring! As usual, we've got a lot to cover so let's get to it!
- Spring ninja Stephane Nicoll just announced Spring Boot 1.3.0.M3, chock full of great features including HTTP session persistence, JMS and AMQP improvements, logging improvements, and auto-configurations for the H2 Database console and embedded MongoDB.
- Spring Batch lead Michael Minella just announced Spring Batch 3.0.5
- Check out this cool look at using Spring Boot to process Twitter feeds with Neo4j
- check out this interesting Chinese-language post on using
.yml
with Spring Boot - Disid corporation has …
Spring Statemachine 1.0.0.M3 Released
We’re pleased to announce a third milestone release of Spring Statemachine 1.0.0.M3.
A lot has happened since a previous milestone and getting here took a bit more time we originally estimated. Now that we're here, let's crack it and see what new features we have in this release.
Beyond usual bug fixes here is a list of main new features:
- Distributed state machine
- Persisting state machine context
- Relax use of enums as states and events
- Programmatic instantiation of state machines
- New recipe modules
- New samples (persist, zookeeper, web)
…
This Week in Spring - August 4th, 2015
Welcome to another installment of This Week in Spring! This week I'm in Washington D.C., delivering some cloud-native realtalk at the Agile 2015 conference with my pal Andrew Clay Shafer then it's off to Denver, CO, again to speak to customers! If you're in either area, say hi!
We're fast approaching SpringOne2GX 2015! Things are getting exciting around here as the team delivers more and more amazing bits! The weeks to come, leading into SpringOne2GX, are my favorite time of the year. Watch this space for new releases aplenty!
- The biggest announcement, this week, of course, is the General availability of Spring framework 4.2! Get the bits while they're hot! This is a game changer release, packed with new features! I'll wait while you go read the new features list..
- ..Welcome back! Have you upgraded your applications already to Spring framework 4.2? Good! Next up on the list: Spring framework lead Juergen Hoeller's followup post looking forward to the next feature release of Spring Framework, 4.3
- Spring Session (and Spring Security!) lead Rob Winch just announced Spring Session 1.0.2…
This Week in Spring - July 28, 2015
Welcome to another installment of This Week in Spring! We've got a lot to cover so let's get to it!
- Spring Data lead Oliver Gierke just announced Spring Data SR2 which includes 40 issues fixed and includes an important update to the support for JTA 1.2's
@Transactional
- Spring Security lead Rob Winch has just announced Spring Security Kerberos 1.0.1 which includes Spring Security 4 and LDAP 2.0 support, as well as a fix for an issue.
- Spring Security lead Rob Winch doesn't sleep so he also just announced Spring Security 3.2.8
- Not resting on his laurels, Spring Security lead Rob Winch also announced Spring Security 4.0.2 which - among other things - includes numerous bug fixes, support for Spring framework 4.2, and minor improvements to Spring Security itself including a status code returning
…
Microservices with Spring
Introduction
NOTE: Revised July 2019
A simple example of setting up a microservices system using Spring, Spring Boot and Spring Cloud.
Microservices allow large systems to be built up from a number of collaborating components. It does at the process level what Spring has always done at the component level: loosely coupled processes instead of loosely coupled components.
For example imagine an online shop with separate microservices for user-accounts, product-catalog order-processing and shopping carts:
Inevitably there are a number of moving…