- Create a Java Project
- Configure to Maven project
- in pom file add JAX RS and APACHE CXF dependencies
- Create a Java class ‘CalcREST’ and type the following code.
1. pom
<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>
<groupId>RestCalcProject</groupId>
<artifactId>RestCalcProject</artifactId>
<version>0.0.1-SNAPSHOT</version>
<properties>
<cxf.version>2.2.3</cxf.version>
</properties>
<repositories>
<repository>
<id>apache-snapshots</id>
<name>Apache SNAPSHOT Repository</name>
<url>http://repository.apache.org/snapshots/</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>javax.ws.rs</groupId>
<artifactId>javax.ws.rs-api</artifactId>
<version>2.0.1</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-api</artifactId>
<version>${cxf.version}</version>
<type>jar</type>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-bundle-jaxrs</artifactId>
<version>2.2.3</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-jaxws</artifactId>
<version>${cxf.version}</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-transports-http</artifactId>
<version>${cxf.version}</version>
</dependency>
<!-- Jetty is needed if you're are not using the CXFServlet -->
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-transports-http-jetty</artifactId>
<version>${cxf.version}</version>
</dependency>
</dependencies>
<build>
<sourceDirectory>src</sourceDirectory>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
@Path("/calc")publicclassCalcREST {@GET@Path("/add/{a}/{b}")@Produces(MediaType.TEXT_PLAIN)publicString addPlainText(@PathParam("a")doublea,@PathParam("b")doubleb) {return(a + b) +"";}@GET@Path("/sub/{a}/{b}")@Produces(MediaType.TEXT_PLAIN)publicString subPlainText(@PathParam("a")doublea,@PathParam("b")doubleb) {return(a - b) +"";}@GET@Path("/add/{a}/{b}")@Produces(MediaType.TEXT_XML)publicString add(@PathParam("a")doublea,@PathParam("b")doubleb) {return"<?xml version=\"1.0\"?>"+"<result>"+ (a + b) +"</result>";}@GET@Path("/sub/{a}/{b}")@Produces(MediaType.TEXT_XML)publicString sub(@PathParam("a")doublea,@PathParam("b")doubleb) {return"<?xml version=\"1.0\"?>"+"<result>"+ (a - b) +"</result>";}}
6. Create a class “CalcRESTStartUp” and type the following code.
public class CalcRESTStartUp { public static void main(String[] args) { JAXRSServerFactoryBean sf = new JAXRSServerFactoryBean(); sf.setResourceClasses(CalcREST.class); sf.setResourceProvider(CalcREST.class, new SingletonResourceProvider(new CalcREST())); Server server = sf.create(); // destroy the server // uncomment when you want to close/destroy it // server.destroy(); }8. Run this class “CalcRESTStartup” as Java Application.
9. Our class “CalcREST” should be identified as root resource class and should be deployed.
10. To test it, open a browser and type the following URL
http://localhost:9999/calcrest/calc?_wadl
A Web Application Description Language (WADL) should be displayed.
11. To test the service we can type the following URLs in the browser
http://localhost:9999/calcrest/calc/add/20/30/
and
http://localhost:9999/calcrest/calc/sub/20/30/
12. The output on the browser will be in XML format even though we have our service produce plain text. This is because of the accept type HTTP header sent by the browser. By default the browser sends the text/html accept type and other xhtml and xml types.
13. create client
static final String ADD_PATH = "calc/add"; static final String SUB_PATH = "calc/sub"; static final String MUL_PATH = "calc/mul"; static final String DIV_PATH = "calc/div"; public static void main(String[] args) { int a = 122; int b = 34; String s = ""; WebClient plainAddClient = WebClient.create(REST_URI); plainAddClient.path(ADD_PATH).path(a + "/" + b).accept("text/plain"); s = plainAddClient.get(String.class); System.out.println(s); WebClient xmlAddClient = WebClient.create(REST_URI); xmlAddClient.path(ADD_PATH).path(a + "/" + b).accept("text/xml"); s = xmlAddClient.get(String.class); System.out.println(s); WebClient plainSubClient = WebClient.create(REST_URI); plainSubClient.path(SUB_PATH).path(a + "/" + b).accept("text/plain"); s = plainSubClient.get(String.class); System.out.println(s); WebClient xmlSubClient = WebClient.create(REST_URI); xmlSubClient.path(SUB_PATH).path(a + "/" + b).accept("text/xml"); s = xmlSubClient.get(String.class); System.out.println(s); }
Немає коментарів:
Дописати коментар