Sunday, September 29, 2019

Java EE 7 : JAX-RS 2.0 REST API sample on Weblogic 12.2.2.

Java EE 7 and JAX-RS 2.0

by Adam Bien
https://www.oracle.com/technetwork/articles/java/jaxrs20-1929352.html

Java EE7 samples
https://github.com/javaee-samples/javaee7-samples/tree/master/jaxrs


Building RESTful Web Services with JAX-RS

 https://docs.oracle.com/javaee/7/tutorial/jaxrs.htm

 JAX-RS API implementations - Jersey and RESTEasy

  https://www.baeldung.com/jax-rs-spec-and-implementations

Weblogic 12.2.1.2

In 12.2.x, WebLogic Server supports Jersey 2.21.x (JAX-RS 2.0 RI) by default in this release.

WebLogic 12.1.2 contains EclipseLink 2.4.2, this means that for the first time EclipseLink MOXy JSON-binding is available in WebLogic out of the box. See https://dzone.com/articles/oracle-weblogic-1212-now

https://wiki.eclipse.org/EclipseLink/Examples/MOXy 

Oracle Fusion Middleware Developing and Securing RESTful Web Services for Oracle WebLogic Server
https://docs.oracle.com/middleware/12212/wls/RESTF/intro-restful-service.htm#RESTF105


Weblogic Java EE7 samples
https://docs.oracle.com/middleware/12212/wls/INTRO/examples.htm#INTRO299

Jersey  JAX-RS implementation - 2.21 supported on Weblogic 12.2.1.2

https://eclipse-ee4j.github.io/jersey/

Sample Bookmark code
https://github.com/jersey/jersey/tree/master/examples/bookmark

JBoss RESTEasy - alternative JAX-RS implementation

RESTFul Web Services for Java - RESTEasy

https://docs.jboss.org/resteasy/docs/4.3.1.Final/userguide/html_single/index.html

RESTEasy examples

https://github.com/resteasy/resteasy-examples/tree/3.6.0.Final

Convert Java object to JSON
https://stackoverflow.com/questions/28079195/how-to-get-json-representation-of-java-objects-in-jax-rs-layer-in-java-ee-7

Sample code

https://github.com/dveselka/weblogic/tree/master/dave-basic-project/rs-api



MyApplication class

package com.dave.service;

import javax.ws.rs.ApplicationPath;
import javax.ws.rs.core.Application;
import java.util.Set;

@ApplicationPath("resources")
public class MyApplication  extends Application {

    @Override
    public Set<Class<?>> getClasses() {
        Set<Class<?>> resources = new java.util.HashSet<>();
        resources.add(MyResource.class);
        return resources;
    }

}

MyResource class


 package com.dave.service; 

import javax.ejb.EJB;
import javax.enterprise.context.RequestScoped;
import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;

@Path("persons")
@RequestScoped
public class MyResource {

    @EJB
    PersonSessionBean bean;

    @GET
    @Produces({"application/json"})
    public Person[] persons() {
        return bean.getPersons().toArray(new Person[0]);
    }

    @GET
    @Produces({"application/json"})
    @Path("{id}")
    public Person getPerson(@PathParam("id") int id) {
        if (id < bean.getPersons().size())
            return bean.getPersons().get(id);
        else
            return null;
    }
}



Call the endpoint

http://myHostName/contextPath/servletURI/resourceURI


localhost:7001/rs-api/resources/persons

JSON returned

[{"age":50,"name":"dave"},{"age":30,"name":"abc"}]


Call the endpoint with id

localhost:7001/rs-api/resources/persons/1

Call Python REST client

import requests
import os
import sys
import json
from pprint import pprint


def getPerson(hostName):

    url = 'http://' + hostName + ':7001/rs-api/resources/persons'

    print ('--------------------------------------------------------------------')

    os.environ['no_proxy'] = hostName
    os.environ['PYTHONWARNINGS'] = "ignore:Unverified HTTPS request"


    headers = {
        'Accept': 'application/json',
        'X-Requested-By': 'MyClient',
    }

    response = requests.get(url, headers=headers)

    data = response.json()

    print data


if len(sys.argv)>1:
    WEBLOGIC_HOST=sys.argv[1]
else:
    WEBLOGIC_HOST=os.environ.get('HOSTNAME')

print ("WEBLOGIC_HOST=" + WEBLOGIC_HOST)

getPerson(WEBLOGIC_HOST)


Call the Python client from CLI

 python src/test/python/rs-api-client.py
WEBLOGIC_HOST=localhost
--------------------------------------------------------------------
[{u'age': 50, u'name': u'dave'}, {u'age': 30, u'name': u'abc'}]


Debugging - wrong annotation on model classes

https://www.jetbrains.com/help/idea/run-debug-configuration-weblogic-server.html

