1

00:00:00.06 --> 00:00:04.00

- [Instructor] There a few

notable in Spring 5 with JSON



2

00:00:04.00 --> 00:00:06.02

and Protocol Buffer handling.



3

00:00:06.02 --> 00:00:07.06

So there were a couple of upgrades.



4

00:00:07.06 --> 00:00:11.01

The Jackson libraries were

upgrade by default of 3.9,



5

00:00:11.01 --> 00:00:15.05

and Protobuf or Protocol Buffers

were upgraded to version 3.



6

00:00:15.05 --> 00:00:17.08

One of the bigger changes

however in this arena,



7

00:00:17.08 --> 00:00:20.07

is the implementation of JSON-B.



8

00:00:20.07 --> 00:00:23.00

The JSON-B is native support for JSON



9

00:00:23.00 --> 00:00:25.00

within the Java Framework.



10

00:00:25.00 --> 00:00:27.09

It follows many of the same

concepts that we got with JAXB



11

00:00:27.09 --> 00:00:30.05

which was the XML Java Libraries,



12

00:00:30.05 --> 00:00:32.02

and this is really good about maintaining



13

00:00:32.02 --> 00:00:34.07

consistency between the two.



14

00:00:34.07 --> 00:00:38.05

This is specified by JSR 367 and Spring



15

00:00:38.05 --> 00:00:40.09

provides full support.



16

00:00:40.09 --> 00:00:43.04

Now there is a couple of

Current Implementations.



17

00:00:43.04 --> 00:00:46.08

There's Eclipse Yasson, which

the reference implementation



18

00:00:46.08 --> 00:00:48.07

and that's what we're going

to use here in a moment,



19

00:00:48.07 --> 00:00:51.09

and then there's also

a Johnzon, from Apache,



20

00:00:51.09 --> 00:00:54.07

which is a similar

implementation from them.



21

00:00:54.07 --> 00:00:57.02

So let's jump into code

and see what we need to do,



22

00:00:57.02 --> 00:01:00.07

to go use JSON-B within

a Spring application.



23

00:01:00.07 --> 00:01:05.05

Now in our application, we're

going to open up our pom file.



24

00:01:05.05 --> 00:01:06.05

We're going to scroll

down a little bit here,



25

00:01:06.05 --> 00:01:08.04

and we're going to add a few dependencies,



26

00:01:08.04 --> 00:01:09.08

and we're going to do so at



27

00:01:09.08 --> 00:01:12.07

the end of our compiled dependencies.



28

00:01:12.07 --> 00:01:15.06

So the first dependency that

we're going to add here,



29

00:01:15.06 --> 00:01:19.02

is javax.json.bind-api,



30

00:01:19.02 --> 00:01:23.09

and that comes from the

group javax.json.bind.



31

00:01:23.09 --> 00:01:26.08

And we'll select version 1.0.



32

00:01:26.08 --> 00:01:29.08

Now the next dependency

along the similar lines,



33

00:01:29.08 --> 00:01:32.04

is going to be yasson itself,



34

00:01:32.04 --> 00:01:35.06

and that comes from org.eclipse,



35

00:01:35.06 --> 00:01:38.07

and the version on that is 1.0.



36

00:01:38.07 --> 00:01:41.04

And the final dependency

that we're going to add



37

00:01:41.04 --> 00:01:47.01

is javax.json and this

is from org.glassfish,



38

00:01:47.01 --> 00:01:50.00

and the version on that

we're going to select is 1.1.



39

00:01:50.00 --> 00:01:52.01

Now we'll allow Maven to do its thing,



40

00:01:52.01 --> 00:01:54.07

and bring in all its dependencies.



41

00:01:54.07 --> 00:01:56.05

And once those are resolved,

we're actually going to go



42

00:01:56.05 --> 00:01:59.02

open up our application

of properties file.



43

00:01:59.02 --> 00:02:01.09

We need to add a property

to let Spring know,



44

00:02:01.09 --> 00:02:04.03

that we want to use JSON-B.



45

00:02:04.03 --> 00:02:11.04

So it's

