本文是记录一下我自学习cxf + spring4 做一个restful工程的例子
1,需要的jar包
commons-lang3-3.6.jar
cxf-core-3.1.14.jar
cxf-rt-frontend-jaxrs-3.1.14.jar
cxf-rt-frontend-jaxws-3.1.14.jar
cxf-rt-transports-http-3.1.14.jar
jackson-jaxrs-base-2.9.2.jar
jackson-jaxrs-json-provider-2.9.2.jar
jackson-module-jaxb-annotations-2.9.2.jar
org.apache.servicemix.specs.jsr339-api-2.0-2.8.0.jar
以上这些是cxf需要的 这些jar都是在这里下的一个一个下的
http://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-core/2.9.2
也可以去 http://cxf.apache.org/download.html
这里下cxf的包 但这里下的不包含jackson对应的依赖
然后就是加入springframework的jar
https://repo.spring.io/webapp/#/artifacts/browse/tree/General/libs-release-local/org/springframework/spring/5.0.1.RELEASE/spring-framework-5.0.1.RELEASE-dist.zip
这里你可以找到所有版本的spring
2,然后是一些配置文件的设置
我用的是3.0的servlet
web.xml
<servlet>
<servlet-name>cfx</servlet-name>
<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>cfx</servlet-name>
<url-pattern>/service/*</url-pattern>
</servlet-mapping>
需要引入一个外部的cxf配置
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/ws-servlet-context.xml</param-value>
</context-param>
这个是cxf的配置
ws-servlet-context.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:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd
http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<context:component-scan base-package="com.my.test2"/>
<import resource="classpath:META-INF/cxf/cxf.xml" />
<import resource="classpath:META-INF/cxf/cxf-servlet.xml" />
<!-- 如果代码里写了 component("wsTest1") 就不用写这行了 -->
<bean id='wsTest1' class="mytest.ws.imp.WsTest1"/>
<jaxrs:server id="ws" address="/rest" >
<jaxrs:inInterceptors>
<!-- 这里可以加个前置拦截器 -->
</jaxrs:inInterceptors>
<jaxrs:outInterceptors>
<!-- 这里可以加个后置拦截器 -->
</jaxrs:outInterceptors>
<jaxrs:serviceBeans>
<ref bean="wsTest1"/>
</jaxrs:serviceBeans>
<jaxrs:extensionMappings>
<entry key="json" value="application/json"/>
<entry key="xml" value="application/xml"/>
</jaxrs:extensionMappings>
<jaxrs:languageMappings>
</jaxrs:languageMappings>
<jaxrs:providers>
<bean class="com.fasterxml.jackson.jaxrs.json.JacksonJaxbJsonProvider"/>
</jaxrs:providers>
</jaxrs:server>
</beans>
这里要注意两个地方
<url-pattern>/service/*</url-pattern>
<jaxrs:server id="ws" address="/rest" >
访问用的url里这样用 xxx/service/rest/xxx
还有两个 import 是不用自己写的,是cxf的jar里带的
3,java文件
package com.my.test2.webservices.imp;
import java.util.HashMap;
import java.util.Map;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import org.springframework.stereotype.Component;
import com.my.test2.webservices.IWsTest1;
@Path("/ws1")
@Component("wsTest1")
public class WsTest1 implements IWsTest1 {
@Path("/m")
// 配置文件里用了jsonprovider 下面两个就不用写了
//@Consumes(MediaType.APPLICATION_JSON)
//@Produces(MediaType.APPLICATION_JSON)
@GET
public Map<String, String> getWsM1() {
Map<String, String> map = new HashMap<String, String>(){/**
*
*/
private static final long serialVersionUID = 1L;
{put("name","aaa");}};
return map;
}
}
这里有个注意点就是 前台的访问类型一定要和后台一致否则就会报错,我这里输入输出都是json
4,前台访问
这里可以看到这个是ws服务
http://localhost:8080/MyTestSpring4/service/
这个是访问类型不对的例子,因为我需要的返回类型是json
http://localhost:8080/MyTestSpring4/service/rest/ws1/m
这个是jquery的访问
$.ajax({
url:'/MyTestSpring4/service/rest/ws1/m'
,type:'GET'
,dataType:"json"
,contentType:"application/json"
,success:function(ret){
console.log(ret)
}
,error:function(ret){
}
})
好了以上就是这次学习的成果了
这里有个疑问,我们公司用的是接口,就是说java的实现类实现了一个接口,然后所有注解都是写在接口上的,但我也实验了用接口不行。直接用一个pojo就行。看来还是有些地方没有弄通啊。
上面那个问题用下面这个包
javax.inject-1.jar
然后将代码改成
@Named("wsTest1")//用这个就可以用接口了 要不然 不能用接口
public class WsTest1 implements IWsTest1 {
public Map<String, String> getWsM1() {
。。。