Skip to content

Commit 37584c4

Browse files
committed
Implement version 2.0
1 parent 9022f85 commit 37584c4

19 files changed

+925
-77
lines changed

customer-service/pom.xml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,12 +57,23 @@
5757
<artifactId>microprofile-openapi-api</artifactId>
5858
<version>1.1.2</version>
5959
</dependency>
60+
<dependency>
61+
<groupId>org.eclipse.microprofile.config</groupId>
62+
<artifactId>microprofile-config-api</artifactId>
63+
<version>1.3</version>
64+
</dependency>
6065
<dependency>
6166
<groupId>org.apache.geronimo</groupId>
6267
<artifactId>geronimo-openapi-impl</artifactId>
6368
<version>1.0.12</version>
6469
<scope>runtime</scope>
6570
</dependency>
71+
<dependency>
72+
<groupId>org.apache.geronimo.config</groupId>
73+
<artifactId>geronimo-config-impl</artifactId>
74+
<version>1.2.2</version>
75+
<scope>runtime</scope>
76+
</dependency>
6677
<dependency>
6778
<groupId>org.apache.commons</groupId>
6879
<artifactId>commons-lang3</artifactId>

customer-service/src/main/java/de/openknowledge/sample/customer/application/CustomMediaType.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
*/
2121
public final class CustomMediaType {
2222

23-
public static final String CUSTOMER_V1 = "application/vnd.de.openknowledge.sample.customer.v1+json";
23+
public static final String CUSTOMER_V1 = "application/vnd.de.openknowledge.sample.customer.v1+json2";
24+
public static final String CUSTOMER_V2 = "application/vnd.de.openknowledge.sample.customer.v2+json2";
2425

2526
}

customer-service/src/main/java/de/openknowledge/sample/customer/application/CustomerListMessageBodyWriter.java

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
import java.util.List;
2424

2525
import javax.enterprise.context.RequestScoped;
26+
import javax.enterprise.inject.Default;
27+
import javax.enterprise.util.AnnotationLiteral;
2628
import javax.ws.rs.Produces;
2729
import javax.ws.rs.WebApplicationException;
2830
import javax.ws.rs.core.Context;
@@ -35,11 +37,11 @@
3537

