пʼятниця, 22 січня 2016 р.

Create RESTful web services in java(JAX-RS) using Apache CXF

Java API for RESTful Web Services (JAX-RS), is a set if APIs to developer REST service. JAX-RS is part of the Java EE6
Apache CXF is an open source services framework. CXF helps you build and develop services using frontend programming APIs, like JAX-WS and JAX-RS
In this article we will build a service and deploy it on Tomcat




1. add Dynamic web project
2. create packege "com"
3. create in packege a class "rest"
4. add implementation in class "rest"
5. right click on class and convert to service
6. add in class implementatopn

@Path("/calc")
public class CalcREST {

        @GET
        @Path("/add/{a}/{b}")
        @Produces(MediaType.TEXT_PLAIN)
        public String addPlainText(@PathParam("a") double a, @PathParam("b") double b) {
            return (a + b) + "";
        }
       
        @GET
        @Path("/add/{a}/{b}")
        @Produces(MediaType.TEXT_XML)
        public String add(@PathParam("a") double a, @PathParam("b") double b) {
            return "<?xml version=\"1.0\"?>" + "<result>" +  (a + b) + "</result>";
        }
       
        @GET
        @Path("/sub/{a}/{b}")
        @Produces(MediaType.TEXT_PLAIN)
        public String subPlainText(@PathParam("a") double a, @PathParam("b") double b) {
            return (a - b) + "";
        }
       
        @GET
        @Path("/sub/{a}/{b}")
        @Produces(MediaType.TEXT_XML)
        public String sub(@PathParam("a") double a, @PathParam("b") double b) {
            return "<?xml version=\"1.0\"?>" + "<result>" +  (a - b) + "</result>";
        }
}

7. web.xml should be

<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemalocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>WEB-INF/cxf-beans.xml</param-value>
</context-param>
 <display-name>TestCalc</display-name>
  <servlet>
    <description>Apache CXF Endpoint</description>
    <display-name>cxf</display-name>
    <servlet-name>cxf</servlet-name>
    <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>cxf</servlet-name>
    <url-pattern>/*</url-pattern>
  </servlet-mapping>
  <session-config>
    <session-timeout>60</session-timeout>
  </session-config>
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>WEB-INF/cxf-beans.xml</param-value>
  </context-param>
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
</web-app>

8. cxf-beans.xml should be

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:cxf="http://cxf.apache.org/core" xmlns:jaxrs="http://cxf.apache.org/jaxrs"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jaxws="http://cxf.apache.org/jaxws"
xsi:schemaLocation="http://cxf.apache.org/core http://cxf.apache.org/schemas/core.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.xsd
http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd">
<import resource="classpath:META-INF/cxf/cxf.xml" />

    <bean id="CalcService" class="com.rest" />
    <jaxrs:server id="base" address="/rest">
        <jaxrs:serviceBeans>
            <ref bean="CalcService" />
        </jaxrs:serviceBeans>
    </jaxrs:server>
 
    <jaxws:endpoint address="/soap" id="addNumbers"
implementor="com.rest" />
</beans>
9. Run project: right click on project->run as ->run on server
10. the picture should be
11. type  http://localhost:8080/mary/rest/calc/add/10/10
as result should be
<?xml version="1.0"?>
<result>20.0</result>
12. also you can deploy service on Tomcat and run it
13. for rest service
http://localhost:8080/RESTCalculator/rest?_wadl

Example:
Test your REST service under: "http://localhost:8080/RESTfulWebServiceExample/rest/ConversionService/FeetToInch/2". 


Немає коментарів:

Дописати коментар