没时间什么也不多说了。所有的话都在代码里。
?
?
?
一、第一步,首先下载cxf 开发文档
???? 官方地址:http://cxf.apache.org/download.html 去下载一个最新了。
?
二、本地解压,里面有一个lib。建一个web项目把 jar 拷进去,就开始开工了。
?
三、打开web.xml 加入服务启动置配
?
<context-param>
??? ??? <param-name>contextConfigLocation</param-name>
??? ??? <param-value>/WEB-INF/beans.xml</param-value>
??? </context-param>
??? <listener>
??? ??? <listener-class>
??? ??? ??? org.springframework.web.context.ContextLoaderListener
??? ??? </listener-class>
??? </listener>
??? <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>/rest/*</url-pattern>
??? </servlet-mapping>
?
?
四、在WEB-INF/beans.xml spring文件
?
?
<?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"
??? 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">
??? <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" />
??? <jaxrs:server id="studentServiceRs" address="/studentService">
??? ??? <jaxrs:extensionMappings>
??? ??? ??? <entry key="json" value="application/json" />
??? ??? ??? <entry key="xml" value="application/xml" />
??? ??? </jaxrs:extensionMappings>
??? ??? <jaxrs:serviceBeans>
??? ??? ??? <ref bean="studentService" />
??? ??? </jaxrs:serviceBeans>
??? ??? <jaxrs:inInterceptors>
??? ??? ??? <bean class="org.apache.cxf.interceptor.LoggingInInterceptor"></bean>
??? ??? </jaxrs:inInterceptors>
??? ??? <jaxrs:outInterceptors>
??? ??? ??? <bean class="org.apache.cxf.interceptor.LoggingOutInterceptor"></bean>
??? ??? </jaxrs:outInterceptors>
??? </jaxrs:server>
??? <bean id="studentService" class="com.student.web.service.impl.StudentServiceImpl" />
</beans>
?
?
五、创建你的dto? ResultDt.java
package com.student.web.dto;
import java.util.List;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement(name = "root")
public class ResultDto {
??? public Integer totalCount;//总记录数
??? public Integer totalPage;//总页数
??? public List<StudentDto> students;//学生列表
??? public String returncode;//反回状态码
}
?
?
?
StudentDto.java
package com.student.web.dto;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement(name="Student")
public class StudentDto {
??? private Long id;
??? private String name;
??? private Integer age;
??? private Long scroe;
??? private String address;
??? public Long getId() {
??? ??? return id;
??? }
??? public void setId(Long id) {
??? ??? this.id = id;
??? }
??? public String getName() {
??? ??? return name;
??? }
??? public void setName(String name) {
??? ??? this.name = name;
??? }
??? public Integer getAge() {
??? ??? return age;
??? }
??? public void setAge(Integer age) {
??? ??? this.age = age;
??? }
??? public Long getScroe() {
??? ??? return scroe;
??? }
??? public void setScroe(Long scroe) {
??? ??? this.scroe = scroe;
??? }
??? public String getAddress() {
??? ??? return address;
??? }
??? public void setAddress(String address) {
??? ??? this.address = address;
??? }
}
?
?
六、创建你的service 服务接口和实现类
?
IStudentService.java
?
package com.student.web.service;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import com.student.web.dto.ResultDto;
import com.student.web.dto.StudentDto;
@Path(value="/")
public interface IStudentService {
???
??? /**
??? ?* 获取一页学生列表
??? ?* @param page
??? ?* @param limit
??? ?* @return
??? ?*/
??? @GET
??? @Path(value="/getStudentPage/{page}/{limit}")
??? ResultDto getStudentPage(@PathParam("page") int page,@PathParam("limit") int limit);
???
??? /**
??? ?* 得到一个学生
??? ?* @param id
??? ?* @return
??? ?*/
??? @GET
??? @Path(value="/getStudentById/{id}")
??? ResultDto getStudentById(@PathParam("id") long id);
???
??? /**
??? ?* 删除一个学生
??? ?* @param id
??? ?* @return
??? ?*/
??? @GET
??? @Path(value="/deleteDeleteStudentById/{id}")
??? ResultDto deleteDeleteStudentById(@PathParam("id") long id);
???
??? /**
??? ?* 添加一个学生
??? ?* @param student
??? ?* @return
??? ?*/
??? @GET
??? @Path(value="/addStudent/{name}/{age}/{scroe}/{address}")
??? ResultDto addStudent(@PathParam("")StudentDto student);
}
?
?
StudentServiceImpl.java
?
package com.student.web.service.impl;
import java.util.ArrayList;
import java.util.List;
import com.student.web.dto.ResultDto;
import com.student.web.dto.StudentDto;
import com.student.web.service.IStudentService;
public class StudentServiceImpl implements IStudentService {
??? @Override
??? public ResultDto getStudentPage(int page, int limit) {
??? ??? ResultDto result = new ResultDto();
??? ???
??? ??? List<StudentDto> students=new ArrayList<StudentDto>();
??? ??? StudentDto s1=new StudentDto();
??? ??? s1.setId(1l);
??? ??? s1.setName("leiwuluan");
??? ??? s1.setAddress("北京市东城区");
??? ??? s1.setAge(22);
??? ??? s1.setScroe(100l);
??? ??? students.add(s1);
??? ???
??? ??? s1=new StudentDto();
??? ??? s1.setId(1l);
??? ??? s1.setName("leiwuluan");
??? ??? s1.setAddress("北京市东城区");
??? ??? s1.setAge(22);
??? ??? s1.setScroe(100l);
??? ??? students.add(s1);
??? ???
??? ??? s1=new StudentDto();
??? ??? s1.setId(1l);
??? ??? s1.setName("leiwuluan");
??? ??? s1.setAddress("北京市东城区");
??? ??? s1.setAge(22);
??? ??? s1.setScroe(100l);
??? ??? students.add(s1);
??? ???
??? ??? s1=new StudentDto();
??? ??? s1.setId(1l);
??? ??? s1.setName("leiwuluan");
??? ??? s1.setAddress("北京市东城区");
??? ??? s1.setAge(22);
??? ??? s1.setScroe(100l);
??? ??? students.add(s1);
??? ???
??? ??? s1=new StudentDto();
??? ??? s1.setId(1l);
??? ??? s1.setName("leiwuluan");
??? ??? s1.setAddress("北京市东城区");
??? ??? s1.setAge(22);
??? ??? s1.setScroe(100l);
??? ??? students.add(s1);
??? ???
??? ??? s1=new StudentDto();
??? ??? s1.setId(1l);
??? ??? s1.setName("leiwuluan");
??? ??? s1.setAddress("北京市东城区");
??? ??? s1.setAge(22);
??? ??? s1.setScroe(100l);
??? ??? students.add(s1);
??? ???
??? ??? result.students=students;
??? ??? result.totalCount=100;
??? ??? result.totalPage=12;
??? ??? result.returncode="success";
??? ??? return result;
??? }
??? @Override
??? public ResultDto getStudentById(long id) {
??? ??? ResultDto result = new ResultDto();
??? ??? List<StudentDto> students=new ArrayList<StudentDto>();
??? ??? StudentDto s1=new StudentDto();
??? ??? s1.setId(1l);
??? ??? s1.setName("leiwuluan");
??? ??? s1.setAddress("北京市东城区");
??? ??? s1.setAge(22);
??? ??? s1.setScroe(100l);
??? ??? students.add(s1);
??? ???
??? ??? result.students=students;
??? ??? result.returncode="success";
??? ??? return result;
??? }
??? @Override
??? public ResultDto deleteDeleteStudentById(long id) {
??? ??? ResultDto result = new ResultDto();
??? ??? result.returncode="success";
??? ??? return result;
??? }
??? @Override
??? public ResultDto addStudent(StudentDto student) {
??? ??? ResultDto result = new ResultDto();
??? ??? System.out.println("Address:"+student.getAddress()+",Name:"+student.getName()+",ID:"+student.getId());
??? ??? result.returncode="success";
??? ??? return result;
??? }
}
?
?
七、启动服务
?
用浏览器访问:http://localhost:8080/Student_RS/rest/studentService/getStudentPage/1/3.xml
?
显示结果如下:
?
?
上面只是一个简单了rest 服务。
?
如果有兴趣,或刚入门可以看看 李海峰 :《JAVA的WebService支持.pdf》
?
?
dome项目 Student_RS.rar
?
?
?
?
?
?
?
?
?
?
?
?
?