1. Establish Maven WebApp Engineering aty-rest.
2. Add spring Framework, JAX-RS interface, CXF implementation in POM file
<dependency><groupid>javax.ws.rs</groupid><artifactid>javax.ws.rs-api</artifactid ><version>2.0</version></dependency><dependency><groupid>org.springframework </groupId><artifactId>spring-web</artifactId><version>3.1.1.RELEASE</version> </dependency><dependency><groupId>org.apache.cxf</groupId><artifactId> Cxf-rt-frontend-jaxrs</artifactid><version>3.0.0</version></dependency>
3. Writing the rest interface and implementing classes
public interface inameservice{ @GET @Path ("/welcome/") @Produces (Mediatype.application_json) Public String Welcome ();} @Component ("Nameserviceimpl") public class Nameserviceimpl implements inameservice{public String Welcome () { return "{\" name\ ": 123}";} }
Start sping and CXF in 4.web.xml
<!--Spring--><context-param><param-name>contextconfiglocation</param-name>< param-value>classpath:spring.xml</param-value></context-param><listener>< Listener-class>org.springframework.web.context.contextloaderlistener</listener-class></listener ><!--CXF--><servlet><servlet-name>cxf</servlet-name><servlet-class> org.apache.cxf.transport.servlet.cxfservlet</servlet-class></servlet><servlet-mapping>< Servlet-name>cxf</servlet-name><url-pattern>/rest/*</url-pattern></servlet-mapping>
4. Configure the cxf-spring.xml and include it in the Spring.xml
<span style= "FONT-SIZE:18PX;" ><?xml version= "1.0" encoding= "UTF-8"? ><beans xmlns= "Http://www.springframework.org/schema/beans" Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance" xmlns:jaxrs= "Http://cxf.apache.org/jaxrs" xsi: Schemalocation= "http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/ Spring-beans-3.0.xsdhttp://cxf.apache.org/jaxrshttp://cxf.apache.org/schemas/jaxrs.xsd "><jaxrs:server address= "/greet" ><jaxrs:servicebeans><ref bean= "Nameserviceimpl"/></jaxrs:servicebeans> </jaxrs:server></beans></span>
5. Use Maven to package and deploy the war to Tomcat.
Everything is normal and you can access our rest services via Http://127.0.0.1:8080/aty-rest/rest/greet/welcome.
Jax-rs Development (ii): Hello World