1

00:00:00.07 --> 00:00:02.05

- One of those features that really got me



2

00:00:02.05 --> 00:00:04.09

excited with Spring 5.0 is the ability



3

00:00:04.09 --> 00:00:08.09

to have immutable objects,

especially in your web APIs.



4

00:00:08.09 --> 00:00:11.03

As a security-minded

developer, immutability



5

00:00:11.03 --> 00:00:14.06

within your object stack

is always a great thing.



6

00:00:14.06 --> 00:00:16.07

In more than security, when you're looking



7

00:00:16.07 --> 00:00:18.06

at encapsulating objects, especially those



8

00:00:18.06 --> 00:00:22.03

DTO objects, this is a

really, really good thing.



9

00:00:22.03 --> 00:00:24.05

So many people like to protect their DTOs,



10

00:00:24.05 --> 00:00:27.02

myself included, and immutability gives us



11

00:00:27.02 --> 00:00:29.01

the ability to do that.



12

00:00:29.01 --> 00:00:31.00

Well, this has always

kind of been impossible



13

00:00:31.00 --> 00:00:33.05

with Spring MVC because it had to have



14

00:00:33.05 --> 00:00:36.05

a nullable constructor

in order to do its work.



15

00:00:36.05 --> 00:00:39.00

Now, thank you to Kotlin and Lombok we now



16

00:00:39.00 --> 00:00:41.08

have a driver to make this possible,



17

00:00:41.08 --> 00:00:43.09

and with Spring 5.0 we get it.



18

00:00:43.09 --> 00:00:45.07

Now just a quick overview of why



19

00:00:45.07 --> 00:00:47.07

this is important before

we look at the code.



20

00:00:47.07 --> 00:00:49.06

This gives us the ability

to have protection



21

00:00:49.06 --> 00:00:51.05

of our objects.



22

00:00:51.05 --> 00:00:53.08

It also gives us a way

to prevent application



23

00:00:53.08 --> 00:00:56.00

state from changing unexpectedly.



24

00:00:56.00 --> 00:00:58.07

Once I create an object,

it's done, that's it,



25

00:00:58.07 --> 00:01:01.00

and that's really the

value of immutability



26

00:01:01.00 --> 00:01:03.01

and it allows us to have

much stronger contracts



27

00:01:03.01 --> 00:01:07.03

because no one can change

those objects unexpectedly.



28

00:01:07.03 --> 00:01:09.06

So let's take a look at this in code.



29

00:01:09.06 --> 00:01:11.00

We're going to load up our application.



30

00:01:11.00 --> 00:01:12.05

Now there's a couple

things that we need to do



31

00:01:12.05 --> 00:01:14.05

before we can get started, and that's just



32

00:01:14.05 --> 00:01:17.01

because some of the tooling

that exists out there



33

00:01:17.01 --> 00:01:19.00

doesn't yet support HTTP2.



34

00:01:19.00 --> 00:01:23.07

So we actually need to go

turn off all of our HTTP2.



35

00:01:23.07 --> 00:01:26.04

So I'm just going to comment

out all of those properties.



36

00:01:26.04 --> 00:01:28.02

I don't really want to get rid of 'em.



37

00:01:28.02 --> 00:01:30.01

We may use 'em later.



38

00:01:30.01 --> 00:01:32.00

And now that that's done,

let's open up source



39

00:01:32.00 --> 00:01:34.09

main java and enter main package here.



40

00:01:34.09 --> 00:01:37.04

We are going to create a new class file



41

00:01:37.04 --> 00:01:40.05

and we're going to call

that class president.



42

00:01:40.05 --> 00:01:43.00

To our president we're

going to add a Lombok



43

00:01:43.00 --> 00:01:46.07

annotation called at value.



44

00:01:46.07 --> 00:01:48.06

Now this is another one of those things



45

00:01:48.06 --> 00:01:50.09

that I may be a great feature of IntelliJ



46

00:01:50.09 --> 00:01:54.02

Ultimate Edition that

your IDE doesn't have.



47

00:01:54.02 --> 00:01:56.07

IntelliJ Ultimate Edition

actually fully supports



48

00:01:56.07 --> 00:01:58.04

Lombok out of the box.



49

00:01:58.04 --> 00:02:01.05

Check with your IDE provider

and see if it's available.



50

00:02:01.05 --> 00:02:04.00

It may not be and you

may have to rely just on



51

00:02:04.00 --> 00:02:06.05

having errors in your

IDE that actually get



52

00:02:06.05 --> 00:02:08.07

resolved at compilation time.



53

00:02:08.07 --> 00:02:10.05

Some of the value of

Lombok is that I don't



54

00:02:10.05 --> 00:02:12.09

actually have to play getters and setters.



55

00:02:12.09 --> 00:02:13.09

So I'm going to go ahead and put a couple



56

00:02:13.09 --> 00:02:16.07

attributes here, the

first being an integer



57

00:02:16.07 --> 00:02:20.06

of name ID.



58

00:02:20.06 --> 00:02:24.06

The second being a string of first name.



59

00:02:24.06 --> 00:02:28.07

And the last one being

a string of last name.



60

00:02:28.07 --> 00:02:29.07

And that's it.



61

00:02:29.07 --> 00:02:31.01

