Skip to content

Commit 377c81d

Browse files
author
Tomáš Kraus
committed
Used JSON-B in MicroProfile and SE declarative examples.
Signed-off-by: Tomáš Kraus <[email protected]>
1 parent fb5a969 commit 377c81d

File tree

7 files changed

+104
-111
lines changed

7 files changed

+104
-111
lines changed

examples/declarative/data/pom.xml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,8 @@
5959
</dependency>
6060
<dependency>
6161
<groupId>io.helidon.http.media</groupId>
62-
<artifactId>helidon-http-media-jsonp</artifactId>
62+
<artifactId>helidon-http-media-jsonb</artifactId>
63+
<scope>runtime</scope>
6364
</dependency>
6465
<dependency>
6566
<groupId>io.helidon.logging</groupId>
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
* Copyright (c) 2025 Oracle and/or its affiliates.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package io.helidon.examples.declarative.data;
17+
18+
/**
19+
* A pokémon DAO object.
20+
* <p>
21+
* Used to map HTTP request pet data. ID column has {@link jakarta.persistence.GenerationType#IDENTITY}
22+
* strategy for generated values, so it's not required.
23+
*
24+
* @param name the name of the pokémon
25+
* @param type the name of the pokémon type
26+
*/
27+
public record PokemonDto(String name,
28+
String type) {
29+
}

examples/declarative/data/src/main/java/io/helidon/examples/declarative/data/PokemonRepository.java

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@
1515
*/
1616
package io.helidon.examples.declarative.data;
1717

18+
import java.util.List;
1819
import java.util.Optional;
19-
import java.util.stream.Stream;
2020

2121
import io.helidon.data.Data;
2222

