Skip to content

Commit

Permalink
WFLY-4207: updating jax-rs code
Browse files Browse the repository at this point in the history
  • Loading branch information
emmartins authored and rafabene committed Dec 30, 2014
1 parent fea7410 commit debcc86
Show file tree
Hide file tree
Showing 8 changed files with 79 additions and 74 deletions.
18 changes: 3 additions & 15 deletions helloworld-rs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ helloworld-rs: Helloworld Using JAX-RS (Java API for RESTful Web Services)
Author: Gustavo A. Brey, Gaston Coco
Level: Intermediate
Technologies: CDI, JAX-RS
Summary: Demonstrates the use of CDI 1.1 and JAX-RS
Summary: Demonstrates the use of CDI 1.1 and JAX-RS 2.0
Target Project: WildFly
Source: <https://github.com/wildfly/quickstart/>

What is it?
-----------

This example demonstrates the use of *CDI 1.1* and *JAX-RS* in *WildFly*.
This example provides a web application demonstrating basic usage of *CDI 1.1* and *JAX-RS 2.0* in *WildFly*.


System requirements
Expand Down Expand Up @@ -54,19 +54,7 @@ _NOTE: The following build command assumes you have configured your Maven user s
Access the application
---------------------

The application is deployed to <http://localhost:8080/wildfly-helloworld-rs>.

The resource is located at <http://localhost:8080/wildfly-helloworld-rs/rest/>.

To get its XML representation, you need to request the resource with the `Accept` header set to `application/xml`

$ curl http://localhost:8080/wildfly-helloworld-rs/rest/ -H 'accept:application/xml'
<xml><result>Hello World!</result></xml>

To get its JSON representation, you need to request the resource with the `Accept` header set to `application/json`

$ curl http://localhost:8080/wildfly-helloworld-rs/rest/ -H 'accept:application/json'
{"result":"Hello World!"}
The web application is deployed to <http://localhost:8080/wildfly-helloworld-rs>, and includes a root html page that may be used to request the hello world message from the JAX-RS resource, in either XML or JSON format.

Undeploy the Archive
--------------------
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,17 @@

import javax.inject.Inject;
import javax.json.Json;
import javax.json.JsonBuilderFactory;
import javax.json.JsonObject;
import javax.json.JsonObjectBuilder;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.xml.parsers.DocumentBuilder;

/**
* A simple REST service which is able to say hello to someone using HelloService Please take a look at the web.xml where JAX-RS
* is enabled
* A simple JAX-RS 2.0 REST service which is able to say hello to the world using an injected HelloService CDI bean.
* The {@link javax.ws.rs.Path} class annotation value is related to the {@link org.jboss.as.quickstarts.rshelloworld.HelloWorldApplication}'s path.
*
* @author gbrey@redhat.com
* @author Eduardo Martins
*
*/

