webservice简单入门案例
一、创建服务端
1.添加依赖
<dependencies> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-frontend-jaxrs</artifactId> <version>3.0.1</version> </dependency> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-transports-http-jetty</artifactId> <version>3.0.1</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> <version>1.7.12</version> </dependency> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-rs-client</artifactId> <version>3.0.1</version> </dependency> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-rs-extension-providers</artifactId> <version>3.0.1</version> </dependency> <dependency> <groupId>org.codehaus.jettison</groupId> <artifactId>jettison</artifactId> <version>1.3.7</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency> </dependencies>
2.创建实体类
import javax.xml.bind.annotation.XmlRootElement; @XmlRootElement(name = "Car") public class Car { private Integer id; private String carName; private Double price; }
import javax.xml.bind.annotation.XmlRootElement; @XmlRootElement(name = "User") public class User { private Integer id; private String username; private String city; }
3.创建服务接口
import javax.ws.rs.*; import java.util.List; /** * @Path 指定访问当前服务接口或接口的方法的地址 * @Produces 指定支持的响应的数据内容格式 * @POST 修饰在方法上,表示当前方法支持的请求方式.PUT/GET等其他的类似 * @Consumes 表示支持的请求的格式 */ @Path("/userService") @Produces("*/*") //支持响应所有的数据格式 public interface UserService { @POST @Path("/user") @Consumes({"application/xml", "application/json"}) //表示方法支持xml/json请求的数据 void save(User user); @PUT @Path("/user") @Consumes({"application/xml", "application/json"}) void update(User user); @GET @Path("/user") @Produces({"application/xml", "application/json"}) List<User> findAll(); @GET @Path("/user/{id}") @Consumes("application/xml") @Produces({"application/xml", "application/json"}) User findById(@PathParam("id") Integer id); @DELETE @Path("/user/{id}") @Consumes({"application/xml", "application/json"}) void delete(@PathParam("id") Integer id); }
4.创建服务接口实现
import java.util.ArrayList; import java.util.List; public class UserServiceImpl implements UserService { public void save(User user) { System.out.println("save user:" + user); } public void update(User user) { System.out.println("update user:" + user); } public List<User> findAll() { List<User> users = new ArrayList<>(); User user1 = new User(); user1.setId(1); user1.setUsername("小明"); user1.setCity("北京"); List<Car> carList = new ArrayList<>(); Car car1 = new Car(); car1.setId(101); car1.setCarName("保时捷"); car1.setPrice(1000000d); carList.add(car1); Car car2 = new Car(); car2.setId(102); car2.setCarName("宝马"); car2.setPrice(400000d); carList.add(car2); user1.setCars(carList); users.add(user1); User user2 = new User(); user2.setId(2); user2.setUsername("小丽"); user2.setCity("上海"); users.add(user2); return users; } public User findById(Integer id) { if (id == 1) { User user = new User(); user.setId(1); user.setUsername("小明"); user.setCity("北京"); return user; } return null; } public void delete(Integer id) { System.out.println("delete user id :" + id); } }
5.发布服务
import cn.itcast.service.UserServiceImpl; import org.apache.cxf.interceptor.LoggingInInterceptor; import org.apache.cxf.interceptor.LoggingOutInterceptor; import org.apache.cxf.jaxrs.JAXRSServerFactoryBean; public class WebServiceServer { /** * webservice服务端发布服务 */ public static void main(String[] args) { // 基于JAX-RS协议的服务端 工厂对象 JAXRSServerFactoryBean factoryBean = new JAXRSServerFactoryBean(); // 设置服务地址 factoryBean.setAddress("http://localhost:12345/"); // 设置服务类 factoryBean.setServiceBean(new UserServiceImpl()); // 设置日志输入、输出拦截器,可以观察日志信息 factoryBean.getInInterceptors().add(new LoggingInInterceptor()); factoryBean.getOutInterceptors().add(new LoggingOutInterceptor()); // 创建服务 factoryBean.create(); } }
二、创建客户端
1.调用服务
import org.apache.cxf.jaxrs.client.WebClient; import org.junit.Test; import javax.ws.rs.core.MediaType; import java.util.Collection; public class WebServiceClient { /** * webservice客户端调用服务 */ // 请求默认xml格式 @Test public void save() { User user = new User(); user.setId(10); user.setUsername("刘备"); //WebClient就是webservice客户端调用对象 WebClient.create("http://localhost:12345/userService/user") .post(user); } // 指定请求数据为json格式 @Test public void update() { User user = new User(); user.setUsername("关羽"); user.setId(12); user.setCity("荆州"); WebClient.create("http://localhost:12345/userService/user") .type(MediaType.APPLICATION_JSON) .put(user); } @Test public void findById() { User user = WebClient.create("http://localhost:12345/userService/user/1") .type(MediaType.APPLICATION_JSON) //指定请求数据格式 .accept(MediaType.APPLICATION_JSON)//响应数据格式 .get(User.class); System.out.println(user); } @Test public void findAll() { Collection<? extends User> users = WebClient.create("http://localhost:12345/userService/user") .getCollection(User.class); System.out.println(users); } @Test public void delete() { WebClient.create("http://localhost:12345/userService/user/1") .delete(); } }