diff --git a/open-liberty/src/main/java/com/baeldung/openliberty/person/resource/PersonResource.java b/open-liberty/src/main/java/com/baeldung/openliberty/person/resource/PersonResource.java index a729805e13..506214229c 100644 --- a/open-liberty/src/main/java/com/baeldung/openliberty/person/resource/PersonResource.java +++ b/open-liberty/src/main/java/com/baeldung/openliberty/person/resource/PersonResource.java @@ -4,9 +4,9 @@ import java.util.Set; import javax.enterprise.context.RequestScoped; import javax.inject.Inject; -import javax.validation.Validator; -import javax.validation.ConstraintViolation; import javax.transaction.Transactional; +import javax.validation.ConstraintViolation; +import javax.validation.Validator; import javax.ws.rs.Consumes; import javax.ws.rs.GET; import javax.ws.rs.POST; diff --git a/open-liberty/src/main/java/com/baeldung/openliberty/rest/consumes/PersonClient.java b/open-liberty/src/main/java/com/baeldung/openliberty/rest/consumes/PersonClient.java index 205df8e24b..de17e0e62e 100644 --- a/open-liberty/src/main/java/com/baeldung/openliberty/rest/consumes/PersonClient.java +++ b/open-liberty/src/main/java/com/baeldung/openliberty/rest/consumes/PersonClient.java @@ -2,19 +2,16 @@ package com.baeldung.openliberty.rest.consumes; import javax.ws.rs.GET; import javax.ws.rs.Path; -import javax.ws.rs.ProcessingException; import javax.ws.rs.Produces; import javax.ws.rs.core.MediaType; -import org.eclipse.microprofile.rest.client.annotation.RegisterProvider; import org.eclipse.microprofile.rest.client.inject.RegisterRestClient; @RegisterRestClient(configKey = "personClient", baseUri = "http://localhost:9080/") -@RegisterProvider(UriNotFoundExceptionMapper.class) -@Path("/api/person/1") -public interface PersonClient extends AutoCloseable { +public interface PersonClient { @GET + @Path("/api/person") @Produces(MediaType.APPLICATION_JSON) - public String getPerson() throws UriNotFoundException, ProcessingException; + public String getPerson(); } diff --git a/open-liberty/src/main/java/com/baeldung/openliberty/rest/consumes/RestConsumer.java b/open-liberty/src/main/java/com/baeldung/openliberty/rest/consumes/RestConsumer.java index 63496fb4e4..65199c1d9c 100644 --- a/open-liberty/src/main/java/com/baeldung/openliberty/rest/consumes/RestConsumer.java +++ b/open-liberty/src/main/java/com/baeldung/openliberty/rest/consumes/RestConsumer.java @@ -2,7 +2,6 @@ package com.baeldung.openliberty.rest.consumes; import java.net.URI; -import javax.ws.rs.ProcessingException; import javax.ws.rs.client.Client; import javax.ws.rs.client.ClientBuilder; import javax.ws.rs.core.Response; @@ -20,12 +19,11 @@ public class RestConsumer { return result; } - public static String consumeWithRestBuilder(String targetUrl) throws ProcessingException, UriNotFoundException { + public static String consumeWithRestBuilder(String targetUrl) { URI target = URI.create(targetUrl);; PersonClient person = RestClientBuilder.newBuilder() - .baseUri(target) - .register(UriNotFoundExceptionMapper.class) - .build(PersonClient.class); + .baseUri(target) + .build(PersonClient.class); return person.getPerson(); } diff --git a/open-liberty/src/main/java/com/baeldung/openliberty/rest/consumes/UriNotFoundException.java b/open-liberty/src/main/java/com/baeldung/openliberty/rest/consumes/UriNotFoundException.java deleted file mode 100644 index a803b43bb4..0000000000 --- a/open-liberty/src/main/java/com/baeldung/openliberty/rest/consumes/UriNotFoundException.java +++ /dev/null @@ -1,14 +0,0 @@ -package com.baeldung.openliberty.rest.consumes; - -public class UriNotFoundException extends Exception { - - private static final long serialVersionUID = 1L; - - public UriNotFoundException() { - super(); - } - - public UriNotFoundException(String message) { - super(message); - } -} diff --git a/open-liberty/src/main/java/com/baeldung/openliberty/rest/consumes/UriNotFoundExceptionMapper.java b/open-liberty/src/main/java/com/baeldung/openliberty/rest/consumes/UriNotFoundExceptionMapper.java deleted file mode 100644 index 728c4415b3..0000000000 --- a/open-liberty/src/main/java/com/baeldung/openliberty/rest/consumes/UriNotFoundExceptionMapper.java +++ /dev/null @@ -1,23 +0,0 @@ -package com.baeldung.openliberty.rest.consumes; - -import java.util.logging.Logger; -import javax.ws.rs.core.MultivaluedMap; -import javax.ws.rs.core.Response; -import javax.ws.rs.ext.Provider; -import org.eclipse.microprofile.rest.client.ext.ResponseExceptionMapper; - -@Provider -public class UriNotFoundExceptionMapper implements ResponseExceptionMapper { - Logger LOG = Logger.getLogger(UriNotFoundException.class.getName()); - - @Override - public boolean handles(int status, MultivaluedMap headers) { - LOG.info("status = " + status); - return status == 404; - } - - @Override - public UriNotFoundException toThrowable(Response response) { - return new UriNotFoundException(); - } -} diff --git a/open-liberty/src/main/liberty/config/server.xml b/open-liberty/src/main/liberty/config/server.xml index 567d6f3e33..162f4f8261 100644 --- a/open-liberty/src/main/liberty/config/server.xml +++ b/open-liberty/src/main/liberty/config/server.xml @@ -8,7 +8,6 @@ cdi-2.0 jpa-2.2 beanValidation-2.0 - mpConfig-1.3 mpRestClient-1.3 diff --git a/open-liberty/src/test/java/com/baeldung/openliberty/RestClientTest.java b/open-liberty/src/test/java/com/baeldung/openliberty/RestClientTest.java index 9f7b4cd05d..6bc0d71d8f 100644 --- a/open-liberty/src/test/java/com/baeldung/openliberty/RestClientTest.java +++ b/open-liberty/src/test/java/com/baeldung/openliberty/RestClientTest.java @@ -3,19 +3,17 @@ package com.baeldung.openliberty; import static org.junit.Assert.assertEquals; import javax.json.bind.JsonbBuilder; -import javax.ws.rs.ProcessingException; import org.junit.BeforeClass; import org.junit.Test; import com.baeldung.openliberty.person.model.Person; import com.baeldung.openliberty.rest.consumes.RestConsumer; -import com.baeldung.openliberty.rest.consumes.UriNotFoundException; public class RestClientTest { private static String BASE_URL; - + private final String PERSON = "api/person"; @BeforeClass @@ -24,24 +22,20 @@ public class RestClientTest { } @Test - public void testSuite() throws ProcessingException, UriNotFoundException { - this.testJsonBClientBuilder(); - this.testRestClientBuilder(); - } - - public void testJsonBClientBuilder() { + public void whenConsumeWithJsonb_thenGetPerson() { String url = BASE_URL + "/" + PERSON + "/1"; String result = RestConsumer.consumeWithJsonb(url); - + Person person = JsonbBuilder.create().fromJson(result, Person.class); assert person.getId() == 1; assertEquals(person.getUsername(), "normanlewis"); assertEquals(person.getEmail(), "normanlewis@email.com"); } - - public void testRestClientBuilder() throws ProcessingException, UriNotFoundException { + + @Test + public void whenConsumeWithRestBuilder_thenGetPerson() { String result = RestConsumer.consumeWithRestBuilder(BASE_URL); - + Person person = JsonbBuilder.create().fromJson(result, Person.class); assert person.getId() == 1; assertEquals(person.getUsername(), "normanlewis"); diff --git a/open-liberty/src/webapp/META-INF/microprofile-config.properties b/open-liberty/src/webapp/META-INF/microprofile-config.properties deleted file mode 100644 index 5488d9a4f6..0000000000 --- a/open-liberty/src/webapp/META-INF/microprofile-config.properties +++ /dev/null @@ -1 +0,0 @@ -personClient/mp-rest/uri=http://localhost:9080/api/person \ No newline at end of file