WebService实例-CRM系统提供WebService实现用户注册功能

来源:互联网 发布:网络用语ps是什么意思 编辑:程序博客网 时间:2024/05/29 14:52

<—start—>
编写crm的webservice接口,实现客户信息保存操作。在CustomerService接口中新增一个服务接口,用于添加客户注册的信息。

@Path("/customer")@POST@Consumes({ "application/xml", "application/json" })public void regist(Customer customer);

在实现类中,只需要调用dao的save方法保存以下就可。

@Overridepublic void regist(Customer customer) {    System.out.println(customer);    customerRepository.save(customer);}

因为save方法jpa接口中默认就有,所以不需要自定义去创建方法。
接下来就是在CustomerAction类中调用服务了。
调用webservice连接crm保存客户信息:发送json类型的数据,调用post方法将customer传递过去,客户注册就要保存到后台数据库,所以用post方法去添加。

@ParentPackage("json-default")@Namespace("/")@Controller@Scope("prototype")public class CustomerAction2 extends BaseAction<Customer> {    @Action(value="customer_sendSms")    public String sendSms() throws IOException{        //生成短信验证码        String randomCode = RandomStringUtils.randomNumeric(4);        //将短信验证码保存到session中        ServletActionContext.getRequest().getSession().setAttribute(model.getTelephone(),randomCode);        //编辑短信内容        String msg = "你好!本次获取的验证码位:"+randomCode;        //调用SMS服务发送短信        //String result = SmsUtils.sendSmsByHTTP(model.getTelephone(),msg);        String result = "000/XXX";        if(result.startsWith("000")){            //以"000"开头表示短信发送成功            return NONE;        }else{            //发送失败,就抛出一个运行期异常            throw new RuntimeException("短信发送失败,信息码:"+result);        }    }    //属性驱动接收页面填写的验证码    private String checkCode;    public void setCheckCode(String checkCode) {        this.checkCode = checkCode;    }    @Action(value="customer_regist",results={@Result(name="success",type="redirect",location="signup_success.html"),            @Result(name="input",type="redirect",location="signup.html")})    public String regist(){        //先校验短信验证码,如果不通过就跳回登录页面        //从session中获取之前生成的短信验证码        String checkcodeSession = (String) ServletActionContext.getRequest().getAttribute(model.getTelephone());        if(checkcodeSession==null||!checkcodeSession.equals(checkCode)){            System.out.println("短信验证码错误!");            //短信验证码错误            return INPUT;        }        //调用webservice连接crm保存客户信息        WebClient.create("http://localhost:9002/crm_management/services/customerService/customer").type(MediaType.APPLICATION_XML)        .post(model);        System.out.println("客户注册成功...");        return SUCCESS;    }}

<—end—>

原创粉丝点击