一.webService介绍
之前的文章我介绍了cxf webService的使用,它是一种以wsdl发布的形式。下面结合我在了解说明下两组概念:
jax-ws 全称:JavaTM API forXML-Based Web Services ,实现基于soap协议的Web Service提供的API,SOAP:简单对象访问协议,它是交换数据的一种协议规范,是一种轻量的、简单的、基于XML(标准通用标记语言下的一个子集)的协议,
它被设计成在WEB上交换结构化的和固化的信息。
jax-rs 全称: JavaTM API for RESTful Web Services, 基于REST设计风格的WebServcice提供的API。它是一个Java 编程语言的应用程序接口,支持按照表述性状态转移(REST)架构风格创建Web服务。
二.jax-ws 和 jax-rs 的应用场景
jax-ws 是一套标准的soap协议,他是跨语言平台的,可移植性较高,目前在webService服务中是主流,通过服务端发布wsdl格式的xml文件,供客户端访问。
jax-rs 是一套java 编程的应用程序风格,那他就不具备夸平台性,移植性单一。但是目前主流的访问都趋向于rest风格,和springMVC无缝衔接,同时它为dubbo提供了接近透明的REST调用支持。
三.搭建 jax-rs web服务
这个案例是基于我前两篇jax-ws的基础上写的,没有我之前的webService案例的小伙伴,戳这里阅读
http://www.cnblogs.com/sumingk/articles/6069220.html
http://www.cnblogs.com/sumingk/articles/6069490.html
1.这是jax-rs xml文件配置,与ws的发布风格很类似,calss指定Service的地址,address为发布的范围地址,
1 xml version="1.0" encoding="UTF-8"?>
2 <beans
3 xmlns&#61;"http://www.springframework.org/schema/beans"
4 xmlns:xsi&#61;"http://www.w3.org/2001/XMLSchema-instance"
5 xmlns:p&#61;"http://www.springframework.org/schema/p"
6 xmlns:tx&#61;"http://www.springframework.org/schema/tx"
7 xmlns:aop&#61;"http://www.springframework.org/schema/aop"
8 xmlns:jaxws&#61;"http://cxf.apache.org/jaxws"
9 xmlns:jaxrs&#61;"http://cxf.apache.org/jaxrs"
10 xsi:schemaLocation&#61;"http://www.springframework.org/schema/beans
11 http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
12 http://www.springframework.org/schema/tx
13 http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
14 http://www.springframework.org/schema/aop
15 http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
16 http://cxf.apache.org/jaxws
17 http://cxf.apache.org/schemas/jaxws.xsd
18 http://cxf.apache.org/jaxrs
19 http://cxf.apache.org/schemas/jaxrs.xsd">
20
21 <bean id&#61;"userImpl" class&#61;"com.hdjf.app.me.impl.UserServiceImpl" />
22 <jaxrs:server id&#61;"userServer" address&#61;"/userCxf">
23 <jaxrs:serviceBeans>
24 <ref bean&#61;"userImpl"/>
25 jaxrs:serviceBeans>
26 jaxrs:server>
27
28
29 beans>
2.接口的定义
1 package com.hdjf.app.me;
2
3 import javax.jws.WebService;
4 import javax.ws.rs.core.UriInfo;
5
6 /**
7 * 用户接口
8 *
9 * &#64;author zhj
10 */
11 &#64;WebService
12 public interface IUserService {
13
14 /**
15 * 登录
16 *
17 * &#64;param phone
18 * 电话号
19 * &#64;param password
20 * 密码 {"phone":"18619487261","password":"123"}
21 * &#64;return
22 */
23 public String actionlogin(String type, String oper, String source,
24 String version, String url, String secret, String sign,
25 String para, String device,UriInfo ui);
26
27 /**
28 * 注册
29 *
30 * &#64;param phone
31 * 电话号
32 * &#64;param emscode
33 * 手机验证码
34 * &#64;param password
35 * 密码 {"phone":"18619487261","emscode":"123","password":"123"}
36 * &#64;return
37 */
38 public String regist(String type, String oper, String source,
39 String version, String url, String secret, String sign,
40 String para, String device,UriInfo ui);
41
42 /**
43 * 获取动态密码
44 *
45 * &#64;return
46 */
47 public String getDynamicNum(String phone);
48
49 }
3.service实现类
1 package com.hdjf.app.me.impl;
2
3 import java.util.UUID;
4
5 import javax.annotation.Resource;
6 import javax.ws.rs.Consumes;
7 import javax.ws.rs.FormParam;
8 import javax.ws.rs.POST;
9 import javax.ws.rs.Path;
10 import javax.ws.rs.Produces;
11 import javax.ws.rs.core.Context;
12 import javax.ws.rs.core.MediaType;
13 import javax.ws.rs.core.Response;
14 import javax.ws.rs.core.Response.ResponseBuilder;
15 import javax.ws.rs.core.Response.Status;
16 import javax.ws.rs.core.UriInfo;
17
18 import net.sf.json.JSONObject;
19
20 import com.hdjf.app.me.IUserService;
21 import com.hdjf.comm.VO;
22 import com.hdjf.comm.VOList;
23 import com.hdjf.dao.BaseDAO;
24
25 &#64;Path("/UserInfo")
26 &#64;Produces({ "application/json", "application/xml" })
27 public class UserServiceImpl implements IUserService {
28
29 &#64;Resource
30 public BaseDAO jdbcDAO;
31
32 public static void main(String[] args) {
33 // 创建 GUID 对象
34 UUID uuid &#61; UUID.randomUUID();
35 // 得到对象产生的ID
36 String token &#61; uuid.toString().toUpperCase();
37 // 替换 “-”变成空格
38 token &#61; token.replaceAll("-", "");
39 System.out.println(token);
40
41 String json &#61; "{&#39;type&#39;: &#39;truck&#39;,&#39;oper&#39;: &#39;actionlogin&#39;,&#39;source&#39;: &#39;&#39;,"
42 &#43; "&#39;version&#39;: &#39;&#39;,&#39;url&#39;: &#39;&#39;,"
43 &#43; "&#39;para&#39;: {&#39;phone&#39;: &#39;18310665040&#39;,&#39;password&#39;: &#39;123&#39;},"
44 &#43; "&#39;device&#39;: {&#39;os&#39;: &#39;ios&#39;,&#39;imei&#39;: &#39;5284cc188ecb8d8118774e534eaa74a6d8437344&#39;,&#39;osv&#39;: &#39;6.0&#39;},"
45 &#43; "&#39;secret&#39;: &#39;&#39;,&#39;sign&#39;: &#39;&#39;}";
46 JSONObject obj &#61; JSONObject.fromObject(json);
47 System.out.println(JSONObject.fromObject(obj.get("para")).get("phone") &#43;"&#61;&#61;&#61;&#61;"&#43;obj.getString("type"));
48
49 }
50
51 public static String printToken(){
52 // 创建 GUID 对象
53 UUID uuid &#61; UUID.randomUUID();
54 // 得到对象产生的ID
55 String token &#61; uuid.toString().toUpperCase();
56 // 替换 “-”变成空格
57 token &#61; token.replaceAll("-", "");
58
59 return token;
60 }
61
62
63 &#64;POST
64 &#64;Path("/actionlogin")
65 &#64;Consumes(MediaType.APPLICATION_FORM_URLENCODED)
66 &#64;Produces(MediaType.APPLICATION_JSON)
67 public String actionlogin(
68 &#64;FormParam("type") String type,
69 &#64;FormParam("oper") String oper,
70 &#64;FormParam("source") String source,
71 &#64;FormParam("version") String version,
72 &#64;FormParam("url") String url,
73 &#64;FormParam("secret") String secret,
74 &#64;FormParam("sign") String sign,
75 &#64;FormParam("para") String para,
76 &#64;FormParam("device") String device, &#64;Context UriInfo ui) {
77
78 JSONObject paraObj &#61; JSONObject.fromObject(para);
79 String phone &#61; paraObj.getString("phone");
80 String password &#61; paraObj.getString("password");
81 JSONObject obj &#61; new JSONObject();
82
83 obj.put("type", type);
84 obj.put("oper", oper);
85 obj.put("source", source);
86 obj.put("version", version);
87 obj.put("secret", secret);
88 obj.put("sign", sign);
89 String sql &#61; "select user_id,user_name from t_user where user_phone&#61;&#39;"&#43;phone&#43;"&#39; and user_pass&#61;&#39;"&#43;password&#43;"&#39; ";
90 System.out.println(sql);
91 VOList vlist &#61; jdbcDAO.queryByVOList(sql);
92
93 String msg &#61;"登录成功!";
94 obj.remove("para");
95 obj.remove("device");
96 JSONObject dataObj &#61; new JSONObject();
97 if(vlist.size()&#61;&#61;0){
98 msg &#61; "用户名或密码错误!";
99 dataObj.put("uid", "");
100 }else{
101 dataObj.put("uid", vlist.get(0).getAttr("user_id"));
102 }
103 dataObj.put("token", printToken());
104 obj.accumulate("data", dataObj);
105
106 ResponseBuilder rb&#61;Response.created(ui.getRequestUri());
107 int code &#61; rb.status(Status.OK).build().getStatus();
108
109 obj.put("code", code);
110 obj.put("msg", msg);
111 System.out.println(obj.toString());
112 return obj.toString();
113 }
114
115
116 &#64;POST
117 &#64;Path("/regist")
118 &#64;Consumes(MediaType.APPLICATION_FORM_URLENCODED)
119 &#64;Produces(MediaType.APPLICATION_JSON)
120 public String regist(&#64;FormParam("type") String type,
121 &#64;FormParam("oper") String oper,
122 &#64;FormParam("source") String source,
123 &#64;FormParam("version") String version,
124 &#64;FormParam("url") String url,
125 &#64;FormParam("secret") String secret,
126 &#64;FormParam("sign") String sign,
127 &#64;FormParam("para") String para,
128 &#64;FormParam("device") String device, &#64;Context UriInfo ui) {
129
130 JSONObject paraObj &#61; JSONObject.fromObject(para);
131 String phone &#61; paraObj.getString("phone");
132 String password &#61; paraObj.getString("password");
133
134 JSONObject obj &#61; new JSONObject();
135 obj.put("type", type);
136 obj.put("oper", oper);
137 obj.put("source", source);
138 obj.put("version", version);
139 obj.put("secret", secret);
140 obj.put("sign", sign);
141
142 /**
143 * user_type : 1&#xff1a;个人用户&#xff0c;2&#xff1a;企业用户&#39;,
144 * isAuthen : 0:false,1:true
145 * user_authentype,1.已注册用户&#xff0c;2.已实名认证&#xff0c;3.已绑定银行卡&#39;
146 * isGestureLogin, 0:false,1:true
147 */
148 String sql &#61; "select REPLACE(uuid(),&#39;-&#39;,&#39;&#39;) as uuid";
149 VO v &#61; this.jdbcDAO.queryByVO(sql);
150 String uuid &#61; v.getAttr("uuid");
151
152 String insertsql &#61; "insert into t_user (user_id,user_pass,user_phone,user_type,isAuthen,user_authentype,createtime) "
153 &#43; " values (&#39;"&#43;uuid&#43;"&#39;,&#39;"&#43;password&#43;"&#39;,&#39;"&#43;phone&#43;"&#39;,1,0,1,date_format(now(),&#39;%Y-%m-%d %T&#39;))";
154
155 JSONObject dataObj &#61; new JSONObject();
156 dataObj.put("token", printToken());
157 dataObj.put("uid", uuid);
158
159 String msg &#61; "注册成功!";
160 obj.put("msg", msg);
161
162 ResponseBuilder rb&#61;Response.created(ui.getRequestUri());
163 int code &#61; rb.status(Status.OK).build().getStatus();
164 obj.put("code", code);
165 try {
166 jdbcDAO.execute(insertsql);
167 } catch (Exception e) {
168 msg &#61; "用户注册异常"&#43;e.getMessage();
169 obj.put("msg", msg);
170 dataObj.put("uid", "");
171 obj.put("code", rb.status(Status.INTERNAL_SERVER_ERROR).build().getStatus());
172 }
173
174 obj.accumulate("data", dataObj);
175
176 return obj.toString();
177 }
178
179 &#64;Override
180 public String getDynamicNum(String phone) {
181 // TODO Auto-generated method stub
182 return null;
183 }
184
185
186 }
这里需要介绍一下 service实现类上的注解
&#64;Path("/UserInfo") //当前类的范围路径
&#64;Produces({ "application/json", "application/xml" }) //指定当前类返回的数据格式
同样的方法上也有&#64;path注解是当前方法的路径名称&#xff0c;类似于springMVC&#xff0c;&#64;Consumes(MediaType.APPLICATION_FORM_URLENCODED) 定义接受类型&#xff0c;&#64;Produces(MediaType.APPLICATION_JSON) 定义当前方法的返回类型&#xff0c;它的优先级高于类上注解的定义
4.以上的代码是我在工作业务上的应用&#xff0c;小伙伴可以采用简单的定义参数定义&#xff0c;其他的业务工具类我就不贴代码了&#xff0c;有需要的小伙伴可以在留言区&#64;我。
5.最后看下发布jax-rs服务&#xff0c;他的后缀名有别于wsdl,而是_wadl
6.最后客户端的请求访问&#xff0c;就用普通的http请求就可以&#xff0c;相对来说比soap的访问要简单的多。