From 6d1dbffbda38f59c8593c924185ec26006ed107e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Ko=C5=BEusznik?= <jan@kozusznik.cz> Date: Thu, 29 Apr 2021 06:10:02 +0200 Subject: [PATCH] RESTfull Webservice client. --- pom.xml | 7 +++++- .../lab12/client/CourseResourceClient.java | 24 +++++++++++++++++++ .../java2/lab12/client/TableViewSample.java | 11 +++++++-- src/main/resources/application.yaml | 2 +- 4 files changed, 40 insertions(+), 4 deletions(-) create mode 100644 src/main/java/java2/lab12/client/CourseResourceClient.java diff --git a/pom.xml b/pom.xml index d186d92..b3cfb86 100644 --- a/pom.xml +++ b/pom.xml @@ -104,7 +104,12 @@ <artifactId>log4j-core</artifactId> <version>${log4j.version}</version> </dependency> - + <!-- https://mvnrepository.com/artifact/org.apache.cxf/cxf-rt-rs-client --> + <dependency> + <groupId>org.apache.cxf</groupId> + <artifactId>cxf-rt-rs-client</artifactId> + <version>3.4.3</version> + </dependency> </dependencies> <build> <plugins> diff --git a/src/main/java/java2/lab12/client/CourseResourceClient.java b/src/main/java/java2/lab12/client/CourseResourceClient.java new file mode 100644 index 0000000..f0e1e08 --- /dev/null +++ b/src/main/java/java2/lab12/client/CourseResourceClient.java @@ -0,0 +1,24 @@ +package java2.lab12.client; + +import java.util.Collection; + +import javax.ws.rs.Consumes; +import javax.ws.rs.GET; +import javax.ws.rs.POST; +import javax.ws.rs.Path; +import javax.ws.rs.Produces; +import javax.ws.rs.core.MediaType; + +@Path("/") +public interface CourseResourceClient { + + @GET + @Path("courses") + @Produces(MediaType.APPLICATION_JSON) + Collection<Course> allCourses(); + + @POST + @Path("course") + @Consumes(MediaType.APPLICATION_JSON) + Long createCourse(Course course); +} diff --git a/src/main/java/java2/lab12/client/TableViewSample.java b/src/main/java/java2/lab12/client/TableViewSample.java index cbb1718..10d266f 100644 --- a/src/main/java/java2/lab12/client/TableViewSample.java +++ b/src/main/java/java2/lab12/client/TableViewSample.java @@ -190,11 +190,12 @@ public class TableViewSample extends Application { } private Collection<Course> getCourses() { - return Collections.emptyList(); + return getClient().allCourses(); } private void createCourse(Course course) { - // TODO + long id = getClient().createCourse(course); + course.setId(id); } private void updateCourse(Course course) { @@ -204,4 +205,10 @@ public class TableViewSample extends Application { private void removeCourse(Course course) { // TODO } + + private CourseResourceClient getClient() { + return JAXRSClientFactory.create("http://localhost:8080", + CourseResourceClient.class, Collections.singletonList( + JacksonJaxbJsonProvider.class)); + } } \ No newline at end of file diff --git a/src/main/resources/application.yaml b/src/main/resources/application.yaml index 76fb788..fa09f6b 100644 --- a/src/main/resources/application.yaml +++ b/src/main/resources/application.yaml @@ -10,4 +10,4 @@ quarkus: log: sql: true database: - generation: drop-and-create \ No newline at end of file + generation: update \ No newline at end of file -- GitLab