Skip to content

Commit

Permalink
Provide rest-client-<serialization> extensions
Browse files Browse the repository at this point in the history
  • Loading branch information
gsmet committed Mar 24, 2020
1 parent 919783f commit 193ebe2
Show file tree
Hide file tree
Showing 50 changed files with 1,104 additions and 119 deletions.
15 changes: 15 additions & 0 deletions bom/deployment/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,21 @@
<artifactId>quarkus-rest-client-deployment</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-rest-client-jackson-deployment</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-rest-client-jaxb-deployment</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-rest-client-jsonb-deployment</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-jaeger-deployment</artifactId>
Expand Down
15 changes: 15 additions & 0 deletions bom/runtime/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -560,6 +560,21 @@
<artifactId>quarkus-rest-client</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-rest-client-jackson</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-rest-client-jaxb</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-rest-client-jsonb</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-resteasy-common</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ public final class Capabilities extends SimpleBuildItem {
public static final String TRANSACTIONS = "io.quarkus.transactions";
public static final String JACKSON = "io.quarkus.jackson";
public static final String JSONB = "io.quarkus.jsonb";
public static final String REST_JACKSON = "io.quarkus.rest.jackson";
public static final String REST_JSONB = "io.quarkus.rest.jsonb";
public static final String RESTEASY_JSON_EXTENSION = "io.quarkus.resteasy-json";
public static final String RESTEASY = "io.quarkus.resteasy";
public static final String JWT = "io.quarkus.jwt";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@ public final class FeatureBuildItem extends MultiBuildItem {
public static final String RESTEASY_MUTINY = "resteasy-mutiny";
public static final String RESTEASY_QUTE = "resteasy-qute";
public static final String REST_CLIENT = "rest-client";
public static final String REST_CLIENT_JACKSON = "rest-client-jackson";
public static final String REST_CLIENT_JAXB = "rest-client-jaxb";
public static final String REST_CLIENT_JSONB = "rest-client-jsonb";
public static final String SCALA = "scala";
public static final String SCHEDULER = "scheduler";
public static final String SECURITY = "security";
Expand Down
3 changes: 3 additions & 0 deletions extensions/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@
<module>resteasy-mutiny</module>
<module>resteasy-qute</module>
<module>rest-client</module>
<module>rest-client-jackson</module>
<module>rest-client-jsonb</module>
<module>rest-client-jaxb</module>
<module>smallrye-openapi-common</module>
<module>smallrye-openapi</module>
<module>swagger-ui</module>
Expand Down
64 changes: 64 additions & 0 deletions extensions/rest-client-jackson/deployment/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>quarkus-rest-client-jackson-parent</artifactId>
<groupId>io.quarkus</groupId>
<version>999-SNAPSHOT</version>
<relativePath>../</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>quarkus-rest-client-jackson-deployment</artifactId>
<name>Quarkus - REST Client - Jackson - Deployment</name>

<dependencies>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-rest-client-deployment</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-jackson-deployment</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-rest-client-jackson</artifactId>
</dependency>
<!-- Do NOT include resteasy-jackson as a dependency! -->
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-resteasy-deployment</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-junit5-internal</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.rest-assured</groupId>
<artifactId>rest-assured</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<annotationProcessorPaths>
<path>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-extension-processor</artifactId>
<version>${project.version}</version>
</path>
</annotationProcessorPaths>
</configuration>
</plugin>
</plugins>
</build>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package io.quarkus.restclient.jackson.deployment;

import io.quarkus.deployment.Capabilities;
import io.quarkus.deployment.annotations.BuildProducer;
import io.quarkus.deployment.annotations.BuildStep;
import io.quarkus.deployment.builditem.CapabilityBuildItem;
import io.quarkus.deployment.builditem.FeatureBuildItem;

public class RestClientJacksonProcessor {

@BuildStep
void build(BuildProducer<FeatureBuildItem> feature,
BuildProducer<CapabilityBuildItem> capability) {
feature.produce(new FeatureBuildItem(FeatureBuildItem.REST_CLIENT_JACKSON));

capability.produce(new CapabilityBuildItem(Capabilities.REST_JACKSON));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package io.quarkus.restclient.jackson.deployment;

import java.time.Month;
import java.time.ZoneId;
import java.time.ZonedDateTime;

import javax.inject.Inject;
import javax.ws.rs.GET;
import javax.ws.rs.Path;

import org.eclipse.microprofile.rest.client.inject.RestClient;

@Path("/client")
public class ClientResource {

@Inject
@RestClient
RestInterface restInterface;

@GET
@Path("/hello")
public String hello() {
DateDto dateDto = restInterface.get();
ZonedDateTime zonedDateTime = dateDto.getDate();

if (zonedDateTime.getMonth().equals(Month.NOVEMBER)
&& zonedDateTime.getZone().equals(ZoneId.of("Europe/London"))) {
return "OK";
}

return "INVALID";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package io.quarkus.restclient.jackson.deployment;

import java.time.ZonedDateTime;

public class DateDto {

private ZonedDateTime date;

public DateDto() {
}

public DateDto(ZonedDateTime date) {
this.date = date;
}

public void setDate(ZonedDateTime date) {
this.date = date;
}

public ZonedDateTime getDate() {
return date;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package io.quarkus.restclient.jackson.deployment;

import java.time.ZoneId;
import java.time.ZonedDateTime;

import javax.inject.Inject;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

@Produces(MediaType.APPLICATION_JSON)
@Path("/hello")
public class HelloResource {

@Inject
ObjectMapper objectMapper;

@GET
public String hello() throws JsonProcessingException {
return objectMapper
.writeValueAsString(new DateDto(ZonedDateTime.of(1988, 11, 17, 0, 0, 0, 0, ZoneId.of("Europe/Paris"))));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package io.quarkus.restclient.jackson.deployment;

import static org.hamcrest.Matchers.is;

import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.spec.JavaArchive;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;

import io.quarkus.test.QuarkusUnitTest;
import io.restassured.RestAssured;

public class JacksonRestClientTest {

@RegisterExtension
static final QuarkusUnitTest config = new QuarkusUnitTest()
.withConfigurationResource("application.properties")
.setArchiveProducer(() -> ShrinkWrap.create(JavaArchive.class)
.addClasses(ZonedDateTimeObjectMapperCustomizer.class, DateDto.class, HelloResource.class,
RestInterface.class,
ClientResource.class));

@Test
public void testCustomDeserialization() {
RestAssured.get("/client/hello").then()
.body(is("OK"));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package io.quarkus.restclient.jackson.deployment;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

import org.eclipse.microprofile.rest.client.annotation.RegisterClientHeaders;
import org.eclipse.microprofile.rest.client.inject.RegisterRestClient;

@RegisterRestClient
@Path("/hello")
@RegisterClientHeaders
public interface RestInterface {

@GET
@Produces(MediaType.APPLICATION_JSON)
DateDto get();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package io.quarkus.restclient.jackson.deployment;

import java.io.IOException;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatterBuilder;

import javax.inject.Singleton;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import com.fasterxml.jackson.datatype.jsr310.ser.ZonedDateTimeSerializer;

import io.quarkus.jackson.ObjectMapperCustomizer;

@Singleton
public class ZonedDateTimeObjectMapperCustomizer implements ObjectMapperCustomizer {

@Override
public void customize(ObjectMapper objectMapper) {
JavaTimeModule customDateModule = new JavaTimeModule();
customDateModule.addSerializer(ZonedDateTime.class, new ZonedDateTimeSerializer(
new DateTimeFormatterBuilder().appendInstant(0).toFormatter().withZone(ZoneId.of("Z"))));
customDateModule.addDeserializer(ZonedDateTime.class, new ZonedDateTimeEuropeLondonDeserializer());
objectMapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS)
.registerModule(customDateModule);
}

public static class ZonedDateTimeEuropeLondonDeserializer extends JsonDeserializer<ZonedDateTime> {

@Override
public ZonedDateTime deserialize(JsonParser p, DeserializationContext ctxt)
throws IOException, JsonProcessingException {
return ZonedDateTime.parse(p.getValueAsString())
.withZoneSameInstant(ZoneId.of("Europe/London"));
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
io.quarkus.restclient.jackson.deployment.RestInterface/mp-rest/url=${test.url}
20 changes: 20 additions & 0 deletions extensions/rest-client-jackson/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>quarkus-build-parent</artifactId>
<groupId>io.quarkus</groupId>
<version>999-SNAPSHOT</version>
<relativePath>../../build-parent/pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>quarkus-rest-client-jackson-parent</artifactId>
<name>Quarkus - REST Client - Jackson</name>
<packaging>pom</packaging>
<modules>
<module>deployment</module>
<module>runtime</module>
</modules>
</project>
Loading

0 comments on commit 193ebe2

Please sign in to comment.