初入WebService
- 搭建webservice需要用到的jar
-
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:cxf="http://cxf.apache.org/core" 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-2.5.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/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <jaxrs:server id="employeeServiceServer" address="/employeeservice"> <jaxrs:serviceBeans> <ref bean="employeeService" /> </jaxrs:serviceBeans> <jaxrs:providers> <bean class="org.codehaus.jackson.jaxrs.JacksonJsonProvider" /> </jaxrs:providers> </jaxrs:server> <bean id="employeeService" class="service.EmployeeService" /> </beans>
- Web.xml配置
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1"> <display-name>employee-service</display-name> <context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/applicationContext.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> </servlet> <servlet-mapping> <servlet-name>CXFServlet</servlet-name> <url-pattern>/services/*</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> <welcome-file>default.html</welcome-file> <welcome-file>default.htm</welcome-file> <welcome-file>default.jsp</welcome-file> </welcome-file-list> </web-app>
- model/employee.java
package model; import java.io.Serializable; public class Employee implements Serializable { /** * */ private static final long serialVersionUID = 5954986571237608774L; private String id; private String code; private String name; public String getId() { return id; } public void setId(String id) { this.id = id; } public String getCode() { return code; } public void setCode(String code) { this.code = code; } public String getName() { return name; } public void setName(String name) { this.name = name; } @Override public String toString() { return "Employee [code=" + code + ", id=" + id + ", name=" + name + "]"; } }
-
service/employeeservice.java
package service; import java.util.ArrayList; import java.util.List; import javax.ws.rs.Consumes; import javax.ws.rs.GET; import javax.ws.rs.Path; import javax.ws.rs.Produces; import model.Employee; public class EmployeeService { @GET @Path("/employees") @Consumes("application/json") @Produces("application/json") public List<Employee> findallEmployee(){ return initData(); } public List<Employee> initData(){ List<Employee> list = new ArrayList<Employee>(); for(int i=0;i<10;i++){ Employee emp = new Employee(); emp.setId("id"+i); emp.setName("员工"+i); emp.setCode("code"+i); list.add(emp); } return list; } public static void main(String[] args) { EmployeeService a = new EmployeeService(); for(Employee e:a.findallEmployee()){ System.out.println(e); } } }
部署项目,打开浏览器输入地址http://ip:port/项目名称/services/employeeservice/employees