spring.http.converters.preferred-json-mapper,



46

00:02:11.04 --> 00:02:13.07

and we're going to select jsonb.



47

00:02:13.07 --> 00:02:16.00

Now with that there's

actually a bug and we're



48

00:02:16.00 --> 00:02:18.03

going to need to address that real quick.



49

00:02:18.03 --> 00:02:22.02

You cannot do immutable

objects with JSON-B, currently.



50

00:02:22.02 --> 00:02:24.04

I've logged a bug against what I saw,



51

00:02:24.04 --> 00:02:27.00

and I'm hoping that

gets fixed here shortly.



52

00:02:27.00 --> 00:02:29.04

So we're going to convert

this to a data object,



53

00:02:29.04 --> 00:02:31.02

from a value object.



54

00:02:31.02 --> 00:02:34.02

What that does, now I

get getters and setters,



55

00:02:34.02 --> 00:02:37.03

a default constructor and

an AllArgs constructor,



56

00:02:37.03 --> 00:02:39.03

all within this object.



57

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

So, it really reverts us back to



58

00:02:42.03 --> 00:02:44.06

pre using immutable objects.



59

00:02:44.06 --> 00:02:47.08

Let's jump into our

Presidents Controller now.



60

00:02:47.08 --> 00:02:51.05

We're going to make, just

a couple of changes here.



61

00:02:51.05 --> 00:02:55.01

We're going to go ahead and

add a GetMapping in here.



62

00:02:55.01 --> 00:02:58.06

GetMapping is going to return a list,



63

00:02:58.06 --> 00:03:01.01

of President objects.



64

00:03:01.01 --> 00:03:03.06

Call it getAllPresidents.



65

00:03:03.06 --> 00:03:04.08

We don't need any parameters,



66

00:03:04.08 --> 00:03:09.00

and we will simply return the Presidents.



67

00:03:09.00 --> 00:03:10.07

Excellent, so now that's done,



68

00:03:10.07 --> 00:03:14.01

we can go startup our application.



69

00:03:14.01 --> 00:03:17.02

Expand this view of the

console a little bit.



70

00:03:17.02 --> 00:03:19.00

And now that our application is started,



71

00:03:19.00 --> 00:03:21.00

we're going to jump over to our terminal.



72

00:03:21.00 --> 00:03:22.06

We're going to do a

couple of commands here.



73

00:03:22.06 --> 00:03:24.03

The first one that we're going to do,



74

00:03:24.03 --> 00:03:27.02

so that I can use my http tool once again,



75

00:03:27.02 --> 00:03:33.04

and I'm going to POST to

localhost:8080/presidents and



76

00:03:33.04 --> 00:03:43.05

id:=1 firstName=George lastName=Washington



77

00:03:43.05 --> 00:03:46.05

and we'll go ahead and do that verbose.



78

00:03:46.05 --> 00:03:48.07

And it looks the exact

same as we saw before.



79

00:03:48.07 --> 00:03:50.09

But now we're using a different parser,



80

00:03:50.09 --> 00:03:53.00

and we don't really see much to that,



81

00:03:53.00 --> 00:03:54.05

it's just that it's nice to have within



82

00:03:54.05 --> 00:03:56.03

the Spring Framework.



83

00:03:56.03 --> 00:03:59.00

Just to show you that it really

does return the same thing,



84

00:03:59.00 --> 00:04:05.01

we'll go ahead and do an http

on localhost:8080/presidents



85

00:04:05.01 --> 00:04:07.01

- V for verbose



86

00:04:07.01 --> 00:04:08.06

and we return that object.



87

00:04:08.06 --> 00:04:10.09

It looks much the same

way as we would expect it.



88

00:04:10.09 --> 00:04:13.06

Its an array, because it

was a list, that array has



89

00:04:13.06 --> 00:04:14.04

an object in it,



90

00:04:14.04 --> 00:04:16.06

and that object matches what we've saved.



91

00:04:16.06 --> 00:04:19.02

So, that's really the JSON-B binding,



92

00:04:19.02 --> 00:04:21.06

and that's how it works

in the Spring Framework.



