本文介绍如何通过CXF和Spring实现Restful Web Service,数据传输格式使用Json。
首先配置maven的库依赖,对于具体的实现可以参见
Simple JAX-RS Web Service in Java with Spring and CXF
也可以参照
Apache CXF与Spring集成实现Soap Webservice与RESTFul WebService
以下的依赖中spring、CXF、jackson是必须的,其他的根据需要增删
<properties> <java-version>1.8</java-version> <org.springframework-version>4.1.6.RELEASE</org.springframework-version> <org.hibernateframework-version>4.3.10.Final</org.hibernateframework-version> <org.cxf-version>3.1.1</org.cxf-version> <org.jackson-version>2.5.4</org.jackson-version> <org.postgresql-version>9.4-1201-jdbc41</org.postgresql-version> <org.c3p0-version>0.9.5</org.c3p0-version> </properties> <!-- cxf rest --> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-frontend-jaxrs</artifactId> <version>${org.cxf-version}</version> </dependency> <dependency> <groupId>javax.ws.rs</groupId> <artifactId>javax.ws.rs-api</artifactId> <version>2.0</version> </dependency> <!-- jackson --> <dependency> <groupId>com.fasterxml.jackson.jaxrs</groupId> <artifactId>jackson-jaxrs-json-provider</artifactId> <version>${org.jackson-version}</version> </dependency> <!-- spring --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${org.springframework-version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>${org.springframework-version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-orm</artifactId> <version>${org.springframework-version}</version> <type>jar</type> <scope>compile</scope> </dependency> <!-- hibernate --> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-core</artifactId> <version>${org.hibernateframework-version}</version> </dependency> <!-- c3p0 --> <dependency> <groupId>com.mchange</groupId> <artifactId>c3p0</artifactId> <version>${org.c3p0-version}</version> </dependency> <!-- postgresql --> <dependency> <groupId>org.postgresql</groupId> <artifactId>postgresql</artifactId> <version>${org.postgresql-version}</version> </dependency>
另外两个地方需要注意的是web.xml需要配置CXF的servlet
<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>/services/*</url-pattern> </servlet-mapping>
spring的context(或者bean)配置文件中需要加上暴露出去的server信息
<import resource="classpath:META-INF/cxf/cxf.xml" /> <import resource="classpath:META-INF/cxf/cxf-servlet.xml" /> <!-- restful --> <bean id="jsonProvider" class="com.fasterxml.jackson.jaxrs.json.JacksonJsonProvider"/> <jaxrs:server id="artistrs" address="/artistrs"> <jaxrs:serviceBeans> <ref bean="artistsServiceImpl" /> </jaxrs:serviceBeans> <jaxrs:providers> <ref bean="jsonProvider" /> </jaxrs:providers> </jaxrs:server>
此处json的处理非常平滑,并不需要额外的代码,只需要在xml中声明jsonProvider,另外在类接口中通过注解的方式声明HTTP的请求方式,数据传输格式。
@Consumes("application/json")
@Produces("application/json")
public interface ArtistService {
@POST
@Path("/getAllArtists/")
public List<Artists> getAllArtists();
}
具体的测试方式可以通过chrome的插件postman,或者直接写java(使用HttpClient)代码如下
CloseableHttpClient httpclient = HttpClients.createDefault();
HttpPost httpPost = new HttpPost("http://localhost:8080/ArticleWS/services/artistrs/getAllArtists/");
CloseableHttpResponse response2 = httpclient.execute(httpPost);
System.out.println(response2.getStatusLine());
for (Header h : response2.getAllHeaders()){
System.out.println(h.getName() + "--" + h.getValue());
}
HttpEntity entity2 = response2.getEntity();
BufferedReader br = new BufferedReader(new InputStreamReader(entity2.getContent(),"UTF-8"));
String line;
while ((line = br.readLine())!=null)
System.out.println(line);
// do something useful with the response body
// and ensure it is fully consumed
EntityUtils.consume(entity2);
response2.close();
或者通过python console
>>> import requests
>>> resp = requests.post('http://localhost:8080/ArticleWS/services/artistrs/getAllArtists/')
>>> resp.status_code
>>> resp.content.decode()
重点是URL路径<web root>/<cxf servlet url-pattern>/<jaxrs:server address>/<Interface Path>
其他相关文档