3638
/**
3739
* Message body reader that transforms an entity of {@link CustomerResourceType}
38-
* to media type 'application/vnd.de.openknowledge.sample.customer.v1+json'.
40+
* to media type 'application/vnd.de.openknowledge.sample.customer.v2+json'.
3941
*/
4042
@Provider
4143
@RequestScoped
42-
@Produces(CustomMediaType.CUSTOMER_V1)
44+
@Produces(CustomMediaType.CUSTOMER_V2)
4345
public class CustomerListMessageBodyWriter implements MessageBodyWriter<List<CustomerResourceType>> {
4446

4547
@Context
@@ -78,7 +80,7 @@ public void writeTo(
7880
= (MessageBodyWriter<List<CustomerResourceType>>)(MessageBodyWriter<?>)providers.getMessageBodyWriter(
7981
List.class,
8082
new GenericType<List<CustomerResourceType>>() {}.getType(),
81-
annotations,
83+
addDefaultAnnotation(annotations),
8284
MediaType.APPLICATION_JSON_TYPE);
8385
jsonWriter.writeTo(
8486
customers,
@@ -89,4 +91,11 @@ public void writeTo(
8991
httpHeaders,
9092
entityStream);
9193
}
94+
95+
private Annotation[] addDefaultAnnotation(Annotation[] annotations) {
96+
Annotation[] newAnnotations = new Annotation[annotations.length + 1];
97+
System.arraycopy(annotations, 0, newAnnotations, 1, annotations.length);
98+
newAnnotations[0] = new AnnotationLiteral<Default>() {};
99+
return newAnnotations;
100+
}
92101
}

customer-service/src/main/java/de/openknowledge/sample/customer/application/CustomerMessageBodyReader.java

Lines changed: 20 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
import java.lang.reflect.Type;
2222

2323
import javax.enterprise.context.RequestScoped;
24+
import javax.enterprise.inject.Default;
25+
import javax.enterprise.util.AnnotationLiteral;
2426
import javax.ws.rs.Consumes;
2527
import javax.ws.rs.WebApplicationException;
2628
import javax.ws.rs.core.Context;
@@ -31,12 +33,13 @@
3133
import javax.ws.rs.ext.Providers;
3234

3335
/**
34-
* Message body reader that transforms media type 'application/vnd.de.openknowledge.sample.customer.v1+json'
35-
* to an entity of {@link CustomerResourceType}.
36+
* Message body reader that transforms media type
37+
* 'application/vnd.de.openknowledge.sample.customer.v2+json' to an entity of
38+
* {@link CustomerResourceType}.
3639
*/
3740
@Provider
3841
@RequestScoped
39-
@Consumes(CustomMediaType.CUSTOMER_V1)
42+
@Consumes(CustomMediaType.CUSTOMER_V2)
4043
public class CustomerMessageBodyReader implements MessageBodyReader<CustomerResourceType> {
4144

4245
@Context
@@ -48,28 +51,21 @@ public boolean isReadable(Class<?> type, Type genericType, Annotation[] annotati
4851
}
4952

5053
@Override
51-
public CustomerResourceType readFrom(
52-
Class<CustomerResourceType> type,
53-
Type genericType,
54-
Annotation[] annotations,
55-
MediaType mediaType,
56-
MultivaluedMap<String, String> httpHeaders,
57-
InputStream entityStream
58-
) throws IOException, WebApplicationException {
54+
public CustomerResourceType readFrom(Class<CustomerResourceType> type, Type genericType, Annotation[] annotations,
55+
MediaType mediaType, MultivaluedMap<String, String> httpHeaders, InputStream entityStream)
56+
throws IOException, WebApplicationException {
5957

60-
MessageBodyReader<CustomerResourceType> jsonReader = providers.getMessageBodyReader(
61-
CustomerResourceType.class,
62-
CustomerResourceType.class,
63-
annotations,
64-
MediaType.APPLICATION_JSON_TYPE);
58+
MessageBodyReader<CustomerResourceType> jsonReader = providers.getMessageBodyReader(CustomerResourceType.class,
59+
CustomerResourceType.class, addDefaultAnnotation(annotations), MediaType.APPLICATION_JSON_TYPE);
6560

66-
return jsonReader.readFrom(
67-
CustomerResourceType.class,
68-
CustomerResourceType.class,
69-
annotations,
70-
MediaType.APPLICATION_JSON_TYPE,
71-
httpHeaders,
72-
entityStream
73-
);
61+
return jsonReader.readFrom(CustomerResourceType.class, CustomerResourceType.class, annotations,
62+
MediaType.APPLICATION_JSON_TYPE, httpHeaders, entityStream);
63+
}
64+
65+
private Annotation[] addDefaultAnnotation(Annotation[] annotations) {
66+
Annotation[] newAnnotations = new Annotation[annotations.length + 1];
67+
System.arraycopy(annotations, 0, newAnnotations, 1, annotations.length);
68+
newAnnotations[0] = new AnnotationLiteral<Default>() {};
69+
return newAnnotations;
7470
}
7571
}

customer-service/src/main/java/de/openknowledge/sample/customer/application/CustomerMessageBodyWriter.java

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
import java.lang.reflect.Type;
2222

2323
import javax.enterprise.context.RequestScoped;
24+
import javax.enterprise.inject.Default;
25+
import javax.enterprise.util.AnnotationLiteral;
2426
import javax.ws.rs.Produces;
2527
import javax.ws.rs.WebApplicationException;
2628
import javax.ws.rs.core.Context;
@@ -32,11 +34,11 @@
3234

3335
/**
3436
* Message body reader that transforms an entity of {@link CustomerResourceType}
35-
* to media type 'application/vnd.de.openknowledge.sample.customer.v1+json'.
37+
* to media type 'application/vnd.de.openknowledge.sample.customer.v2+json'.
3638
*/
3739
@Provider
3840
@RequestScoped
39-
@Produces(CustomMediaType.CUSTOMER_V1)
41+
@Produces(CustomMediaType.CUSTOMER_V2)
4042
public class CustomerMessageBodyWriter implements MessageBodyWriter<CustomerResourceType> {
4143

4244
@Context
@@ -71,7 +73,7 @@ public void writeTo(
7173
= providers.getMessageBodyWriter(
7274
CustomerResourceType.class,
7375
CustomerResourceType.class,
74-
annotations,
76+
addDefaultAnnotation(annotations),
7577
MediaType.APPLICATION_JSON_TYPE);
7678

7779
jsonWriter.writeTo(
@@ -83,4 +85,11 @@ public void writeTo(
8385
httpHeaders,
8486
entityStream);
8587
}
88+
89+
private Annotation[] addDefaultAnnotation(Annotation[] annotations) {
90+
Annotation[] newAnnotations = new Annotation[annotations.length + 1];
91+
System.arraycopy(annotations, 0, newAnnotations, 1, annotations.length);
92+
newAnnotations[0] = new AnnotationLiteral<Default>() {};
93+
return newAnnotations;
94+
}
8695
}

customer-service/src/main/java/de/openknowledge/sample/customer/application/CustomerResource.java

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@
4444
import de.openknowledge.sample.customer.domain.Customer;
4545
import de.openknowledge.sample.customer.domain.CustomerNotFoundException;
4646
import de.openknowledge.sample.customer.domain.CustomerRepository;
47-
import de.openknowledge.sample.customer.domain.Name;
4847

4948
/**
5049
* A resource that provides access to the {@link Customer} entity.
@@ -58,16 +57,13 @@ public class CustomerResource {
5857
private CustomerRepository repository;
5958

6059
@POST
61-
@Consumes({ MediaType.APPLICATION_JSON, CustomMediaType.CUSTOMER_V1 })
62-
@RequestBody(name = "Customer", content = {
63-
@Content(mediaType = MediaType.APPLICATION_JSON, schema = @Schema(implementation = CustomerResourceType.class)),
64-
@Content(mediaType = CustomMediaType.CUSTOMER_V1, schema = @Schema(implementation = CustomerResourceType.class)),
65-
})
60+
@Consumes({MediaType.APPLICATION_JSON, CustomMediaType.CUSTOMER_V1, CustomMediaType.CUSTOMER_V2})
61+
@RequestBody(name = "Customer", content = @Content(mediaType = CustomMediaType.CUSTOMER_V2, schema = @Schema(implementation = CustomerResourceType.class)))
6662
public Response createCustomer(CustomerResourceType customer, @Context UriInfo uriInfo) {
6763
LOG.log(Level.INFO, "Create customer {0}", customer);
6864

6965
Customer newCustomer = new Customer();
70-
newCustomer.setName(new Name(customer.getFirstName(), customer.getLastName()));
66+
newCustomer.setName(customer.getName());
7167
newCustomer.setEmailAddress(customer.getEmailAddress());
7268
newCustomer.setGender(customer.getGender());
7369

@@ -99,7 +95,7 @@ public Response deleteCustomer(@PathParam("id") Long customerId) {
9995

10096
@GET
10197
@Path("/{id}")
102-
@Produces({ MediaType.APPLICATION_JSON, CustomMediaType.CUSTOMER_V1 })
98+
@Produces({MediaType.APPLICATION_JSON, CustomMediaType.CUSTOMER_V1, CustomMediaType.CUSTOMER_V2})
10399
public CustomerResourceType getCustomer(@PathParam("id") Long customerId) {
104100
LOG.log(Level.INFO, "Find customer with id {0}", customerId);
105101

@@ -117,7 +113,7 @@ public CustomerResourceType getCustomer(@PathParam("id") Long customerId) {
117113
}
118114

119115
@GET
120-
@Produces({ MediaType.APPLICATION_JSON, CustomMediaType.CUSTOMER_V1 })
116+
@Produces({MediaType.APPLICATION_JSON, CustomMediaType.CUSTOMER_V1, CustomMediaType.CUSTOMER_V2})
121117
public List<CustomerResourceType> getCustomers() {
122118
LOG.info("Find all customers");
123119

@@ -131,18 +127,15 @@ public List<CustomerResourceType> getCustomers() {
131127

132128
@PUT
133129
@Path("/{id}")
134-
@Consumes({ MediaType.APPLICATION_JSON, CustomMediaType.CUSTOMER_V1 })
135-
@RequestBody(name = "Customer", content = {
136-
@Content(mediaType = MediaType.APPLICATION_JSON, schema = @Schema(implementation = CustomerResourceType.class)),
137-
@Content(mediaType = CustomMediaType.CUSTOMER_V1, schema = @Schema(implementation = CustomerResourceType.class)),
138-
})
130+
@Consumes({MediaType.APPLICATION_JSON, CustomMediaType.CUSTOMER_V1, CustomMediaType.CUSTOMER_V2})
131+
@RequestBody(name = "Customer", content = @Content(mediaType = CustomMediaType.CUSTOMER_V2, schema = @Schema(implementation = CustomerResourceType.class)))
139132
public Response updateCustomer(@PathParam("id") Long customerId, CustomerResourceType modifiedCustomer) {
140133
LOG.log(Level.INFO, "Update customer with id {0}", customerId);
141134

142135
try {
143136
Customer foundCustomer = repository.find(customerId);
144137

145-
foundCustomer.setName(new Name(modifiedCustomer.getFirstName(), modifiedCustomer.getLastName()));
138+
foundCustomer.setName(modifiedCustomer.getName());
146139
foundCustomer.setEmailAddress(modifiedCustomer.getEmailAddress());
147140
foundCustomer.setGender(modifiedCustomer.getGender());
148141

customer-service/src/main/java/de/openknowledge/sample/customer/application/CustomerResourceType.java

Lines changed: 10 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@
1515
*/
1616
package de.openknowledge.sample.customer.application;
1717

18-
import java.util.Optional;
19-
2018
import org.eclipse.microprofile.openapi.annotations.media.Schema;
2119

2220
import de.openknowledge.sample.customer.domain.Customer;
@@ -32,6 +30,7 @@ public class CustomerResourceType {
3230
@Schema(readOnly = true)
3331
private Long id;
3432

33+
@Schema
3534
private Name name;
3635

3736
private String emailAddress;
@@ -48,40 +47,23 @@ public CustomerResourceType(Customer customer) {
4847
this.gender = customer.getGender();
4948
}
5049

50+
public CustomerResourceType(Long id, Name name, String emailAddress, Gender gender) {
51+
this.id = id;
52+
this.name = name;
53+
this.emailAddress = emailAddress;
54+
this.gender = gender;
55+
}
56+
5157
public Long getId() {
5258
return id;
5359
}
5460

5561
public Name getName() {
56-
return name;
62+
return name;
5763
}
5864

5965
public void setName(Name name) {
60-
this.name = name;
61-
}
62-
63-
@Schema(deprecated = true)
64-
public String getFirstName() {
65-
return Optional.ofNullable(name).map(Name::getFirstName).orElse(null);
66-
}
67-
68-
public void setFirstName(String firstName) {
69-
if (name == null) {
70-
name = new Name();
71-
}
72-
name.setFirstName(firstName);
73-
}
74-
75-
@Schema(deprecated = true)
76-
public String getLastName() {
77-
return Optional.ofNullable(name).map(Name::getLastName).orElse(null);
78-
}
79-
80-
public void setLastName(String lastName) {
81-
if (name == null) {
82-
name = new Name();
83-
}
84-
name.setLastName(lastName);
66+
this.name = name;
8567
}
8668

8769
public String getEmailAddress() {

customer-service/src/main/java/de/openknowledge/sample/customer/application/JaxRsActivator.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
license = @License(
4141
name = "Apache License, Version 2.0",
4242
url = "http://www.apache.org/licenses/LICENSE-2.0"),
43-
version = "1",
43+
version = "2",
4444
description = "A customer service"),
4545
components = @Components(requestBodies = @RequestBody(name = "Customer", content = @Content(schema = @Schema(implementation = CustomerResourceType.class)))))
4646
public class JaxRsActivator extends Application {

0 commit comments

Comments
 (0)