BAEL-3660 - Open Liberty: code cleanup

This commit is contained in:
Anshul BANSAL 2020-01-17 16:16:23 +02:00
parent 6d517ce9fb
commit 04e64eb304
8 changed files with 15 additions and 65 deletions

View File

@ -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;

View File

@ -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();
}

View File

@ -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,11 +19,10 @@ 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);
return person.getPerson();
}

View File

@ -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);
}
}

View File

@ -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<UriNotFoundException> {
Logger LOG = Logger.getLogger(UriNotFoundException.class.getName());
@Override
public boolean handles(int status, MultivaluedMap<String, Object> headers) {
LOG.info("status = " + status);
return status == 404;
}
@Override
public UriNotFoundException toThrowable(Response response) {
return new UriNotFoundException();
}
}

View File

@ -8,7 +8,6 @@
<feature>cdi-2.0</feature>
<feature>jpa-2.2</feature>
<feature>beanValidation-2.0</feature>
<feature>mpConfig-1.3</feature>
<feature>mpRestClient-1.3</feature>
</featureManager>

View File

@ -3,14 +3,12 @@ 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 {
@ -24,12 +22,7 @@ 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);
@ -39,7 +32,8 @@ public class RestClientTest {
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);

View File

@ -1 +0,0 @@
personClient/mp-rest/uri=http://localhost:9080/api/person