The add value annotation's

actually going to give



62

00:02:31.01 --> 00:02:32.09

me a constructor that takes all



63

00:02:32.09 --> 00:02:37.09

of those arguments and getters

but no setters on this class.



64

00:02:37.09 --> 00:02:39.03

Let's go ahead and close

that down and we're



65

00:02:39.03 --> 00:02:42.03

going to create another

new class, and this one



66

00:02:42.03 --> 00:02:46.07

we are going to call president controller.



67

00:02:46.07 --> 00:02:48.06

And then to our president

controller we are going



68

00:02:48.06 --> 00:02:52.07

to add a rest controller

annotation as well



69

00:02:52.07 --> 00:02:59.09

as a request mapping annotation.



70

00:02:59.09 --> 00:03:02.00

And now we are going to create an internal



71

00:03:02.00 --> 00:03:10.04

memory store, so a list

of president objects.



72

00:03:10.04 --> 00:03:16.03

Call this presidents, set it

equal to a new array list.



73

00:03:16.03 --> 00:03:19.07

This is just so we have somewhere

to put our stuff later on.



74

00:03:19.07 --> 00:03:23.00

We're going to set a post mapping



75

00:03:23.00 --> 00:03:25.05

and on that post mapping we will return



76

00:03:25.05 --> 00:03:31.01

a president object, but we

are going to add a president.



77

00:03:31.01 --> 00:03:40.00

We will take a request

body of type president



78

00:03:40.00 --> 00:03:44.04

and we will simply do

a presidents dot add,



79

00:03:44.04 --> 00:03:50.00

add our president, and then return him.



80

00:03:50.00 --> 00:03:51.05

Now I'm going to make one

more quick change here.



81

00:03:51.05 --> 00:03:57.00

I'm actually going to

remove my push controller



82

00:03:57.00 --> 00:03:58.07

'cause I don't want

that push handler to get



83

00:03:58.07 --> 00:04:02.02

in the way now that we've

actually turned off HTTP20.



84

00:04:02.02 --> 00:04:06.00

And we'll start up our application.



85

00:04:06.00 --> 00:04:11.00

We'll let it start up here.



86

00:04:11.00 --> 00:04:13.08

Now, prior to having the

support for immutability



87

00:04:13.08 --> 00:04:15.08

what I'm about to show

you would've never worked.



88

00:04:15.08 --> 00:04:17.04

It actually would've

failed and said that it



89

00:04:17.04 --> 00:04:19.07

didn't have a default constructor.



90

00:04:19.07 --> 00:04:22.00

Let's jump over to a terminal window.



91

00:04:22.00 --> 00:04:25.01

So we're going to use

the curl command here,



92

00:04:25.01 --> 00:04:31.06

dash x post at local host

8080 slash presidents



93

00:04:31.06 --> 00:04:40.08

dash h with a content type

application slash json.



94

00:04:40.08 --> 00:04:44.05

We're going to do a dash

d, open curly brace,



95

00:04:44.05 --> 00:05:02.07

ID one, first name J-E-O-R-G-E,

last name Washington.



96

00:05:02.07 --> 00:05:05.04

Then that out and we'll

go ahead and hit return.



97

00:05:05.04 --> 00:05:07.00

And we got a response back.



98

00:05:07.00 --> 00:05:10.08

And that response came

from the actual server.



99

00:05:10.08 --> 00:05:12.05

So we're going to do an HTTP action,



100

00:05:12.05 --> 00:05:15.08

and this the HTTPIE tool that I use.



101

00:05:15.08 --> 00:05:17.03

If you don't have it, you can do a similar



102

00:05:17.03 --> 00:05:19.09

action with curl or you can install this.



103

00:05:19.09 --> 00:05:21.05

It's totally up to you.



104

00:05:21.05 --> 00:05:27.06

We're going to do a post at

local host 8080 slash presidents.



105

00:05:27.06 --> 00:05:31.09

We're going to set our ID

equal to one, our first



106

00:05:31.09 --> 00:05:37.03

name equal to George, then our last name



107

00:05:37.03 --> 00:05:40.01

equal to Washington.



108

00:05:40.01 --> 00:05:42.07

And we're going to set that to verbose.



109

00:05:42.07 --> 00:05:44.08

So what you'll see here

is we actually did a post



110

00:05:44.08 --> 00:05:47.03

over it should be one,

one, with a first name



111

00:05:47.03 --> 00:05:48.07

ID and last name.



112

00:05:48.07 --> 00:05:50.02

And then we got that response back out



113

00:05:50.02 --> 00:05:52.01

which is what we would expect, but most



114

00:05:52.01 --> 00:05:54.05

importantly we got a 200 out.



115

00:05:54.05 --> 00:05:55.09

Now the interesting thing

here is if you'd done



116

00:05:55.09 --> 00:05:59.01

this prior to version 5.0 of Spring, again



117

00:05:59.01 --> 00:06:00.09

it would've failed

because it didn't actually



118

00:06:00.09 --> 00:06:03.05

have a constructor that it could call.



119

00:06:03.05 --> 00:06:05.06

But we now have that, so

we now have an immutable



120

00:06:05.06 --> 00:06:08.07

object in our application

that we could call



121

00:06:08.07 --> 00:06:09.08

through the web API.



