简单使用CXF实现webserver(rs的独立发布)

简单使用cxf_rs的方式实现webserver

1创建maven project java项目

2,在maven文件中导入相关依赖

<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.创建实体类 Car

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.创建实体类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;
    }

    @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中的方法

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.编写服务器端发布项目

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.编写客户端程序

首先需要导入cxf-rt-rs-client客户端依赖

导入相关依赖

   <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!



 

转载于:https://www.cnblogs.com/johnny-ylp/p/7828075.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值