Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MicroProfile REST Client v1.1 support #3844

Closed
wants to merge 18 commits into from
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
microprofile-rest-client tck and example added
Signed-off-by: Gaurav Gupta <gaurav.gupta.jc@gmail.com>
  • Loading branch information
jGauravGupta committed Jul 17, 2018
commit e89c80a845e409465de37ee50f522b56348fb94f
35 changes: 35 additions & 0 deletions examples/helloworld-microprofile-rest-client/README.MD
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
[//]: # " Copyright (c) 2015, 2018 Oracle and/or its affiliates. All rights reserved. "
[//]: # " (Portions) Copyright (c) 2018 Payara Foundation and/or its affiliates. "
[//]: # " "
[//]: # " This program and the accompanying materials are made available under the "
[//]: # " terms of the Eclipse Distribution License v. 1.0, which is available at "
[//]: # " http://www.eclipse.org/org/documents/edl-v10.php. "
[//]: # " "
[//]: # " SPDX-License-Identifier: BSD-3-Clause "

Hello World Example
===================

This example demonstrates Hello World Microprofile Rest Client example.
JAX-RS resource returns the usual text `Hello World!`
and `HelloWorldClient` rest client interface used as a means to invoke the `HelloWorldResource` service.

Contents
--------

The mapping of the URI path space is presented in the following table:

URI path | Resource class | HTTP methods | Notes
-------------------- | ------------------- | ------------ | --------------------------------------------------------
**_/helloworld_** | HelloWorldResource | GET | Returns `Hello World!`

Running the Example
-------------------

Run the example as follows:

> mvn clean compile exec:java

This deploys the example using [Grizzly](http://grizzly.java.net/) container. You can access the application at:

- <http://localhost:8080/helloworld>
85 changes: 85 additions & 0 deletions examples/helloworld-microprofile-rest-client/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--

Copyright (c) 2018 [Payara Foundation and/or its affiliates. All rights reserved.

This program and the accompanying materials are made available under the
terms of the Eclipse Public License v. 2.0, which is available at
http://www.eclipse.org/legal/epl-2.0.

This Source Code may also be made available under the following Secondary
Licenses when the conditions for such availability set forth in the
Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
version 2 with the GNU Classpath Exception, which is available at
https://www.gnu.org/software/classpath/license.html.

SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0

-->

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>org.glassfish.jersey.examples</groupId>
<artifactId>project</artifactId>
<version>2.28-SNAPSHOT</version>
</parent>

<artifactId>helloworld-microprofile-rest-client</artifactId>
<packaging>jar</packaging>
<name>jersey-examples-helloworld-microprofile-rest-client</name>

<description>Example using only the standard JAX-RS API's and the lightweight HTTP server bundled in JDK.</description>

<dependencies>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-jdk-http</artifactId>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-client</artifactId>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.ext</groupId>
<artifactId>jersey-microprofile-rest-client</artifactId>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.inject</groupId>
<artifactId>jersey-hk2</artifactId>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<configuration>
<mainClass>org.glassfish.jersey.examples.helloworld.jaxrs.App</mainClass>
</configuration>
</plugin>
</plugins>
</build>

<profiles>
<profile>
<id>release</id>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
</plugin>
</plugins>
</build>
</profile>
</profiles>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
/*
* Copyright (c) 2012, 2018 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Distribution License v. 1.0, which is available at
* http://www.eclipse.org/org/documents/edl-v10.php.
*
* SPDX-License-Identifier: BSD-3-Clause
*/

package org.glassfish.jersey.examples.helloworld.jaxrs;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.URI;

import javax.ws.rs.core.UriBuilder;
import javax.ws.rs.ext.RuntimeDelegate;

import com.sun.net.httpserver.HttpHandler;
import com.sun.net.httpserver.HttpServer;

/**
* Hello world application using only the standard JAX-RS API and lightweight HTTP server bundled in JDK.
*
* @author Martin Matula
*/
public class App {

/**
* Starts the lightweight HTTP server serving the JAX-RS application.
*
* @return new instance of the lightweight HTTP server
* @throws IOException
*/
static HttpServer startServer() throws IOException {
// create a new server listening at port 8080
final HttpServer server = HttpServer.create(new InetSocketAddress(getBaseURI().getPort()), 0);
Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() {
@Override
public void run() {
server.stop(0);
}
}));

// create a handler wrapping the JAX-RS application
HttpHandler handler = RuntimeDelegate.getInstance().createEndpoint(new JaxRsApplication(), HttpHandler.class);

// map JAX-RS handler to the server root
server.createContext(getBaseURI().getPath(), handler);

// start the server
server.start();

return server;
}

public static void main(String[] args) throws IOException, InterruptedException {
System.out.println("\"Hello World\" Jersey Example Application");

startServer();

System.out.println("Application started.\n"
+ "Try accessing " + getBaseURI() + "helloworld in the browser.\n"
+ "Hit enter to stop the application...");

Thread.currentThread().join();
}

private static int getPort(int defaultPort) {
final String port = System.getProperty("jersey.config.test.container.port");
if (null != port) {
try {
return Integer.parseInt(port);
} catch (NumberFormatException e) {
System.out.println("Value of jersey.config.test.container.port property"
+ " is not a valid positive integer [" + port + "]."
+ " Reverting to default [" + defaultPort + "].");
}
}
return defaultPort;
}

/**
* Gets base {@link URI}.
*
* @return base {@link URI}.
*/
public static URI getBaseURI() {
return UriBuilder.fromUri("http://localhost/").port(getPort(8080)).build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* Copyright (c) 2010, 2018 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Distribution License v. 1.0, which is available at
* http://www.eclipse.org/org/documents/edl-v10.php.
*
* SPDX-License-Identifier: BSD-3-Clause
*/

package org.glassfish.jersey.examples.helloworld.jaxrs;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

/**
*
* @author Jakub Podlesak (jakub.podlesak at oracle.com)
*/
@Path("helloworld")
public class HelloWorldResource {
public static final String CLICHED_MESSAGE = "Hello World!";

@GET
@Produces(MediaType.TEXT_PLAIN)
public String getHello() {
return CLICHED_MESSAGE;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* Copyright (c) 2012, 2018 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Distribution License v. 1.0, which is available at
* http://www.eclipse.org/org/documents/edl-v10.php.
*
* SPDX-License-Identifier: BSD-3-Clause
*/

package org.glassfish.jersey.examples.helloworld.jaxrs;

import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
import javax.ws.rs.core.Application;

/**
* JAX-RS Application class for this example.
*
* @author Martin Matula
*/
public class JaxRsApplication extends Application {
private final Set<Class<?>> classes;

public JaxRsApplication() {
HashSet<Class<?>> c = new HashSet<Class<?>>();
c.add(HelloWorldResource.class);
classes = Collections.unmodifiableSet(c);
}

@Override
public Set<Class<?>> getClasses() {
return classes;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* Copyright (c) 2018 Payara Foundation and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* This Source Code may also be made available under the following Secondary
* Licenses when the conditions for such availability set forth in the
* Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
* version 2 with the GNU Classpath Exception, which is available at
* https://www.gnu.org/software/classpath/license.html.
*
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
*/

package org.glassfish.jersey.examples.helloworld.jaxrs;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import org.eclipse.microprofile.rest.client.inject.RegisterRestClient;

/**
* Client interface represents the HelloWorldResource rest service
*
* @author Gaurav Gupta
*/
@Path("helloworld")
@RegisterRestClient
interface HelloWorldClient {

@GET
@Produces(MediaType.TEXT_PLAIN)
public String getHello();

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* Copyright (c) 2018 Payara Foundation and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* This Source Code may also be made available under the following Secondary
* Licenses when the conditions for such availability set forth in the
* Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
* version 2 with the GNU Classpath Exception, which is available at
* https://www.gnu.org/software/classpath/license.html.
*
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
*/
package org.glassfish.jersey.examples.helloworld.jaxrs;


import org.junit.Test;
import static org.junit.Assert.assertEquals;

import com.sun.net.httpserver.HttpServer;
import org.eclipse.microprofile.rest.client.RestClientBuilder;

/**
* Simple test to call HelloWorldResource REST API using HelloWorldClient proxy client.
*
* @author Gaurav Gupta
*/
public class HelloWorldClientTest {

@Test
public void testHelloWorld() throws Exception {
HttpServer server = App.startServer();

HelloWorldClient simpleGetApi = RestClientBuilder.newBuilder()
.baseUri(App.getBaseURI())
.build(HelloWorldClient.class);

assertEquals(HelloWorldResource.CLICHED_MESSAGE, simpleGetApi.getHello());

server.stop(0);
}
}
1 change: 1 addition & 0 deletions examples/pom.xml
Original file line number Diff line number Diff line change
@@ -80,6 +80,7 @@
<module>helloworld-weld</module>
<module>helloworld-spring-webapp</module>
<module>helloworld-spring-annotations</module>
<module>helloworld-microprofile-rest-client</module>
<module>http-patch</module>
<module>http-trace</module>
<module>https-clientserver-grizzly</module>
Loading