####<Oct 2, 2019, 10:49:04,541 AM CEST> <Error> <org.glassfish.jersey.server.wadl.internal.generators.WadlGeneratorJAXBGrammarGenerator> <rdfadm> <AdminServer> <[ACTIVE] ExecuteThread: '2' for queue: 'weblogic.kernel.Default (self-tuning)'> <<anonymous>> <> <90846077-0af6-47ac-bace-a3fc2236a3d2-00000016> <1570006144541> <[severity-value: 8] [rid: 0] [partition-id: 0] [partition-name: DOMAIN] > <BEA-000000> <Failed to generate the schema for the JAX-B elements
javax.xml.bind.JAXBException: 
Exception Description: The class com.clearstream.service.Book requires a zero argument constructor or a specified factory method.  Note that non-static inner classes do not have zero argument constructors and are not supported.
 - with linked exception:
[Exception [EclipseLink-50001] (Eclipse Persistence Services - 2.6.4.v20160829-44060b6): org.eclipse.persistence.exceptions.JAXBException
Exception Description: The class com.dave.service.Book requires a zero argument constructor or a specified factory method.  Note that non-static inner classes do not have zero argument constructors and are not supported.]
    at org.glassfish.jersey.internal.Errors$1.call(Errors.java:271)
    at org.glassfish.jersey.internal.Errors$1.call(Errors.java:267)
    at org.glassfish.jersey.internal.Errors.process(Errors.java:315)
    at org.glassfish.jersey.internal.Errors.process(Errors.java:297)
    at org.glassfish.jersey.internal.Errors.process(Errors.java:267)
    at org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:317)
    at org.glassfish.jersey.server.ServerRuntime.process(ServerRuntime.java:305)
    at org.glassfish.jersey.server.ApplicationHandler.handle(ApplicationHandler.java:1154)
    at org.glassfish.jersey.servlet.WebComponent.serviceImpl(WebComponent.java:471)
    at org.glassfish.jersey.servlet.WebComponent.service(WebComponent.java:425)
    at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:383)
    at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:336)
    at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:223)
    at weblogic.servlet.internal.StubSecurityHelper$ServletServiceAction.run(StubSecurityHelper.java:286)
    at weblogic.servlet.internal.StubSecurityHelper$ServletServiceAction.run(StubSecurityHelper.java:260)
    at weblogic.servlet.internal.StubSecurityHelper.invokeServlet(StubSecurityHelper.java:137)
    at weblogic.servlet.internal.ServletStubImpl.execute(ServletStubImpl.java:350)
    at weblogic.servlet.internal.TailFilter.doFilter(TailFilter.java:25)
    at weblogic.servlet.internal.FilterChainImpl.doFilter(FilterChainImpl.java:78)
    at weblogic.servlet.internal.RequestEventsFilter.doFilter(RequestEventsFilter.java:32)
    at weblogic.servlet.internal.FilterChainImpl.doFilter(FilterChainImpl.java:78)
    at weblogic.servlet.internal.WebAppServletContext$ServletInvocationAction.wrapRun(WebAppServletContext.java:3683)
    at weblogic.servlet.internal.WebAppServletContext$ServletInvocationAction.run(WebAppServletContext.java:3649)
    at weblogic.security.acl.internal.AuthenticatedSubject.doAs(AuthenticatedSubject.java:326)
    at weblogic.security.service.SecurityManager.runAsForUserCode(SecurityManager.java:197)
    at weblogic.servlet.provider.WlsSecurityProvider.runAsForUserCode(WlsSecurityProvider.java:203)
    at weblogic.servlet.provider.WlsSubjectHandle.run(WlsSubjectHandle.java:71)
    at weblogic.servlet.internal.WebAppServletContext.doSecuredExecute(WebAppServletContext.java:2433)
    at weblogic.servlet.internal.WebAppServletContext.securedExecute(WebAppServletContext.java:2281)
    at weblogic.servlet.internal.WebAppServletContext.execute(WebAppServletContext.java:2259)
    at weblogic.servlet.internal.ServletRequestImpl.runInternal(ServletRequestImpl.java:1691)
    at weblogic.servlet.internal.ServletRequestImpl.run(ServletRequestImpl.java:1651)
    at weblogic.servlet.provider.ContainerSupportProviderImpl$WlsRequestExecutor.run(ContainerSupportProviderImpl.java:270)
    at weblogic.invocation.ComponentInvocationContextManager._runAs(ComponentInvocationContextManager.java:348)
    at weblogic.invocation.ComponentInvocationContextManager.runAs(ComponentInvocationContextManager.java:333)
    at weblogic.work.LivePartitionUtility.doRunWorkUnderContext(LivePartitionUtility.java:54)
    at weblogic.work.PartitionUtility.runWorkUnderContext(PartitionUtility.java:41)
    at weblogic.work.SelfTuningWorkManagerImpl.runWorkUnderContext(SelfTuningWorkManagerImpl.java:640)
    at weblogic.work.ExecuteThread.execute(ExecuteThread.java:406)
    at weblogic.work.ExecuteThread.run(ExecuteThread.java:346)
