简单使用CXF实现webserver(rs的独立发布)
生活随笔
收集整理的這篇文章主要介紹了
简单使用CXF实现webserver(rs的独立发布)
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
簡單使用cxf_rs的方式實現(xiàn)webserver
1創(chuàng)建maven project java項目
2,在maven文件中導(dǎo)入相關(guān)依賴
<dependencies><!--使用 CXF 的RS開發(fā)模式 --><dependency><groupId>org.apache.cxf</groupId><artifactId>cxf-rt-frontend-jaxrs</artifactId><version>3.1.7</version></dependency>
<!-- 內(nèi)置jetty 的 WEB服務(wù)器--><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.創(chuàng)建實體類 Car
package cn.test.cxf.domain;import javax.xml.bind.annotation.XmlRootElement;@XmlRootElement(name = "Car") //指定序列化(轉(zhuǎn)換 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;}@Overridepublic String toString() {return "Car [id=" + id + ", carName=" + carName + ", price=" + price + "]";}}3.創(chuàng)建實體類User
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;}@Overridepublic 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實現(xiàn)類,實現(xiàn)service中的方法
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.編寫服務(wù)器端發(fā)布項目
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);//設(shè)置訪問地址jaxrsServerFactoryBean.setAddress("http://localhost:9996");//發(fā)布服務(wù) jaxrsServerFactoryBean.create();} }7.編寫客戶端程序
首先需要導(dǎo)入cxf-rt-rs-client客戶端依賴
導(dǎo)入相關(guān)依賴
<dependency><groupId>org.apache.cxf</groupId><artifactId>cxf-rt-rs-client</artifactId><version>3.1.7</version></dependency>創(chuàng)建客戶端程序
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);} }測試完成輸出結(jié)果是:
[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!
?
轉(zhuǎn)載于:https://www.cnblogs.com/johnny-ylp/p/7828075.html
總結(jié)
以上是生活随笔為你收集整理的简单使用CXF实现webserver(rs的独立发布)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 潘多拉设置有线中继_避坑指南:购买无线中
- 下一篇: numpy加权平均