一、創建服務端
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(); } }