webservice的使用

提供方

导入maven依赖

<!-- 客户端 -->
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-rs-client</artifactId>
</dependency>
<!-- 转换json -->
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-rs-extension-providers</artifactId>
</dependency>
<dependency>
<groupId>org.codehaus.jettison</groupId>
<artifactId>jettison</artifactId>
</dependency>

在接口上

public interface CustomerService {
    /**
     * 根据地址查询客户
     */
    @GET
    @Path("/findByAddress")
    @Produces(MediaType.APPLICATION_JSON)
    public Customer findByAddress(@QueryParam("address")String address);

    /**
     * 查询为关联定区的客户
     */
    @GET
    @Path("/findByNoAssociateCustomers")
    @Produces(MediaType.APPLICATION_JSON)
    public List<Customer> findByNoAssociateCustomers();


    /**
     * 查询已经关联某个定区的客户
     */
    @GET
    @Path("/findByHasAssociateCustomers")
    @Produces(MediaType.APPLICATION_JSON)
    public List<Customer> findByHasAssociateCustomers(@QueryParam("fixedAreaId")Integer fixedAreaId);

    /**
     * 保存定义与客户关系
     */
    @PUT
    @Path("/associateCustomersToFixedArea")
    public void associateCustomersToFixedArea(@QueryParam("fixedAreaId")Integer fixedAreaId,@QueryParam("custIds")String custIds);

    /**
     * 查询手机号的客户
     */
    @GET
    @Path("/checkTelephone")
    @Produces(MediaType.APPLICATION_JSON)
    public Customer checkTelephone(@QueryParam("telephone")String telephone);

    /**
     * 保存客户信息
     */
    @POST
    @Consumes(MediaType.APPLICATION_JSON)
    public void saveCustomer(Customer customer);

    /**
     * 激活客户
     */
    @PUT
    public void activeCustomer(@QueryParam("telephone")String telephone);
}

 

实现类

@Service
@Transactional
public class CustomerServiceImpl implements CustomerService {
    @Autowired
    private CustomerDao customerDao;

    @Override
    public Customer findByAddress(String address) {
        return customerDao.findByAddress(address);
    }

    @Override
    public List<Customer> findByNoAssociateCustomers() {
        return customerDao.findByFixedAreaIdIsNull();
    }

    @Override
    public List<Customer> findByHasAssociateCustomers(Integer fixedAreaId) {
        return customerDao.findByFixedAreaId(fixedAreaId);
    }

    @Override
    public void associateCustomersToFixedArea(Integer fixedAreaId, String custIds) {
        //1.先清理绑定过的定区
        customerDao.clearCustomers(fixedAreaId);
        if (custIds != null && !custIds.trim().equals("")) {
            String[] cstIdArray = custIds.split(",");
            for (String cstId : cstIdArray) {
                customerDao.bindCustomerToFixedArea(fixedAreaId, Integer.valueOf(cstId));
            }
        }

    }

    @Override
    public Customer checkTelephone(String telephone) {
        return customerDao.findByTelephone(telephone);
    }

    @Override
    public void saveCustomer(Customer customer) {
        customerDao.save(customer);
    }

    @Override
    public void activeCustomer(String telephone) {
        customerDao.activeCustomer(telephone);
    }
}

customer的类要添加的注解

@Entity
@Table(name = "t_customer", schema = "")
@XmlRootElement
public class Customer implements java.io.Serializable {

    private Integer id;
    private String username;
    private String password;
    private String type;
    private String sex;
    private String telephone;
    private String address;
    private String email;
    private Integer fixedAreaId;

    // Constructors

    /**
     * default constructor
     */
    public Customer() {
    }

    @Id
    @GeneratedValue(strategy = IDENTITY)
    @Column(name = "ID", unique = true, nullable = false)
    public Integer getId() {
        return this.id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    @Column(name = "USERNAME")
    public String getUsername() {
        return this.username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    @Column(name = "PASSWORD")
    public String getPassword() {
        return this.password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    @Column(name = "TYPE")
    public String getType() {
        return this.type;
    }

    public void setType(String type) {
        this.type = type;
    }

    @Column(name = "SEX")
    public String getSex() {
        return this.sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    @Column(name = "TELEPHONE")
    public String getTelephone() {
        return this.telephone;
    }

    public void setTelephone(String telephone) {
        this.telephone = telephone;
    }

    @Column(name = "ADDRESS")
    public String getAddress() {
        return this.address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    @Column(name = "EMAIL")
    public String getEmail() {
        return this.email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    @Column(name = "FIXED_AREA_ID")
    public Integer getFixedAreaId() {
        return this.fixedAreaId;
    }

    public void setFixedAreaId(Integer fixedAreaId) {
        this.fixedAreaId = fixedAreaId;
    }
}

配置文件applicationContext-webservice.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:jaxrs="http://cxf.apache.org/jaxrs"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans.xsd
    http://cxf.apache.org/jaxrs 
    http://cxf.apache.org/schemas/jaxrs.xsd">
    
    <jaxrs:server address="/customerService">
        <jaxrs:serviceBeans>
            <bean class="com.fyc.crm.service.impl.CustomerServiceImpl"/>
        </jaxrs:serviceBeans>
        <!-- 日志拦截器 -->
        <jaxrs:inInterceptors>
            <bean class="org.apache.cxf.interceptor.LoggingInInterceptor"/>
        </jaxrs:inInterceptors>
        <jaxrs:outInterceptors>
            <bean class="org.apache.cxf.interceptor.LoggingOutInterceptor"/>
        </jaxrs:outInterceptors>
    </jaxrs:server>

    
</beans>

 

调用端依赖

  <!-- cxf:jax-rs -->
        <!-- 服务端 -->
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-frontend-jaxrs</artifactId>
        </dependency>
        <!-- 客户端 -->
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-rs-client</artifactId>
        </dependency>
        <!-- 转换json -->
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-rs-extension-providers</artifactId>
        </dependency>
        <dependency>
            <groupId>org.codehaus.jettison</groupId>
            <artifactId>jettison</artifactId>
        </dependency>

调用端的调用方式

 //判断数据库是否存在了这个手机号码
        Customer cust = WebClient.create(Constants.CRM_URL + "/customerService/checkTelephone?telephone=" + customer.getTelephone()).accept(MediaType.APPLICATION_JSON).get(Customer.class);

 //到这里证明可以被注册的手机号码正确,保存客户
        WebClient.create(Constants.CRM_URL + "/customerService").type(MediaType.APPLICATION_JSON).post(customer);

  //判断是否已经激活
          Customer customer= (Customer) WebClient.create(Constants.CRM_URL+"/customerService/checkTelephone?telephone="+telephone).accept(MediaType.APPLICATION_JSON).getCollection(Customer.class);
 

 

上一篇:UVA12267 Telephone Network


下一篇:487-3279(POJ1002)