Expand All @@ -39,17 +37,27 @@ public class HelloWorld {
@Inject
HelloService helloService;

/**
* Retrieves a JSON hello world message.
* The {@link javax.ws.rs.Path} method annotation value is related to the one defined at the class level.
* @return
*/
@GET
@Path("/")
@Path("json")
@Produces({ "application/json" })
public JsonObject getHelloWorldJSON() {
return Json.createObjectBuilder()
.add("result", helloService.createHelloMessage("World"))
.build();
}

/**
* Retrieves a XML hello world message.
* The {@link javax.ws.rs.Path} method annotation value is related to the one defined at the class level.
* @return
*/
@GET
@Path("/")
@Path("xml")
@Produces({ "application/xml" })
public String getHelloWorldXML() {
return "<xml><result>" + helloService.createHelloMessage("World") + "</result></xml>";
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* JBoss, Home of Professional Open Source
* Copyright 2014, Red Hat, Inc. and/or its affiliates, and individual
* contributors by the @authors tag. See the copyright.txt in the
* distribution for a full listing of individual contributors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.jboss.as.quickstarts.rshelloworld;

import javax.ws.rs.ApplicationPath;
import javax.ws.rs.core.Application;

/**
* A class extending {@link javax.ws.rs.core.Application} is the portable way to define JAX-RS 2.0 resources, and the {@link javax.ws.rs.ApplicationPath} defines the root path shared by all these resources.
*
* @author Eduardo Martins
*/
@ApplicationPath("rest")
public class HelloWorldApplication extends Application {
}
25 changes: 0 additions & 25 deletions helloworld-rs/src/main/webapp/WEB-INF/web.xml

This file was deleted.

10 changes: 5 additions & 5 deletions jax-rs-client/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@ jax-rs-client: External JAX-RS Client
======================
Author: Blaine Mincey
Level: Intermediate
Technologies: JAX-RS
Summary: Demonstrates the use an external JAX-RS RestEasy client which interacts with a JAX-RS Web service that uses CDI 1.1 and JAX-RS
Technologies: JAX-RS 2.0
Summary: Demonstrates the usage of JAX-RS 2.0 client, to interact with a remote JAX-RS 2.0 REST Service
Prerequisites: helloworld-rs
Target Project: WildFly
Source: <https://github.com/wildfly/quickstart/>

What is it?
-----------

This example demonstrates an external JAX-RS RestEasy client which interacts with a JAX-RS Web service that uses *CDI 1.1* and *JAX-RS*
This example demonstrates an external JAX-RS 2.0 client which interacts with a JAX-RS 2.0 Rest service
in *JBoss WildFly*.

This client "calls" the HelloWorld JAX-RS Web Service that was created in the `helloworld-rs` quickstart. See the **Prerequisite** section below for details on how to build and deploy the `helloworld-rs` quickstart.
Expand Down Expand Up @@ -59,10 +59,10 @@ Investigate the Console Output
This command will compile the example and execute a test to make two separate requests to the Web Service.
Towards the end of the Maven build output, you should see the following if the execution is successful:

URL: http://localhost:8080/wildfly-helloworld-rs/rest/
URL: http://localhost:8080/wildfly-helloworld-rs/rest/xml
MediaType: application/xml
<xml><result>Hello World!</result></xml>

URL: http://localhost:8080/wildfly-helloworld-rs/rest/
URL: http://localhost:8080/wildfly-helloworld-rs/rest/json
MediaType: application/json
{"result":"Hello World!"}
Original file line number Diff line number Diff line change
Expand Up @@ -25,20 +25,20 @@
* build and deploy helloworld-rs.
*/

import static javax.ws.rs.core.MediaType.APPLICATION_JSON_TYPE;
import static javax.ws.rs.core.MediaType.APPLICATION_XML_TYPE;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import org.jboss.resteasy.plugins.providers.jsonp.JsonObjectProvider;
import org.junit.BeforeClass;
import org.junit.Test;

import javax.json.JsonObject;
import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

import org.jboss.resteasy.plugins.providers.jsonp.JsonObjectProvider;
import org.junit.BeforeClass;
import org.junit.Test;
import static javax.ws.rs.core.MediaType.APPLICATION_JSON_TYPE;
import static javax.ws.rs.core.MediaType.APPLICATION_XML_TYPE;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

/**
* JUnit4 Test class which makes a request to the RESTful helloworld-rs web service.
Expand Down Expand Up @@ -72,7 +72,7 @@ public static void beforeClass() {
*/
@Test
public void testXML() {
Response response = getResource(resourceURL, APPLICATION_XML_TYPE);
Response response = getResource(resourceURL+"xml", APPLICATION_XML_TYPE);

String content = response.readEntity(String.class);
System.out.println(content);
Expand All @@ -83,7 +83,7 @@ public void testXML() {

@Test
public void testJSON() {
Response response = getResource(resourceURL, APPLICATION_JSON_TYPE);
Response response = getResource(resourceURL+"json", APPLICATION_JSON_TYPE);

JsonObject content = response.readEntity(JsonObject.class);
System.out.println(content);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,23 @@
/*
* JBoss, Home of Professional Open Source
* Copyright 2014, Red Hat, Inc. and/or its affiliates, and individual
* contributors by the @authors tag. See the copyright.txt in the
* distribution for a full listing of individual contributors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jboss.as.quickstarts.threadracing.stage.jaxrs;

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

/**
* A class extending {@link javax.ws.rs.core.Application} is the portable way to define JAX-RS 2.0 REST Services, and the {@link javax.ws.rs.ApplicationPath} defines the common path of such services.
Expand All @@ -15,16 +30,5 @@ public class BoxApplication extends Application {
* the jaxrs app path
*/
public static final String PATH = "box";

/**
* To define a REST Service for the app simply add its class to the set returned by this method implementation.
* @return
*/
@Override
public Set<Class<?>> getClasses() {
Set<Class<?>> resources = new java.util.HashSet<>();
resources.add(BoxService.class);
return resources;
}

}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* JBoss, Home of Professional Open Source
* Copyright 2013, Red Hat, Inc. and/or its affiliates, and individual
* Copyright 2014, Red Hat, Inc. and/or its affiliates, and individual
* contributors by the @authors tag. See the copyright.txt in the
* distribution for a full listing of individual contributors.
*
Expand Down

0 comments on commit debcc86

Please sign in to comment.