Saturday, October 5, 2019

Creating a RESTful web service using Java Microprofile

 Creating a RESTful web service using Java Microprofile - OpenLiberty guide

https://openliberty.io/guides/rest-intro.html?_ga=2.32933814.922244966.1570259159-606366657.1570259159


Install latest version of Maven  on Centos 7.6 ( sample failed with 3.0.5 default version)
https://www.tecmint.com/install-apache-maven-on-centos-7/

No implementation for org.codehaus.plexus.languages.java.jpms.LocationManager was bound.
  while locating org.apache.maven.plugin.surefire.SurefirePlugin

On Fedora 30 I get following error in Maven
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal net.wasdev.wlp.maven.plugins:liberty-maven-plugin:2.7:install-server (install-server) on project microprofile: Unable to resolve artifact: io.openliberty:openliberty-runtime:19.0.0.9: Could not transfer artifact io.openliberty:openliberty-runtime:zip:19.0.0.9 from/to central (https://repo.maven.apache.org/maven2): User-specified log class 'org.codehaus.mojo.pluginsupport.logging.DelegatingLog' cannot be found or is not useable. -> [Help 1]
Installing latest version of Maven removed error


Clone the tutorial  sample repo
https://github.com/eugenp/tutorials

Go to Microprofile sample
https://github.com/eugenp/tutorials/tree/master/microprofile

Follow the tutorial article
https://www.baeldung.com/eclipse-microprofile

Change versions to latest in pom.xml - check versions at https://mvnrepository.com/artifact/io.openliberty/openliberty-runtime/

tail pom.xml 
    <properties>
        <app.name>library</app.name>
        <package.file>${project.build.directory}/${app.name}-service.jar</package.file>
        <packaging.type>runnable</packaging.type>
        <microprofile.version>1.2</microprofile.version>
        <liberty-maven-plugin.version>2.7</liberty-maven-plugin.version>
        <openliberty-runtime.version>19.0.0.9</openliberty-runtime.version>
    </properties>

JsonObject - Java EE 7
https://docs.oracle.com/javaee/7/api/javax/json/JsonObject.html

Reader/Writer Java EE7 sample https://github.com/javaee-samples/javaee7-samples/tree/master/jaxrs/readerwriter-json

Compile and start the service
mvn package
java -jar target/library-service.jar


microprofile]$ java -jar target/library-service.jar
Extracting files to /home/centos/wlpExtract/library-service_1453971251685/wlp
Successfully extracted all product files.
Launching defaultServer (Open Liberty 19.0.0.9/wlp-1.0.32.cl190920190905-0148) on OpenJDK 64-Bit Server VM, version 1.8.0_222-b10 (en_US)
[AUDIT   ] CWWKE0001I: The server defaultServer has been launched.
[AUDIT   ] CWWKZ0058I: Monitoring dropins for applications.
[AUDIT   ] CWWKT0016I: Web application available (default_host): http://localhost:9080/
[AUDIT   ] CWWKZ0001I: Application microprofile-1.0-SNAPSHOT started in 1.746 seconds.
[AUDIT   ] CWWKF0012I: The server installed the following features: [cdi-1.2, jaxrs-2.0, jaxrsClient-2.0, jndi-1.0, json-1.0, jsonp-1.0, servlet-3.1].
[AUDIT   ] CWWKF0011I: The defaultServer server is ready to run a smarter planet. The defaultServer server started in 5.136 seconds.


Test the service



curl http://localhost:9080/library/books
[{"id":"0001-201910","isbn":"1","name":"Building Microservice With Eclipse MicroProfile","author":"baeldung","pages":"420"}] 

On Weblogic I got following error
Oct 07, 2019 10:16:16 AM org.glassfish.jersey.message.internal.WriterInterceptorExecutor$TerminalWriterInterceptor aroundWriteTo
SEVERE: MessageBodyWriter not found for media type=application/json, type=class java.util.ArrayList, genericType=class java.util.ArrayList.
<Oct 7, 2019, 10:16:16,652 AM CEST> <Error> <org.glassfish.jersey.message.internal.WriterInterceptorExecutor> <BEA-000000> <MessageBodyWriter not found for media type=application/json, type=class java.util.ArrayList, genericType=class java.util.ArrayList.>
Using this hint from Adam Bien solved the issue http://www.adam-bien.com/roller/abien/entry/jax_rs_returning_a_list
    @GET
    @Produces(MediaType.APPLICATION_JSON)
    public Response getAllBooks() {

        List<Book> books = bookManager.getAll();
        GenericEntity<List<Book>> list = new GenericEntity<List<Book>>(books) {
        };
        return Response.ok(list).build();
        
    }

No comments:

Post a Comment