@@ -34,24 +34,24 @@
3434
public interface PokemonRepository extends Data.CrudRepository<Pokemon, Integer> {
3535

3636
/**
37-
* Retrieves a stream of {@link Pokemon} entities ordered by its {@code name}.
37+
* Retrieves a list of {@link Pokemon} entities ordered by its {@code name}.
3838
* <p>
39-
* Query defined by method name: return stream of {@link Pokemon} entities ordered by {@code name} property.
39+
* Query defined by method name: return list of {@link Pokemon} entities ordered by {@code name} property.
4040
*
41-
* @return a stream of {@link Pokemon} entities
41+
* @return a list of {@link Pokemon} entities
4242
*/
43-
Stream<Pokemon> streamOrderByName();
43+
List<Pokemon> listOrderByName();
4444

4545
/**
46-
* Retrieves a stream of {@link Pokemon} entities associated with a specific {@link Type} name.
46+
* Retrieves a list of {@link Pokemon} entities associated with a specific {@link Type} name.
4747
* <p>
48-
* Query defined by method name: return unordered stream of {@link Pokemon} entities with {@code type.name}
48+
* Query defined by method name: return unordered list of {@link Pokemon} entities with {@code type.name}
4949
* property matching the {@code typeName} method argument.
5050
*
5151
* @param typeName the name of the {@link Type}
52-
* @return a stream of {@link Pokemon} entities with the specified type name
52+
* @return a list of {@link Pokemon} entities with the specified type name
5353
*/
54-
Stream<Pokemon> streamByType_Name(String typeName);
54+
List<Pokemon> listByType_Name(String typeName);
5555

5656
/**
5757
* Retrieves a {@link Pokemon} entity by its name.

examples/declarative/data/src/main/java/io/helidon/examples/declarative/data/PokemonService.java

Lines changed: 13 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,15 @@
1515
*/
1616
package io.helidon.examples.declarative.data;
1717

18-
import java.util.stream.Collector;
18+
import java.util.List;
19+
import java.util.Optional;
1920

2021
import io.helidon.common.media.type.MediaTypes;
2122
import io.helidon.http.Http;
2223
import io.helidon.service.registry.Service;
2324
import io.helidon.transaction.Tx;
2425
import io.helidon.webserver.http.RestServer;
2526

26-
import jakarta.json.Json;
27-
import jakarta.json.JsonArray;
28-
import jakarta.json.JsonArrayBuilder;
29-
import jakarta.json.JsonObject;
30-
3127
@SuppressWarnings("deprecation")
3228
@Http.Path("/pokemon")
3329
@Service.Singleton
@@ -47,64 +43,35 @@ class PokemonService {
4743
@Http.GET
4844
@Http.Path("/all")
4945
@Http.Produces(MediaTypes.APPLICATION_JSON_VALUE)
50-
JsonArray all() {
51-
return pokemonRepository.streamOrderByName()
52-
.map(pokemon -> Json.createObjectBuilder()
53-
.add("id", pokemon.getId())
54-
.add("name", pokemon.getName())
55-
.add("type", pokemon.getType().getName())
56-
.build())
57-
.collect(Collector.of(Json::createArrayBuilder,
58-
JsonArrayBuilder::add,
59-
JsonArrayBuilder::add,
60-
JsonArrayBuilder::build));
46+
List<Pokemon> all() {
47+
return pokemonRepository.listOrderByName();
6148
}
6249

6350
@Http.GET
6451
@Http.Path("/type/{name}")
6552
@Http.Produces(MediaTypes.APPLICATION_JSON_VALUE)
66-
JsonArray type(@Http.PathParam("name") String name) {
67-
return pokemonRepository.streamByType_Name(name)
68-
.map(pokemon -> Json.createObjectBuilder()
69-
.add("id", pokemon.getId())
70-
.add("name", pokemon.getName())
71-
.add("type", pokemon.getType().getName())
72-
.build())
73-
.collect(Collector.of(Json::createArrayBuilder,
74-
JsonArrayBuilder::add,
75-
JsonArrayBuilder::add,
76-
JsonArrayBuilder::build));
53+
List<Pokemon> type(@Http.PathParam("name") String name) {
54+
return pokemonRepository.listByType_Name(name);
7755
}
7856

7957
@Http.GET
8058
@Http.Path("/get/{name}")
8159
@Http.Produces(MediaTypes.APPLICATION_JSON_VALUE)
82-
JsonObject pokemon(@Http.PathParam("name") String name) {
83-
return pokemonRepository.findByName(name)
84-
.map(pokemon -> Json.createObjectBuilder()
85-
.add("id", pokemon.getId())
86-
.add("name", pokemon.getName())
87-
.add("type", pokemon.getType().getName())
88-
.build())
89-
.orElse(JsonObject.EMPTY_JSON_OBJECT);
60+
Optional<Pokemon> pokemon(@Http.PathParam("name") String name) {
61+
return pokemonRepository.findByName(name);
9062
}
9163

9264
@Http.POST
9365
@Http.Consumes(MediaTypes.APPLICATION_JSON_VALUE)
9466
@Http.Produces(MediaTypes.APPLICATION_JSON_VALUE)
95-
JsonObject insert(@Http.Entity JsonObject pokemonJson) {
96-
Pokemon pokemon = insertPokemon(pokemonJson);
97-
return Json.createObjectBuilder()
98-
.add("id", pokemon.getId())
99-
.add("name", pokemon.getName())
100-
.add("type", pokemon.getType().getName())
101-
.build();
67+
Pokemon insert(@Http.Entity PokemonDto pokemonDto) {
68+
return insertPokemon(pokemonDto);
10269
}
10370

10471
@Tx.Required
105-
Pokemon insertPokemon(JsonObject pokemonJson) {
106-
Type type = typeRepository.getByName(pokemonJson.getString("type"));
107-
return pokemonRepository.insert(new Pokemon(pokemonJson.getString("name"), type));
72+
Pokemon insertPokemon(PokemonDto pokemonDto) {
73+
Type type = typeRepository.getByName(pokemonDto.type());
74+
return pokemonRepository.insert(new Pokemon(pokemonDto.name(), type));
10875
}
10976

11077
@Http.DELETE
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
* Copyright (c) 2025 Oracle and/or its affiliates.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package io.helidon.examples.microprofile.data;
17+
18+
/**
19+
* A pokémon DAO object.
20+
* <p>
21+
* Used to map HTTP request pet data. ID column has {@link jakarta.persistence.GenerationType#IDENTITY}
22+
* strategy for generated values, so it's not required.
23+
*
24+
* @param name the name of the pokémon
25+
* @param type the name of the pokémon type
26+
*/
27+
public record PokemonDto(String name,
28+
String type) {
29+
}

examples/microprofile/data/src/main/java/io/helidon/examples/microprofile/data/PokemonRepository.java

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@
1515
*/
1616
package io.helidon.examples.microprofile.data;
1717

18+
import java.util.List;
1819
import java.util.Optional;
19-
import java.util.stream.Stream;
2020

2121
import io.helidon.data.Data;
2222

@@ -34,24 +34,24 @@
3434
public interface PokemonRepository extends Data.CrudRepository<Pokemon, Integer> {
3535

3636
/**
37-
* Retrieves a stream of {@link Pokemon} entities ordered by its {@code name}.
37+
* Retrieves a list of {@link Pokemon} entities ordered by its {@code name}.
3838
* <p>
39-
* Query defined by method name: return stream of {@link Pokemon} entities ordered by {@code name} property.
39+
* Query defined by method name: return list of {@link Pokemon} entities ordered by {@code name} property.
4040
*
41-
* @return a stream of {@link Pokemon} entities
41+
* @return a list of {@link Pokemon} entities
4242
*/
43-
Stream<Pokemon> streamOrderByName();
43+
List<Pokemon> listOrderByName();
4444

4545
/**
46-
* Retrieves a stream of {@link Pokemon} entities associated with a specific {@link Type} name.
46+
* Retrieves a list of {@link Pokemon} entities associated with a specific {@link Type} name.
4747
* <p>
48-
* Query defined by method name: return unordered stream of {@link Pokemon} entities with {@code type.name}
48+
* Query defined by method name: return unordered list of {@link Pokemon} entities with {@code type.name}
4949
* property matching the {@code typeName} method argument.
5050
*
5151
* @param typeName the name of the {@link Type}
52-
* @return a stream of {@link Pokemon} entities with the specified type name
52+
* @return a list of {@link Pokemon} entities with the specified type name
5353
*/
54-
Stream<Pokemon> streamByType_Name(String typeName);
54+
List<Pokemon> listByType_Name(String typeName);
5555

5656
/**
5757
* Retrieves a {@link Pokemon} entity by its name.

examples/microprofile/data/src/main/java/io/helidon/examples/microprofile/data/PokemonService.java

Lines changed: 13 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,12 @@
1515
*/
1616
package io.helidon.examples.microprofile.data;
1717

18+
import java.util.List;
1819
import java.util.Optional;
19-
import java.util.stream.Collector;
2020

2121
import io.helidon.common.media.type.MediaTypes;
2222

2323
import jakarta.inject.Inject;
24-
import jakarta.json.Json;
25-
import jakarta.json.JsonArray;
26-
import jakarta.json.JsonArrayBuilder;
27-
import jakarta.json.JsonObject;
2824
import jakarta.transaction.Transactional;
2925
import jakarta.ws.rs.Consumes;
3026
import jakarta.ws.rs.DELETE;
@@ -69,17 +65,8 @@ public PokemonService(PokemonRepository pokemonRepository,
6965
@GET
7066
@Path("/all")
7167
@Produces(MediaTypes.APPLICATION_JSON_VALUE)
72-
public JsonArray all() {
73-
return pokemonRepository.streamOrderByName()
74-
.map(pokemon -> Json.createObjectBuilder()
75-
.add("id", pokemon.getId())
76-
.add("name", pokemon.getName())
77-
.add("type", pokemon.getType().getName())
78-
.build())
79-
.collect(Collector.of(Json::createArrayBuilder,
80-
JsonArrayBuilder::add,
81-
JsonArrayBuilder::add,
82-
JsonArrayBuilder::build));
68+
public List<Pokemon> all() {
69+
return pokemonRepository.listOrderByName();
8370
}
8471

8572
/**
@@ -92,17 +79,8 @@ public JsonArray all() {
9279
@GET
9380
@Path("/type/{name}")
9481
@Produces(MediaTypes.APPLICATION_JSON_VALUE)
95-
public JsonArray type(@PathParam("name") String name) {
96-
return pokemonRepository.streamByType_Name(name)
97-
.map(pokemon -> Json.createObjectBuilder()
98-
.add("id", pokemon.getId())
99-
.add("name", pokemon.getName())
100-
.add("type", pokemon.getType().getName())
101-
.build())
102-
.collect(Collector.of(Json::createArrayBuilder,
103-
JsonArrayBuilder::add,
104-
JsonArrayBuilder::add,
105-
JsonArrayBuilder::build));
82+
public List<Pokemon> type(@PathParam("name") String name) {
83+
return pokemonRepository.listByType_Name(name);
10684
}
10785

10886
/**
@@ -114,14 +92,8 @@ public JsonArray type(@PathParam("name") String name) {
11492
@GET
11593
@Path("/get/{name}")
11694
@Produces(MediaTypes.APPLICATION_JSON_VALUE)
117-
public JsonObject pokemon(@PathParam("name") String name) {
118-
return pokemonRepository.findByName(name)
119-
.map(pokemon -> Json.createObjectBuilder()
120-
.add("id", pokemon.getId())
121-
.add("name", pokemon.getName())
122-
.add("type", pokemon.getType().getName())
123-
.build())
124-
.orElse(JsonObject.EMPTY_JSON_OBJECT);
95+
public Optional<Pokemon> pokemon(@PathParam("name") String name) {
96+
return pokemonRepository.findByName(name);
12597
}
12698

12799
/**
@@ -130,25 +102,20 @@ public JsonObject pokemon(@PathParam("name") String name) {
130102
* Pet entity content is supplied as JSON object.
131103
* Pokémon type record must already exist. Pokémon entity must not exist in the database.
132104
*
133-
* @param pokemonJson the pokémon to insert into the database
105+
* @param pokemonDto the pokémon to insert into the database
134106
* @return the new {@link Pokemon} entity
135107
*/
136108
@POST
137109
@Consumes(MediaTypes.APPLICATION_JSON_VALUE)
138110
@Produces(MediaTypes.APPLICATION_JSON_VALUE)
139-
public JsonObject insert(JsonObject pokemonJson) {
140-
Pokemon pokemon = insertPokemon(pokemonJson);
141-
return Json.createObjectBuilder()
142-
.add("id", pokemon.getId())
143-
.add("name", pokemon.getName())
144-
.add("type", pokemon.getType().getName())
145-
.build();
111+
public Pokemon insert(PokemonDto pokemonDto) {
112+
return insertPokemon(pokemonDto);
146113
}
147114

148115
@Transactional
149-
Pokemon insertPokemon(JsonObject pokemonJson) {
150-
Type type = typeRepository.getByName(pokemonJson.getString("type"));
151-
return pokemonRepository.insert(new Pokemon(pokemonJson.getString("name"), type));
116+
Pokemon insertPokemon(PokemonDto pokemonDto) {
117+
Type type = typeRepository.getByName(pokemonDto.type());
118+
return pokemonRepository.insert(new Pokemon(pokemonDto.name(), type));
152119
}
153120

154121
/**

0 commit comments

Comments
 (0)