Caused By: Exception [EclipseLink-50001] (Eclipse Persistence Services - 2.6.4.v20160829-44060b6): org.eclipse.persistence.exceptions.JAXBException
Exception Description: The class com.dave.service.Book requires a zero argument constructor or a specified factory method.  Note that non-static inner classes do not have zero argument constructors and are not supported.
    at org.eclipse.persistence.exceptions.JAXBException.factoryMethodOrConstructorRequired(JAXBException.java:148)
    at org.eclipse.persistence.jaxb.compiler.AnnotationsProcessor.finalizeProperties(AnnotationsProcessor.java:966)
    at org.eclipse.persistence.jaxb.compiler.AnnotationsProcessor.processClassesAndProperties(AnnotationsProcessor.java:304)
    at org.eclipse.persistence.jaxb.compiler.Generator.<init>(Generator.java:158)
    at org.glassfish.jersey.internal.Errors$1.call(Errors.java:271)
    at org.glassfish.jersey.internal.Errors$1.call(Errors.java:267)
    at org.glassfish.jersey.internal.Errors.process(Errors.java:315)
    at org.glassfish.jersey.internal.Errors.process(Errors.java:297)
    at org.glassfish.jersey.internal.Errors.process(Errors.java:267)
    at org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:317)
    at org.glassfish.jersey.server.ServerRuntime.process(ServerRuntime.java:305)
    at org.glassfish.jersey.server.ApplicationHandler.handle(ApplicationHandler.java:1154)
    at org.glassfish.jersey.servlet.WebComponent.serviceImpl(WebComponent.java:471)
    at org.glassfish.jersey.servlet.WebComponent.service(WebComponent.java:425)
    at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:383)
    at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:336)
    at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:223)
    at weblogic.servlet.internal.StubSecurityHelper$ServletServiceAction.run(StubSecurityHelper.java:286)
    at weblogic.servlet.internal.StubSecurityHelper$ServletServiceAction.run(StubSecurityHelper.java:260)
    at weblogic.servlet.internal.StubSecurityHelper.invokeServlet(StubSecurityHelper.java:137)
    at weblogic.servlet.internal.ServletStubImpl.execute(ServletStubImpl.java:350)
    at weblogic.servlet.internal.TailFilter.doFilter(TailFilter.java:25)
    at weblogic.servlet.internal.FilterChainImpl.doFilter(FilterChainImpl.java:78)
    at weblogic.servlet.internal.RequestEventsFilter.doFilter(RequestEventsFilter.java:32)
    at weblogic.servlet.internal.FilterChainImpl.doFilter(FilterChainImpl.java:78)
    at weblogic.servlet.internal.WebAppServletContext$ServletInvocationAction.wrapRun(WebAppServletContext.java:3683)
    at weblogic.servlet.internal.WebAppServletContext$ServletInvocationAction.run(WebAppServletContext.java:3649)
    at weblogic.security.acl.internal.AuthenticatedSubject.doAs(AuthenticatedSubject.java:326)
    at weblogic.security.service.SecurityManager.runAsForUserCode(SecurityManager.java:197)
    at weblogic.servlet.provider.WlsSecurityProvider.runAsForUserCode(WlsSecurityProvider.java:203)
    at weblogic.servlet.provider.WlsSubjectHandle.run(WlsSubjectHandle.java:71)
    at weblogic.servlet.internal.WebAppServletContext.doSecuredExecute(WebAppServletContext.java:2433)
    at weblogic.servlet.internal.WebAppServletContext.securedExecute(WebAppServletContext.java:2281)
    at weblogic.servlet.internal.WebAppServletContext.execute(WebAppServletContext.java:2259)
    at weblogic.servlet.internal.ServletRequestImpl.runInternal(ServletRequestImpl.java:1691)
    at weblogic.servlet.internal.ServletRequestImpl.run(ServletRequestImpl.java:1651)
    at weblogic.servlet.provider.ContainerSupportProviderImpl$WlsRequestExecutor.run(ContainerSupportProviderImpl.java:270)
    at weblogic.invocation.ComponentInvocationContextManager._runAs(ComponentInvocationContextManager.java:348)
    at weblogic.invocation.ComponentInvocationContextManager.runAs(ComponentInvocationContextManager.java:333)
    at weblogic.work.LivePartitionUtility.doRunWorkUnderContext(LivePartitionUtility.java:54)
    at weblogic.work.PartitionUtility.runWorkUnderContext(PartitionUtility.java:41)
    at weblogic.work.SelfTuningWorkManagerImpl.runWorkUnderContext(SelfTuningWorkManagerImpl.java:640)
    at weblogic.work.ExecuteThread.execute(ExecuteThread.java:406)
    at weblogic.work.ExecuteThread.run(ExecuteThread.java:346)

No comments:

Post a Comment