簡單使用cxf_rs的方式實現webserverhtml
1建立maven project java項目java
2,在maven文件中導入相關依賴web
<dependencies>
<!--使用 CXF 的RS開發模式 --> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-frontend-jaxrs</artifactId> <version>3.1.7</version> </dependency>
<!-- 內置jetty 的 WEB服務器--> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-transports-http-jetty</artifactId> <version>3.1.1</version> </dependency> <!-- 使用日誌 --> <dependency> <groupId>org.slf4j</groupId> <artifactId>log4j-over-slf4j</artifactId> <version>1.7.12</version> </dependency> </dependencies>
2.建立實體類 Carapache
package cn.test.cxf.domain; import javax.xml.bind.annotation.XmlRootElement; @XmlRootElement(name = "Car") //指定序列化(轉換 XML、JSON) 對象名字 public class Car { private Integer id; private String carName; private Double price; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getCarName() { return carName; } public void setCarName(String carName) { this.carName = carName; } public Double getPrice() { return price; } public void setPrice(Double price) { this.price = price; } @Override public String toString() { return "Car [id=" + id + ", carName=" + carName + ", price=" + price + "]"; } }
3.建立實體類Userjson
package cn.test.cxf.domain; import java.util.ArrayList; import java.util.List; import javax.xml.bind.annotation.XmlRootElement; @XmlRootElement(name = "User") public class User { private Integer id; private String username; private String city; private List<Car> cars = new ArrayList<Car>(); public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getCity() { return city; } public void setCity(String city) { this.city = city; } public List<Car> getCars() { return cars; } public void setCars(List<Car> cars) { this.cars = cars; } @Override public String toString() { return "User [id=" + id + ", username=" + username + ", city=" + city + ", cars=" + cars + "]"; } }
4.編寫Service服務器
package cn.test.cxf.service; import java.util.List; import javax.ws.rs.Consumes; import javax.ws.rs.DELETE; import javax.ws.rs.GET; import javax.ws.rs.POST; import javax.ws.rs.PUT; import javax.ws.rs.Path; import javax.ws.rs.PathParam; import javax.ws.rs.Produces; import cn.test.cxf.domain.User;
@Path("/userService") @Produces("*/*") public interface IUserService { @POST @Path("/user") @Consumes({ "application/xml", "application/json" }) public void saveUser(User user); @PUT @Path("/user") @Consumes({ "application/xml", "application/json" }) public void updateUser(User user); @GET @Path("/user") @Produces({ "application/xml", "application/json" }) public List<User> findAllUsers(); @GET @Path("/user/{id}") @Consumes("application/xml") @Produces({ "application/xml", "application/json" }) public User finUserById(@PathParam("id") Integer id); @DELETE @Path("/user/{id}") @Consumes("application/xml") public void deleteUser(@PathParam("id") Integer id); }
5.編寫service實現類,實現service中的方法app
package cn.test.cxf.service; import java.util.ArrayList; import java.util.List; import cn.test.cxf.domain.Car; import cn.test.cxf.domain.User; public class UserServiceImpl implements IUserService { public void saveUser(User user) { System.out.println("save user:" + user); } public void updateUser(User user) { System.out.println("update user:" + user); } public List<User> findAllUsers() { List<User> users = new ArrayList<User>(); User user1 = new User(); user1.setId(1); user1.setUsername("小明"); user1.setCity("北京"); List<Car> carList1 = new ArrayList<Car>(); Car car1 = new Car(); car1.setId(101); car1.setCarName("保時捷"); car1.setPrice(1000000d); carList1.add(car1); Car car2 = new Car(); car2.setId(102); car2.setCarName("寶馬"); car2.setPrice(400000d); carList1.add(car2); user1.setCars(carList1); users.add(user1); User user2 = new User(); user2.setId(2); user2.setUsername("小麗"); user2.setCity("上海"); users.add(user2); return users; } public User finUserById(Integer id) { if (id == 1) { User user1 = new User(); user1.setId(1); user1.setUsername("小明"); user1.setCity("北京"); return user1; } return null; } public void deleteUser(Integer id) { System.out.println("delete user id :" + id); } }
6.編寫服務器端發佈項目frontend
package cn.test.cxf.server; import org.apache.cxf.jaxrs.JAXRSServerFactoryBean; import cn.test.cxf.domain.Car; import cn.test.cxf.domain.User; import cn.test.cxf.service.IUserService; import cn.test.cxf.service.UserServiceImpl; public class Rs_server { public static void main(String[] args) { IUserService userService = new UserServiceImpl(); JAXRSServerFactoryBean jaxrsServerFactoryBean=new JAXRSServerFactoryBean(); jaxrsServerFactoryBean.setResourceClasses(User.class,Car.class); jaxrsServerFactoryBean.setServiceBean(userService); //設置訪問地址 jaxrsServerFactoryBean.setAddress("http://localhost:9996"); //發佈服務 jaxrsServerFactoryBean.create(); } }
7.編寫客戶端程序dom
首先須要導入cxf-rt-rs-client客戶端依賴maven
導入相關依賴
<dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-rs-client</artifactId> <version>3.1.7</version> </dependency>
建立客戶端程序
package cn.test.cxf.server; import java.util.Collection; import javax.ws.rs.core.MediaType; import org.apache.cxf.jaxrs.client.WebClient; import cn.test.cxf.domain.User; public class ClientTest { public static void main(String[] args) { Collection<? extends User> collection = WebClient.create("http://localhost:9996/userService/user") .accept(MediaType.APPLICATION_XML).getCollection(User.class); System.out.println(collection); //添加用戶 User user = new User(); WebClient.create("http://localhost:9996/userService/user") .type(MediaType.APPLICATION_XML).post(user); } }
測試完成輸出結果是:
[User [id=1, username=小明, city=北京, cars=[Car [id=101, carName=保時捷, price=1000000.0], Car [id=102, carName=寶馬, price=400000.0]]], User [id=2, username=小麗, city=上海, cars=[]]]
END!
:-D!