JAX-RS提供了一些标注将一个资源类,一个POJOJava类,封装为Web资源。标注包括:
(1)、@Path,标注资源类或方法的相对路径
(2)、@GET,@PUT,@POST,@DELETE,标注方法是用的HTTP请求的类型
(3)、@Produces,标注返回的MIME媒体类型,( 注解标注,这个注解可以包含一组字符串,默认值是*/*,它指定REST 服务的响应结果的MIME 类型,例如:application/xml、application/json、image/jpeg 等),你 也可以同时返回多种类型,但具体生成结果时使用哪种格式取决于ContentType。CXF 默认返回的是JSON 字符串。
(4)、@PathParam,@QueryParam,@HeaderParam,@CookieParam,@MatrixParam,@FormParam,分别标注方法的参数来自于HTTP请求的不同位置,例如@PathParam来自于URL的路径,@QueryParam来自于URL的查询参数,@HeaderParam来自于HTTP请求的头信息,@CookieParam来自于HTTP请求的Cookie。
1.web.xml
- <?xml version="1.0" encoding="UTF-8"?>
- <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
- <display-name>CXF Rest Services</display-name>
- <welcome-file-list>
- <welcome-file>index.html</welcome-file>
- </welcome-file-list>
- <servlet>
- <servlet-name>CXFServlet</servlet-name>
- <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
- <load-on-startup>1</load-on-startup>
- </servlet>
- <servlet-mapping>
- <servlet-name>CXFServlet</servlet-name>
- <url-pattern>/*</url-pattern>
- </servlet-mapping>
- <!-- 设置spring容器加载配置文件的路径 -->
- <context-param>
- <param-name>contextConfigLocation</param-name>
- <param-value>classpath:applicationContext.xml</param-value>
- </context-param>
- <listener>
- <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
- </listener>
- </web-app>
2.applicationContext.xml
- <?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:aop="http://www.springframework.org/schema/aop"
- xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jdbc="http://www.springframework.org/schema/jdbc"
- xmlns:context="http://www.springframework.org/schema/context"
- xmlns:cache="http://www.springframework.org/schema/cache"
- xsi:schemaLocation="
- http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
- http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
- http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd
- http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
- http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
- http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd
- http://www.springframework.org/schema/cache
- http://www.springframework.org/schema/cache/spring-cache-3.2.xsd"
- default-lazy-init="true">
- <!-- 包扫描、注解相关 -->
- <context:component-scan base-package="com.example">
- <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
- </context:component-scan>
- <import resource="classpath:applicationContext-cxf.xml" />
- </beans>
3.applicationContext-cxf.xml
- <?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:jaxws="http://cxf.apache.org/jaxws"
- xmlns:jaxrs="http://cxf.apache.org/jaxrs"
- xmlns:http-conf="http://cxf.apache.org/transports/http/configuration"
- xsi:schemaLocation="
- 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/transports/http/configuration
- http://cxf.apache.org/schemas/configuration/http-conf.xsd
- http://cxf.apache.org/jaxws
- http://cxf.apache.org/schemas/jaxws.xsd">
- <import resource="classpath:META-INF/cxf/cxf.xml" />
- <import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" />
- <import resource="classpath:META-INF/cxf/cxf-servlet.xml" />
- <http-conf:conduit
- name="*.http-conduit">
- <http-conf:client AllowChunking="false"/>
- </http-conf:conduit>
- <bean id="jsonProvider" class="org.apache.cxf.jaxrs.provider.json.JSONProvider">
- <property name="dropRootElement" value="true"/>
- <property name="dropCollectionWrapperElement" value="true"/>
- </bean>
- <jaxrs:server id="serviceContainer" address="/rsapi">
- <jaxrs:serviceBeans>
- <ref bean="testService" />
- </jaxrs:serviceBeans>
- <jaxrs:providers>
- <ref bean="jsonProvider" />
- </jaxrs:providers>
- <jaxrs:languageMappings>
- <entry key="en" value="en-gb" />
- </jaxrs:languageMappings>
- </jaxrs:server>
- </beans>
4.CXFDemoImpl.java
- @Service("testService")
- public class CXFDemoImpl implements CXFDemo {
- @Override
- public String sayHello(final String foo) {
- return "hello " + foo;
- }
- @Override
- public String sayHi() {
- return "Hi";
- }
- @Override
- public TestObject getObject() {
- final TestObject obj = new TestObject();
- obj.setId(12);
- obj.setName("name1");
- obj.setDescription("this is a description.");
- final List<TestObject> fris = new ArrayList<>();
- TestObject obj1 = new TestObject();
- obj1.setId(13);
- obj1.setName("name2");
- obj1.setDescription("this is a description.");
- fris.add(obj1);
- obj1 = new TestObject();
- obj1.setId(14);
- obj1.setName("name3");
- obj1.setDescription("this is a description.");
- fris.add(obj1);
- obj.setFriends(fris);
- return obj;
- }
- }
5.CXFDemo.java
- @Path("/test/")
- public interface CXFDemo {
- @GET
- @Path("/sayHello/{foo}")
- @Produces({ MediaType.APPLICATION_JSON })
- public String sayHello(@PathParam("foo") String foo);
- @GET
- @Path("/sayhi")
- @Produces({ MediaType.APPLICATION_JSON })
- public String sayHi();
- @GET
- @Path("/getobject")
- @Produces({ MediaType.APPLICATION_JSON })
- public TestObject getObject();
- }
6.TestObject.java
- @XmlRootElement(name = "test")
- public class TestObject {
- private int id;
- private String name;
- private String description;
- private List<TestObject> friends;
- public int getId() {
- return this.id;
- }
- public void setId(final int id) {
- this.id = id;
- }
- public String getName() {
- return this.name;
- }
- public void setName(final String name) {
- this.name = name;
- }
- public String getDescription() {
- return this.description;
- }
- public void setDescription(final String description) {
- this.description = description;
- }
- public List<TestObject> getFriends() {
- return this.friends;
- }
- public void setFriends(final List<TestObject> friends) {
- this.friends = friends;
- }
- }
部署到tomcat,访问:http://localhost:8080/rest_cxf/rsapi/test/getobject
输出:
{"description":"this is a description.","friends":[{"description":"this is a description.","id":13,"name"
:"name2"},{"description":"this is a description.","id":14,"name":"name3"}],"id":12,"name":"name1"}