能够通过简单的html get方式访问并传值就可以了
接口用@path定义,一经采纳立马给悬赏,可追加!急,在线等,谢谢了
试试以下例子
cxf、spring集成webservice 配置restful方式
刚刚配置完spring集成cxf使用restful方式部署webservice,整个过程感觉就是爽和简单,欢迎抛砖引玉
第一步:当然是下载jar包了
使用到的jar有以下:
1、spring jar包我就不说了,地球人都知道
2、cxf-2.6.9.jar 最新版是2.7X版本,我开始下的2.7版本,发现支持的JSR是2.0,起码需要JSR-339.api.jar,但是我下的是jsr311-api-1.1.1.jar,只能支持2.7以下版本,后来又重新下了cxf2.6.9在这里记录下避免忘记了。
3、jsr311-api-1.1.1.jar
4、geronimo-servlet_3.0_spec-1.0.jar
5、neethi-3.0.2.jar
第二步: 配置项目中web.xml
CXFService
org.apache.cxf.transport.servlet.CXFServlet
CXFService
/*
第三步:
新建appalicationcontext_cxf.xml
内容如下
<?xml version="1.0" encoding="UTF-8"?>
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jaxws="http://cxf.apache.org/jaxws"
xmlns:jaxrs="http://cxf.apache.org/jaxrs"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.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">
jaxrs:serviceBeans
/jaxrs:serviceBeans
jaxrs:extensionMappings
/jaxrs:extensionMappings
jaxrs:languageMappings
/jaxrs:languageMappings
/jaxrs:server
下面就是例子
新建Sample接口类
@Path(value = "/sample")
public interface Sample {
@GET
@Path("/bean/{id}")
@Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
public String getBean(@PathParam("id") int id);
@GET
@Path("/list/{id}")
@Produces({ "application/xml","application/json" })
public String getBean2(@PathParam("id") int id);
}
新建Sample接口的实现类
@Path(value = "/sample")
public class RESTSampleSource implements Sample {
@Override
@GET
@Path("/bean/{id}")
@Produces({ "application/xml", "application/json" })
public String getBean(@PathParam("id") int id) {
return "1";
}
@Override
@GET
@Path("/list")
@Produces({ "application/xml","application/json" })
public String getBean2(@PathParam("id") int id) {
return "{id:'123'}";
}
}
好了,启动发布
使用
http://localhost:8080/cxfWebservice/rest/sample/bean/123 默认返回xml
http://localhost:8080/cxfWebservice/rest/sample/list?id=1&_type=json 加入_type=json则可以返回json数据