SSH集成cxf 发布restful webservice

首先讲一下什么是restful webservice ,这个问题网上一搜有很多博文去长篇大论的介绍它,但是最后你看完了也会觉得云里雾里的,所以我在这里简单的讲一下我理解的rest webservice,其实我在使用restful webservice的初衷是为了解决跨域的问题。之前做过一个移动端的产品,使用html5套的原生安卓app的壳,调用服务端发布的rest 服务,通过ajax就可以请求到服务器,并返回需要的json值。

最近自己也在尝试去使用自己搭建的框架去集成CXF发布restwebservice服务,程序已经运行一段时间了,一直都想总结一下,当下终于有时间去记录一下自己的学习历程了。

首先这边使用的框架是hibernate+struts2+spring 典型的三大框架,当然现在很多企业都在使用springMVC 这个其实无所谓的,其次是本项目采用maven进行管理构建,所以配置jar的时候会非常的方便,好了,废话不多说,直接上干货。

一、配置maven依赖包

 1 <dependency>
 2             <groupId>org.apache.cxf</groupId>
 3             <artifactId>cxf-rt-rs-security-cors</artifactId>
 4             <version>3.0.1</version>
 5         </dependency>
 6         <dependency>
 7             <groupId>org.apache.cxf</groupId>
 8             <artifactId>cxf-core</artifactId>
 9             <version>3.0.1</version>
10         </dependency>
11         <dependency>
12             <groupId>org.apache.cxf</groupId>
13             <artifactId>cxf-rt-frontend-jaxrs</artifactId>
14             <version>3.0.1</version>
15         </dependency>
16         <dependency>
17             <groupId>javax.ws.rs</groupId>
18             <artifactId>javax.ws.rs-api</artifactId>
19             <version>2.0</version>
20         </dependency>
21         <dependency>
22             <groupId>org.apache.cxf</groupId>
23             <artifactId>cxf-rt-transports-http</artifactId>
24             <version>3.0.1</version>
25         </dependency>
26         <dependency>
27             <groupId>org.osgi</groupId>
28             <artifactId>org.osgi.core</artifactId>
29             <version>4.2.0</version>
30         </dependency>
31         <dependency>
32             <groupId>org.apache.cxf</groupId>
33             <artifactId>cxf-rt-ws-policy</artifactId>
34             <version>3.0.1</version>
35         </dependency>
36         <dependency>
37             <groupId>org.apache.cxf</groupId>
38             <artifactId>cxf-rt-wsdl</artifactId>
39             <version>3.0.1</version>
40         </dependency>
41         <dependency>
42             <groupId>wsdl4j</groupId>
43             <artifactId>wsdl4j</artifactId>
44             <version>1.6.3</version>
45         </dependency>
46         <dependency>
47             <groupId>org.osgi</groupId>
48             <artifactId>org.osgi.compendium</artifactId>
49             <version>4.2.0</version>
50         </dependency>
51         <dependency>
52             <groupId>xml-resolver</groupId>
53             <artifactId>xml-resolver</artifactId>
54             <version>1.2</version>
55         </dependency>
56         <dependency>
57             <groupId>asm</groupId>
58             <artifactId>asm</artifactId>
59             <version>3.3.1</version>
60         </dependency>
maven配置

二、配置web.xml

 1  
 2     <!-- webservice配置 start --> 
 3      <servlet-mapping>
 4         <servlet-name>CXFServlet</servlet-name>
 5         <url-pattern>/ws/*</url-pattern>
 6     </servlet-mapping>
 7     <servlet>
 8         <servlet-name>CXFServlet</servlet-name>
 9         <servlet-class>
10             org.apache.cxf.transport.servlet.CXFServlet
11         </servlet-class>
12     </servlet>
13     
14     <!-- webservice配置 end --> 
web.xml配置

三、配置applicationContext.xml

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
 4     xmlns:context="http://www.springframework.org/schema/context"
 5     xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"
 6     xmlns:aop="http://www.springframework.org/schema/aop" xmlns:jaxws="http://cxf.apache.org/jaxws"
 7     xmlns:soap="http://cxf.apache.org/bindings/soap" xmlns:jaxrs="http://cxf.apache.org/jaxrs"
 8     xsi:schemaLocation="http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd
 9          http://cxf.apache.org/bindings/soap http://cxf.apache.org/schemas/configuration/soap.xsd
10          http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
11          http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd 
12          http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.5.xsd
13          http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
14          http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
15          http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.xsd">
16 
17     <!-- 配置发布服务用的实现类 -->
18     <bean id="rest_MobileServiceImpl" class="cfs.wsdl.mobile.MobileServiceImpl"
19         scope="prototype">
20         <property name="dao">
21             <ref bean="commonBaseDaoHib" />
22         </property>
23     </bean>
24      
25       <jaxrs:server address="/rest">
26         <jaxrs:serviceBeans>
27             <ref bean="rest_MobileServiceImpl" />
28         </jaxrs:serviceBeans>
29         <jaxrs:providers>
30             <bean class="org.codehaus.jackson.jaxrs.JacksonJsonProvider" />
31              <bean
32                 class="org.apache.cxf.rs.security.cors.CrossOriginResourceSharingFilter">
33                 <property name="allowOrigins" value="*"></property>
34                 <property name="allowHeaders" value="*"></property>
35                 <property name="allowCredentials" value="true"></property>
36             </bean>
37         </jaxrs:providers>
38         <jaxrs:extensionMappings>
39             <entry key="json" value="application/json" />
40             <entry key="xml" value="application/xml" />
41         </jaxrs:extensionMappings>
42         <jaxrs:languageMappings>
43             <entry key="en" value="en-gb" />
44         </jaxrs:languageMappings>
45     </jaxrs:server>  
46       
47 </beans>
applicationContext-wsdl.xml

四、接口编写,我这边直接将项目用的接口贴出来了,我们项目中用的全是post提交方式,如果使用get提交可以参考登录方法中被注释的那个方法

  1 package cfs.wsdl.mobile;
  2 
  3 import javax.ws.rs.Consumes;
  4 import javax.ws.rs.FormParam;
  5 import javax.ws.rs.GET;
  6 import javax.ws.rs.POST;
  7 import javax.ws.rs.PUT;
  8 import javax.ws.rs.Path;
  9 import javax.ws.rs.PathParam;
 10 import javax.ws.rs.Produces;
 11 import javax.ws.rs.core.MediaType;
 12 
 13 import cfs.wsdl.cache.Mycache;
 14 
 15 @Path("/rest_MobileService")
 16 public interface MobileService {
 17     
 18     
 19     /**
 20      * 外部注册接口
 21      */
 22     @POST
 23     @Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
 24     @Path("/external/register")
 25     public String ExternalRegistration(String param);
 26     
 27     /**
 28      * 注册接口
 29      */
 30     @POST
 31     @Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
 32     @Path("/register")
 33     public String Register(String param);
 34 
 35     /**
 36      * 登录接口
 37      */
 38     /*@GET
 39     @Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
 40     @Path("/login/{param}")
 41     public String Login(@PathParam("param")String param);*/
 42 
 43     /**
 44      * 登录接口
 45      */
 46     @POST
 47     @Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
 48     @Path("/login")
 49     public String Login(String param);
 50     /**
 51      * 根据pid加载地区接口
 52      */
 53     @POST
 54     @Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
 55     @Path("/perfect/province")
 56     public String Province(String param);
 57     
 58     /**
 59      * 根据地区id加载学校接口
 60      */
 61     @POST
 62     @Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
 63     @Path("/perfect/school")
 64     public String School(String param);
 65     
 66     /**
 67      * 获取学制接口
 68      */
 69     @POST
 70     @Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
 71     @Path("/perfect/edusystem")
 72     public String EduSystem();
 73     
 74     /**
 75      * 根据学校id加载年级信息接口
 76      */
 77     @POST
 78     @Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
 79     @Path("/perfect/grade")
 80     public String Grade(String param);
 81     
 82     /**
 83      * 根据学校id和年级id加载学科信息接口
 84      */
 85     @POST
 86     @Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
 87     @Path("/perfect/subject")
 88     public String Subject(String param);
 89     
 90     /**
 91      * 加载全部学科信息接口
 92      */
 93     @POST
 94     @Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
 95     @Path("/perfect/allsubject")
 96     public String AllSubject(String param);
 97     
 98     /**
 99      * 加载用户信息接口
100      */
101     @POST
102     @Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
103     @Path("/load/user")
104     public String LoadUser(String param);
105     
106     
107     /**
108      * 根据学校id和年级id和学科id加载版本信息接口
109      */
110     @POST
111     @Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
112     @Path("/perfect/master")
113     public String Master(String param);
114     
115     
116 
117     /**
118      * 根据年级id加载版本信息接口(实际是小学、初中都用人教版,高中去教材里去查,只要是高中(年级为11、12、13)去数据库里查教材 能查到的版本都列出来
119      * )
120      */
121     @POST
122     @Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
123     @Path("/perfect/masterbygrade")
124     public String MasterByGrade(String param);
125     
126     
127     /**
128      * 完善信息保存提交接口
129      */
130     @POST
131     @Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
132     @Path("/perfect/submit")
133     public String Perfect(String param);
134     
135     /**
136      * 验证登录用户是否有班级信息接口
137      */
138     @POST
139     @Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
140     @Path("/class/validate")
141     public String ClassValidate(String param);
142     
143     /**
144      * 新建班级信息接口
145      */
146     @POST
147     @Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
148     @Path("/class/add")
149     public String NewClass(String param);
150     
151     
152     /**
153      * 根据用户id和班级号加载学科信息接口
154      */
155     @POST
156     @Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
157     @Path("/class/subject")
158     public String ClassSubject(String param);
159     
160     /**
161      * 生成邀请码接口
162      */
163     @POST
164     @Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
165     @Path("/class/code")
166     public String ClassCode(String param);
167     
168     /**
169      * 接受邀请码接口
170      */
171     @POST
172     @Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
173     @Path("/class/receivecode")
174     public String ReceiveCode(String param);
175     
176     
177     /**
178      * 移除老师接口
179      */
180     @POST
181     @Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
182     @Path("/class/removeteacher")
183     public String RemoveTeacher(String param);
184     
185     /**
186      * 转让管理权接口
187      */
188     @POST
189     @Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
190     @Path("/class/attornmanage")
191     public String AttornManage(String param);
192     
193     /**
194      * 解散班级接口
195      */
196     @POST
197     @Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
198     @Path("/class/dismiss")
199     public String Dismiss(String param);
200     
201     /**
202      * 查询指定班级学生接口
203      */
204     @POST
205     @Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
206     @Path("/class/student")
207     public String Student(String param);
208     
209     /**
210      * 删除指定班级学生接口
211      */
212     @POST
213     @Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
214     @Path("/class/delstudent")
215     public String DelStudent(String param);
216     
217     /**
218      * 查询教材、章节课时接口
219      */
220     @POST
221     @Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
222     @Path("/work/section")
223     public String Section(String param);
224     
225     /**
226      * 查询作业类型接口
227      */
228     @POST
229     @Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
230     @Path("/work/type")
231     public String workType(String param);
232     
233     /**
234      * 新建作业接口
235      */
236     @POST
237     @Consumes(MediaType.APPLICATION_JSON)
238     @Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
239     @Path("/work/add")
240     public String workAdd(String param);
241     
242     
243     /**
244      * 查询课时作业接口
245      */
246     @POST
247     @Consumes(MediaType.APPLICATION_JSON)
248     @Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
249     @Path("/work/classhour")
250     public String workClassHour(String param);
251     
252     
253     /**
254      * 查询指定老师创建的作业接口
255      */
256     @POST
257     @Consumes(MediaType.APPLICATION_JSON)
258     @Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
259     @Path("/work/gethomework")
260     public String gethomework(String param);
261     
262     
263     
264     /**
265      * 保存试题接口
266      */
267     @POST
268     @Consumes(MediaType.APPLICATION_JSON)
269     @Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
270     @Path("/work/addquestions")
271     public String workAddQuestions(String param);
272     
273     
274     
275     /**
276      * 获取试题信息接口
277      */
278     @POST
279     @Consumes(MediaType.APPLICATION_JSON)
280     @Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
281     @Path("/work/getquestions")
282     public String getquestions(String param);
283     
284     
285     
286     /**
287      * 修改试题信息接口
288      */
289     @POST
290     @Consumes(MediaType.APPLICATION_JSON)
291     @Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
292     @Path("/work/eidtquestions")
293     public String editQuestions(String param);
294     
295     /**
296      * 移除试题信息接口
297      */
298     @POST
299     @Consumes(MediaType.APPLICATION_JSON)
300     @Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
301     @Path("/work/delquestions")
302     public String delQuestions(String param);
303     
304     
305     
306     /**
307      * 移除作业信息接口
308      */
309     @POST
310     @Consumes(MediaType.APPLICATION_JSON)
311     @Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
312     @Path("/work/delhomework")
313     public String delhomework(String param);
314     
315     /**
316      * 布置作业接口
317      */
318     @POST
319     @Consumes(MediaType.APPLICATION_JSON)
320     @Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
321     @Path("/work/assign")
322     public String workAssign(String param);
323     
324     /**
325      * 修改布置作业接口
326      */
327     @POST
328     @Consumes(MediaType.APPLICATION_JSON)
329     @Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
330     @Path("/work/editassign")
331     public String workEditAssign(String param);
332     
333     /**
334      * 移除布置作业接口
335      */
336     @POST
337     @Consumes(MediaType.APPLICATION_JSON)
338     @Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
339     @Path("/work/delassign")
340     public String workDelAssign(String param);
341     
342     /**
343      * 查看我布置的作业(历次作业)接口
344      */
345     @POST
346     @Consumes(MediaType.APPLICATION_JSON)
347     @Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
348     @Path("/work/assignlist")
349     public String workGetAssignList(String param);
350     
351     
352     /**
353      * 查看布置作业详细信息接口
354      */
355     @POST
356     @Consumes(MediaType.APPLICATION_JSON)
357     @Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
358     @Path("/work/getassign")
359     public String workGetAssign(String param);
360     
361     
362     /**
363      * 查看批改作业信息接口
364      */
365     @POST
366     @Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
367     @Path("/work/correct/homework")
368     public String correctHomeWork(String param);
369     
370     
371     /**
372      * 查看批改试题信息接口
373      */
374     @POST
375     @Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
376     @Path("/work/correct/question")
377     public String correcQuestion(String param);
378     
379     /**
380      * 批改作业保存错题接口
381      */
382     @POST
383     @Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
384     @Path("/work/correct/submit")
385     public String correcSubmit(String param);
386     
387     
388     /**
389      * 批改作业保存错题给平板的接口
390      */
391     @POST
392     @Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
393     @Path("/work/correct/submitpostil")
394     public String correcSubmitPostil(String param);
395     
396     
397     /**
398      * 查看学生做完的作业接口
399      */
400     @POST
401     @Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
402     @Path("/view/homework")
403     public String viewHomework(String param);
404     
405     /**
406      * 查看学生做完的试题接口
407      */
408     @POST
409     @Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
410     @Path("/view/question")
411     public String viewQuestion(String param);
412     
413     /**
414      * 统计接口
415      */
416     @POST
417     @Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
418     @Path("/report")
419     public String report(String param);
420     
421     
422     /**
423      * 学生端完善信息接口
424      */
425     @POST
426     @Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
427     @Path("/student/perfect")
428     public String studentPerfect(String param);
429     
430     
431     /**
432      * 加载学生信息接口
433      */
434     @POST
435     @Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
436     @Path("/student/loaduser")
437     public String studentLoaduser(String param);
438     
439     /**
440      * 学生端查看科目,并查询新作业信息
441      */
442     @POST
443     @Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
444     @Path("/student/subject")
445     public String studentSubject(String param);
446     
447     /**
448      * 学生端查看指定学科的作业信息
449      */
450     @POST
451     @Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
452     @Path("/student/scuject/homework")
453     public String studentHomeworkByScuject(String param);
454     
455     /**
456      * 学生端查询指定作业下试题信息(答题使用)
457      */
458     @POST
459     @Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
460     @Path("/student/questions")
461     public String studentQuestions(String param);
462     
463     
464     /**
465      * 学生保存作业信息接口
466      */
467     @POST
468     @Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
469     @Path("/student/savehomework")
470     public String studentSaveHomework(String param);
471     
472     /**
473      * 学生端加载教材、章、节、课时信息接口
474      */
475     @POST
476     @Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
477     @Path("/student/section")
478     public String studentSection(String param);
479     
480     /**
481      * 学生端加载本次错题信息接口
482      */
483     @POST
484     @Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
485     @Path("/student/error")
486     public String studentError(String param);
487     
488     /**
489      * 学生端加载当前错题(过滤本次,未掌握的错题)信息接口
490      */
491     @POST
492     @Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
493     @Path("/student/error/current")
494     public String studentErrorByCurrent(String param);
495     
496     /**
497      * 学生端加载历史错题(已掌握的错题)信息接口
498      */
499     @POST
500     @Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
501     @Path("/student/error/history")
502     public String studentErrorByHistory(String param);
503     
504     
505     /**
506      * 学生端将当前错题转移到历史错题中接口
507      */
508     @POST
509     @Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
510     @Path("/student/curren2history")
511     public String studentCurren2history(String param);
512     
513     
514     
515     /**
516      * 家长完善信息接口
517      */
518     @POST
519     @Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
520     @Path("/patriarch/perfect")
521     public String patriarchPerfect(String param);
522     
523     /**
524      * 家长端给孩子退班接口
525      */
526     @POST
527     @Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
528     @Path("/patriarch/quit")
529     public String patriarchQuit(String param);
530     
531     /**
532      * 家长端查询孩子作业接口
533      */
534     @POST
535     @Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
536     @Path("/patriarch/work")
537     public String patriarchWork(String param);
538     
539     /**
540      * 家长端查看学生做完的试题接口
541      */
542     @POST
543     @Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
544     @Path("/patriarch/question")
545     public String patriarchQuestion(String param);
546     
547     /**
548      * 家长端查看家长信息
549      */
550     @POST
551     @Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
552     @Path("/patriarch/loaduser")
553     public String patriarchLoaduser(String param);
554     
555     /**
556      * 查看历史消息接口
557      */
558     @POST
559     @Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
560     @Path("/message/history")
561     public String messageHistory(String param);
562     
563     /**
564      * 更改密码接口
565      */
566     @POST
567     @Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
568     @Path("/repwd")
569     public String RePwd(String param);
570     
571     /**
572      * 收藏试题接口
573      */
574     @POST
575     @Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
576     @Path("/collect/question")
577     public String collectQuestion(String param);
578     
579     /**
580      * 查询我收藏的试题接口
581      */
582     @POST
583     @Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
584     @Path("/collect/view")
585     public String collectView(String param);
586     
587     
588 }
MobileService

五、编写对应的service实现类,也就是在第二步中配置的实现类,文件太大只好挂个路径了。

 http://pan.baidu.com/s/1skOAOpv

六、个人使用心得:

通过这种方式发布的服务,经过测试ios可以通过anf框架调用成功,Android也可以通过OKhttp方式去调用。

下面贴出我用ajax方式调用的方式:

   1 <script type="text/javascript">
   2     var  path="http://127.0.0.1:8082/cfs/ws/rest/rest_MobileService";
   3     //外部注册接口
   4     function ExternalRegistration(){
   5         $.ajax({
   6               url:path+"/external/register",
   7               type:"POST",
   8               contentType:"application/json; charset=utf-8",
   9               dataType:"json",
  10               data:{phone:"13844444444",type:"teacher",uname:"张三",
  11                   password:"bed3026624c120367f155dcf431a3eed",school_id:"1000121",grade_id:"6",
  12                   subject_id:"04",tatted_code:"63259",proclaimed_pwd:"88888888"},
  13               success: function(data){
  14                  alert( mini.encode(data));
  15               },
  16               error:function(){
  17                   alert("接口没有调用成功!");
  18               }
  19             });
  20     }
  21 
  22     
  23     //注册接口
  24     function register(){
  25         //调用此接口必须要传递的参数有:
  26         //手机号:phone
  27         //角色 :profile_name 注意:角色只能是三种(teacher、student、parent)
  28         $.ajax({
  29               url:path+"/register",
  30               type:"POST",
  31               contentType:"application/json; charset=utf-8",
  32               dataType:"json",
  33               data:{phone:"13844444444",profile_name:"parent"},
  34               success: function(data){
  35                  alert( mini.encode(data));
  36               },
  37               error:function(){
  38                   alert("接口没有调用成功!");
  39               }
  40             });
  41     }
  42 
  43     //登录接口
  44     function login(){
  45         //调用此接口必须要传递的参数有:
  46         //手机号:phone
  47         //角色 :profile_name 注意:角色只能是三种(teacher、student、parent)
  48          $.ajax({
  49               url:path+"/login",
  50               type:"POST",
  51               contentType:"application/json; charset=utf-8",
  52               dataType:"json",
  53               data:{phone:"13800000000",profile_name:"student"},
  54               success: function(data){
  55                  alert( mini.encode(data));
  56               },
  57               error:function(){
  58                   alert("接口没有调用成功!");
  59               }
  60             }); 
  61             
  62     }
  63     
  64     
  65 
  66     //修改密码接口
  67     function RePwd(){
  68         $.ajax({
  69               url:path+"/repwd",
  70               type:"POST",
  71               contentType:"application/json; charset=utf-8",
  72               dataType:"json",
  73               data:{user_id:"49cd3115-db52-4645-bf87-2aac8d05c33b",pwd:"123",newpwd:"111"},
  74               success: function(data){
  75                  alert( mini.encode(data));
  76               },
  77               error:function(){
  78                   alert("接口没有调用成功!");
  79               }
  80             });
  81     }
  82     
  83     //根据地区pid加载地区接口
  84     function perfectProvince(){
  85         //调用此接口必须要传递的参数有:
  86         //地区父级id:pid 注意:查询省地区的pid是0,第一次调用可以传0
  87         $.ajax({
  88               url:path+"/perfect/province",
  89               type:"POST",
  90               contentType:"application/json; charset=utf-8",
  91               dataType:"json",
  92               data:{pid:"0"},
  93               success: function(data){
  94                  alert( mini.encode(data));
  95               },
  96               error:function(){
  97                   alert("接口没有调用成功!");
  98               }
  99             });
 100     }
 101 
 102     //根据地区id加载学校接口
 103     function perfectSchool(){
 104         //调用此接口必须要传递的参数有:
 105         //地区id:area
 106         $.ajax({
 107               url:path+"/perfect/school",
 108               type:"POST",
 109               contentType:"application/json; charset=utf-8",
 110               dataType:"json",
 111               data:{area:"15072"},
 112               success: function(data){
 113                  alert( mini.encode(data));
 114               },
 115               error:function(){
 116                   alert("接口没有调用成功!");
 117               }
 118             });
 119     }
 120     
 121 
 122     //获取学制接口
 123     function perfectEdusystem(){
 124         $.ajax({
 125               url:path+"/perfect/edusystem",
 126               type:"POST",
 127               contentType:"application/json; charset=utf-8",
 128               dataType:"json",
 129               data:{},
 130               success: function(data){
 131                  alert( mini.encode(data));
 132               },
 133               error:function(){
 134                   alert("接口没有调用成功!");
 135               }
 136             });
 137     }
 138     
 139     //根据学校id加载年级信息接口
 140     function perfectGrade(){
 141         //调用此接口必须要传递的参数有:
 142         //学校id:school_id
 143         $.ajax({
 144               url:path+"/perfect/grade",
 145               type:"POST",
 146               contentType:"application/json; charset=utf-8",
 147               dataType:"json",
 148               data:{school_id:"111"},
 149               success: function(data){
 150                  alert( mini.encode(data));
 151               },
 152               error:function(){
 153                   alert("接口没有调用成功!");
 154               }
 155             });
 156     }
 157     
 158     //根据学校id和年级id加载学科信息接口
 159     function perfectSubject(){
 160         //调用此接口必须要传递的参数有:
 161         //学校id:school_id
 162         //年级id:grade_id
 163         $.ajax({
 164               url:path+"/perfect/subject",
 165               type:"POST",
 166               contentType:"application/json; charset=utf-8",
 167               dataType:"json",
 168               data:{school_id:"111",grade_id:"3"},
 169               success: function(data){
 170                  alert( mini.encode(data));
 171               },
 172               error:function(){
 173                   alert("接口没有调用成功!");
 174               }
 175             });
 176     }
 177     
 178     
 179     //查询全部学科信息接口
 180     function perfectAllSubject(){
 181         $.ajax({
 182               url:path+"/perfect/allsubject",
 183               type:"POST",
 184               contentType:"application/json; charset=utf-8",
 185               dataType:"json",
 186               data:{},
 187               success: function(data){
 188                  alert( mini.encode(data));
 189               },
 190               error:function(){
 191                   alert("接口没有调用成功!");
 192               }
 193             });
 194     }
 195     
 196     
 197     
 198     //根据学校id和年级id和学科id加载版本信息接口
 199     function perfectMaster(){
 200         //调用此接口必须要传递的参数有:
 201         //学校id:school_id
 202         //年级id:grade_id
 203         //学科id:subject_id
 204         $.ajax({
 205               url:path+"/perfect/master",
 206               type:"POST",
 207               contentType:"application/json; charset=utf-8",
 208               dataType:"json",
 209               data:{school_id:"111",grade_id:"3",subject_id:"03"},
 210               success: function(data){
 211                  alert( mini.encode(data));
 212               },
 213               error:function(){
 214                   alert("接口没有调用成功!");
 215               }
 216             });
 217     }
 218     
 219     //根据年级id加载版本信息接口
 220     function MasterByGrade(){
 221         //调用此接口必须要传递的参数有:
 222         //年级id:grade_id
 223         $.ajax({
 224               url:path+"/perfect/masterbygrade",
 225               type:"POST",
 226               contentType:"application/json; charset=utf-8",
 227               dataType:"json",
 228               data:{grade_id:"4"},
 229               success: function(data){
 230                  alert( mini.encode(data));
 231               },
 232               error:function(){
 233                   alert("接口没有调用成功!");
 234               }
 235             });
 236     }
 237     
 238     
 239     
 240     //完善信息保存提交接口
 241     function perfectSubmit(){
 242         //调用此接口必须要传递的参数有:
 243         //用户id:user_id
 244         //用户名称:uname
 245         //学校id:school_id
 246         //学制id:edu_system_id
 247         //年级id:grade_id
 248         //学科id:subject_id
 249         //版本id:master_id
 250         $.ajax({
 251               url:path+"/perfect/submit",
 252               type:"POST",
 253               contentType:"application/json; charset=utf-8",
 254               dataType:"json",
 255               data:{user_id:"e1cf1db9-0688-4a4e-9bf3-09801048bcce",uname:"李四",
 256                   school_id:"111",edu_system_id:"1",grade_id:"3",subject_id:"03",master_id:"01"},
 257               success: function(data){
 258                  alert( mini.encode(data));
 259               },
 260               error:function(){
 261                   alert("接口没有调用成功!");
 262               }
 263             });
 264     }
 265     
 266     //加载用户信息接口
 267     function loadUser(){
 268         $.ajax({
 269               url:path+"/load/user",
 270               type:"POST",
 271               contentType:"application/json; charset=utf-8",
 272               dataType:"json",
 273               data:{user_id:"3f16ecbe-7d4b-4ace-bb05-cefbe602e0c4"},
 274               success: function(data){
 275                  alert( mini.encode(data));
 276               },
 277               error:function(){
 278                   alert("接口没有调用成功!");
 279               }
 280             });
 281     }
 282     
 283     
 284     
 285     
 286     //验证登录用户是否有班级信息接口
 287     function classValidate(){
 288         //调用此接口必须要传递的参数有:
 289         //用户id:user_id
 290         $.ajax({
 291               url:path+"/class/validate",
 292               type:"POST",
 293               contentType:"application/json; charset=utf-8",
 294               dataType:"json",
 295               data:{user_id:"3f16ecbe-7d4b-4ace-bb05-cefbe602e0c4"},
 296               success: function(data){
 297                  alert( mini.encode(data));
 298               },
 299               error:function(){
 300                   alert("接口没有调用成功!");
 301               }
 302             });
 303     }
 304     
 305     //新建班级信息接口
 306     function classAdd(){
 307         //调用此接口必须要传递的参数有:
 308         //用户id:user_id
 309         //班级名称:class_name
 310         $.ajax({
 311               url:path+"/class/add",
 312               type:"POST",
 313               contentType:"application/json; charset=utf-8",
 314               dataType:"json",
 315               data:{user_id:"e7be0886-81e3-4135-9603-649c22843388",class_name:"测试班级1"},
 316               success: function(data){
 317                  alert( mini.encode(data));
 318               },
 319               error:function(){
 320                   alert("接口没有调用成功!");
 321               }
 322             });
 323     }
 324     
 325     //获取当前用户下指定班级的学科接口
 326     function classSubject(){
 327         //调用此接口必须要传递的参数有:
 328         //用户id:user_id
 329         //班级号:class_id
 330         $.ajax({
 331               url:path+"/class/subject",
 332               type:"POST",
 333               contentType:"application/json; charset=utf-8",
 334               dataType:"json",
 335               data:{user_id:"85de5fa7-d281-4b5b-b98f-d352a21d9b39",class_id:"1005"},
 336               success: function(data){
 337                  alert( mini.encode(data));
 338               },
 339               error:function(){
 340                   alert("接口没有调用成功!");
 341               }
 342             });
 343     }
 344     
 345     
 346     // 生成邀请码接口
 347     function classCode(){
 348         //调用此接口必须要传递的参数有:
 349         //被邀请人的手机号:phone
 350         //班级号:class_id
 351         //学科id:subject_id
 352         $.ajax({
 353               url:path+"/class/code",
 354               type:"POST",
 355               contentType:"application/json; charset=utf-8",
 356               dataType:"json",
 357               data:{phone:"13812345678",class_id:"6",subject_id:"04"},
 358               success: function(data){
 359                  alert( mini.encode(data));
 360               },
 361               error:function(){
 362                   alert("接口没有调用成功!");
 363               }
 364             });
 365     }
 366     
 367     // 接受邀请接口
 368     function classReceivecode(){
 369         //调用此接口必须要传递的参数有:
 370         //被邀请人用户id:user_id
 371         //邀请码:code
 372         $.ajax({
 373               url:path+"/class/receivecode",
 374               type:"POST",
 375               contentType:"application/json; charset=utf-8",
 376               dataType:"json",
 377               data:{user_id:"e1cf1db9-0688-4a4e-9bf3-09801048bcce",code:"929404"},
 378               success: function(data){
 379                  alert( mini.encode(data));
 380               },
 381               error:function(){
 382                   alert("接口没有调用成功!");
 383               }
 384             });
 385     }
 386     
 387     
 388     // 移除老师接口
 389     function classRemoveteacher(){
 390         //调用此接口必须要传递的参数有:
 391         //要移除的用户id:user_id
 392         //班级号:class_id
 393         //学科id:subject_id
 394         $.ajax({
 395               url:path+"/class/removeteacher",
 396               type:"POST",
 397               contentType:"application/json; charset=utf-8",
 398               dataType:"json",
 399               data:{user_id:"e1cf1db9-0688-4a4e-9bf3-09801048bcce",class_id:"6",subject_id:"04"},
 400               success: function(data){
 401                  alert( mini.encode(data));
 402               },
 403               error:function(){
 404                   alert("接口没有调用成功!");
 405               }
 406             });
 407     }
 408     
 409     // 转让管理权接口
 410     function classAttornmanage(){
 411         //调用此接口必须要传递的参数有:
 412         //要转让的用户id:user_id
 413         //班级号:class_id
 414         $.ajax({
 415               url:path+"/class/attornmanage",
 416               type:"POST",
 417               contentType:"application/json; charset=utf-8",
 418               dataType:"json",
 419               data:{user_id:"e1cf1db9-0688-4a4e-9bf3-09801048bcce",class_id:"6"},
 420               success: function(data){
 421                  alert( mini.encode(data));
 422               },
 423               error:function(){
 424                   alert("接口没有调用成功!");
 425               }
 426             });
 427     }
 428     
 429     // 解散班级接口
 430     function classDismiss(){
 431         //调用此接口必须要传递的参数有:
 432         //班级号:class_id
 433         $.ajax({
 434               url:path+"/class/dismiss",
 435               type:"POST",
 436               contentType:"application/json; charset=utf-8",
 437               dataType:"json",
 438               data:{class_id:"6"},
 439               success: function(data){
 440                  alert( mini.encode(data));
 441               },
 442               error:function(){
 443                   alert("接口没有调用成功!");
 444               }
 445             });
 446     }
 447     
 448     // 查询指定班级学生接口
 449     function classStudent(){
 450         //调用此接口必须要传递的参数有:
 451         //班级号:class_id
 452         $.ajax({
 453               url:path+"/class/student",
 454               type:"POST",
 455               contentType:"application/json; charset=utf-8",
 456               dataType:"json",
 457               data:{class_id:"1005"},
 458               success: function(data){
 459                  alert( mini.encode(data));
 460               },
 461               error:function(){
 462                   alert("接口没有调用成功!");
 463               }
 464             });
 465     }
 466     
 467     // 删除指定班级学生接口
 468     function classDelstudent(){
 469         //调用此接口必须要传递的参数有:
 470         //班级号:class_id
 471         //要删除的学生用户id:user-id
 472         $.ajax({
 473               url:path+"/class/delstudent",
 474               type:"POST",
 475               contentType:"application/json; charset=utf-8",
 476               dataType:"json",
 477               data:{class_id:"5",user_id:"0bac17a6-5614-4510-a30a-667a79110ce0"},
 478               success: function(data){
 479                  alert( mini.encode(data));
 480               },
 481               error:function(){
 482                   alert("接口没有调用成功!");
 483               }
 484             });
 485     }
 486     
 487     //查询教材、章节课时接口
 488     function workSection(){
 489          $.ajax({
 490               url:path+"/work/section",
 491               type:"POST",
 492               contentType:"application/json; charset=utf-8",
 493               dataType:"json",
 494               data:{user_id:"762d11af-55b1-4abb-81e6-607d8ac90351"},
 495               success: function(data){
 496                  alert( mini.encode(data));
 497               },
 498               error:function(){
 499                   alert("接口没有调用成功!");
 500               }
 501             });
 502     }
 503     
 504     //查询作业类型接口
 505     function workType(){
 506          $.ajax({
 507               url:path+"/work/type",
 508               type:"POST",
 509               contentType:"application/json; charset=utf-8",
 510               dataType:"json",
 511               data:{},
 512               success: function(data){
 513                  alert( mini.encode(data));
 514               },
 515               error:function(){
 516                   alert(111);
 517               }
 518             });
 519     }
 520     
 521     
 522     //查询课时作业试题接口
 523     function workClassHour(){
 524           $.ajax({
 525               url:path+"/work/classhour",
 526               type:"POST",
 527               contentType:"application/json; charset=utf-8",
 528               dataType:"json",
 529               data:{textbook_id:"2",section_id:"01-04-03"},
 530               success: function(data){
 531                  alert( mini.encode(data));
 532               },
 533               error:function(){
 534                   alert("接口没有调用成功!");
 535               }
 536             }); 
 537     }
 538     
 539     
 540     //新建作业接口
 541     function workAdd(){
 542          $.ajax({
 543               url:path+"/work/add",
 544               type:"POST",
 545               contentType:"application/json; charset=utf-8",
 546               dataType:"json",
 547               data:{user_id:"e7be0886-81e3-4135-9603-649c22843388",homework_name:"第一课时",textbook_id:"1",section_id:"21"},
 548               success: function(data){
 549                  alert( mini.encode(data));
 550               },
 551               error:function(){
 552                   alert("接口没有调用成功!");
 553               }
 554             });
 555     }
 556     
 557     //查询指定用户创建的作业接口
 558     function gethomeWork(){
 559          $.ajax({
 560               url:path+"/work/gethomework",
 561               type:"POST",
 562               contentType:"application/json; charset=utf-8",
 563               dataType:"json",
 564               data:{user_id:"e7be0886-81e3-4135-9603-649c22843388"    },
 565               success: function(data){
 566                  alert( mini.encode(data));
 567               },
 568               error:function(){
 569                   alert("接口没有调用成功!");
 570               }
 571             });
 572     }
 573     
 574     
 575     //保存试题接口
 576     function workAddqut(){
 577           $.ajax({
 578               url:path+"/work/addquestions",
 579               type:"POST",
 580               contentType:"application/json; charset=utf-8",
 581               dataType:"json",
 582               data:{homework_id:"be4a2cca-e17c-4deb-9650-1288d1cbf17d",question_no:"5",question_type:"3",
 583                   stem_text:"这是题干文字信息",stem_picture:$("#mydiv").text()+","+$("#mydiv").text(),
 584                   reference_answers_text:"填空1#填空2#填空3#填空4",
 585                   reference_answers_pciture:"",
 586               },
 587               success: function(data){
 588                  alert( mini.encode(data));
 589               },
 590               error:function(){
 591                   alert("接口没有调用成功!");
 592               }
 593             }); 
 594     }
 595     
 596     //获取试题信息接口
 597     function getquestions(){
 598           $.ajax({
 599               url:path+"/work/getquestions",
 600               type:"POST",
 601               contentType:"application/json; charset=utf-8",
 602               dataType:"json",
 603               data:{homework_id:"be4a2cca-e17c-4deb-9650-1288d1cbf17d"},
 604               success: function(data){
 605                  alert( mini.encode(data));
 606               },
 607               error:function(){
 608                   alert("接口没有调用成功!");
 609               }
 610             }); 
 611     }
 612     
 613     //修改试题接口
 614     function workEditqut(){
 615           $.ajax({
 616               url:path+"/work/eidtquestions",
 617               type:"POST",
 618               contentType:"application/json; charset=utf-8",
 619               dataType:"json",
 620               data:{questions_id:"15c7a680-de67-4ff7-a604-1ed74f2fe3a9",question_no:"6",question_type:"2",
 621                   stem_text:"这是题干文字信息测试测试",stem_picture:$("#mydiv").text()+","+$("#mydiv").text(),
 622                   reference_answers_text:"填空1#填空2",
 623                   reference_answers_pciture:"",
 624               },
 625               success: function(data){
 626                  alert( mini.encode(data));
 627               },
 628               error:function(){
 629                   alert("接口没有调用成功!");
 630               }
 631             }); 
 632     }
 633     
 634     
 635     //移除试题接口
 636     function workDelqut(){
 637           $.ajax({
 638               url:path+"/work/delquestions",
 639               type:"POST",
 640               contentType:"application/json; charset=utf-8",
 641               dataType:"json",
 642               data:{questions_id:"15c7a680-de67-4ff7-a604-1ed74f2fe3a9"},
 643               success: function(data){
 644                  alert( mini.encode(data));
 645               },
 646               error:function(){
 647                   alert("接口没有调用成功!");
 648               }
 649             }); 
 650     }
 651     
 652     //移除作业接口
 653     function workDelhomework(){
 654           $.ajax({
 655               url:path+"/work/delhomework",
 656               type:"POST",
 657               contentType:"application/json; charset=utf-8",
 658               dataType:"json",
 659               data:{homework_id:"be4a2cca-e17c-4deb-9650-1288d1cbf17d"},
 660               success: function(data){
 661                  alert( mini.encode(data));
 662               },
 663               error:function(){
 664                   alert("接口没有调用成功!");
 665               }
 666             }); 
 667     }
 668     
 669     //布置作业接口
 670     function  workAssign(){
 671          $.ajax({
 672               url:path+"/work/assign",
 673               type:"POST",
 674               contentType:"application/json; charset=utf-8",
 675               dataType:"json",
 676               data:{homework_id:"be4a2cca-e17c-4deb-9650-1288d1cbf17d",release_time:"2016-04-10 13:33:22",
 677                   end_time:"2016-04-10 15:33:22",answershow_time:"2016-04-10 15:33:22",assign_classes:"8"},
 678               success: function(data){
 679                  alert( mini.encode(data));
 680               },
 681               error:function(){
 682                   alert("接口没有调用成功!");
 683               }
 684             }); 
 685     }
 686     
 687     
 688     //修改布置作业接口
 689     function  workEditAssign(){
 690          $.ajax({
 691               url:path+"/work/editassign",
 692               type:"POST",
 693               contentType:"application/json; charset=utf-8",
 694               dataType:"json",
 695               data:{assign_id:"4b86160b-4c8e-4b8a-993d-ec67462eaff8",release_time:"2016-05-10 13:33:22",
 696                   end_time:"2016-05-10 15:33:22",answershow_time:"2016-05-10 15:33:22",assign_classes:"8"},
 697               success: function(data){
 698                  alert( mini.encode(data));
 699               },
 700               error:function(){
 701                   alert("接口没有调用成功!");
 702               }
 703             }); 
 704     }
 705     
 706     //移除布置作业接口
 707     function  workDelAssign(){
 708          $.ajax({
 709               url:path+"/work/delassign",
 710               type:"POST",
 711               contentType:"application/json; charset=utf-8",
 712               dataType:"json",
 713               data:{assign_id:"4b86160b-4c8e-4b8a-993d-ec67462eaff8"},
 714               success: function(data){
 715                  alert( mini.encode(data));
 716               },
 717               error:function(){
 718                   alert("接口没有调用成功!");
 719               }
 720             }); 
 721     }
 722     
 723     
 724     //查看我布置的作业(历次作业)接口
 725     function  workGetAssignList(){
 726          $.ajax({
 727               url:path+"/work/assignlist",
 728               type:"POST",
 729               contentType:"application/json; charset=utf-8",
 730               dataType:"json",
 731               data:{user_id:"c7a3d486-c4e0-454a-9d9a-84f21fa5c560"},
 732               success: function(data){
 733                  alert( mini.encode(data));
 734               },
 735               error:function(){
 736                   alert("接口没有调用成功!");
 737               }
 738             }); 
 739     }
 740     
 741     
 742     //查看布置作业详细信息接口
 743     function  workGetAssign(){
 744          $.ajax({
 745               url:path+"/work/getassign",
 746               type:"POST",
 747               contentType:"application/json; charset=utf-8",
 748               dataType:"json",
 749               data:{assign_id:"ade06ab5-946d-49d0-9f3b-162431e797f4"},
 750               success: function(data){
 751                  alert( mini.encode(data));
 752               },
 753               error:function(){
 754                   alert("接口没有调用成功!");
 755               }
 756             }); 
 757     }
 758     
 759     
 760     //查看批改作业信息接口
 761     function  correctHomeWork(){
 762          $.ajax({
 763               url:path+"/work/correct/homework",
 764               type:"POST",
 765               contentType:"application/json; charset=utf-8",
 766               dataType:"json",
 767               data:{user_id:"93301552-1834-4816-9c4c-795dc46b5c99"},
 768               success: function(data){
 769                  alert( mini.encode(data));
 770               },
 771               error:function(){
 772                   alert("接口没有调用成功!");
 773               }
 774             }); 
 775     }
 776     
 777     
 778     //查看批改试题信息接口
 779     function  correcQuestion(){
 780          $.ajax({
 781               url:path+"/work/correct/question",
 782               type:"POST",
 783               contentType:"application/json; charset=utf-8",
 784               dataType:"json",
 785               data:{assignmen_id:"ee8c7553-7613-4c02-b9d0-e603f098e4c8"},
 786               success: function(data){
 787                  alert( mini.encode(data));
 788               },
 789               error:function(){
 790                   alert("接口没有调用成功!");
 791               }
 792             }); 
 793     }
 794     
 795     //批改作业保存错题接口
 796     function  correcSubmit(){
 797          $.ajax({
 798               url:path+"/work/correct/submit",
 799               type:"POST",
 800               contentType:"application/json; charset=utf-8",
 801               dataType:"json",
 802               data:{question_id:"f2ce60d2-5d30-4100-a988-0d8f28084788",result:"1"},
 803               success: function(data){
 804                  alert( mini.encode(data));
 805               },
 806               error:function(){
 807                   alert("接口没有调用成功!");
 808               }
 809             }); 
 810     }
 811     
 812     
 813     
 814     //查看学生做完的作业接口(不含全是未知试题的作业)
 815     function  viewHomework(){
 816          $.ajax({
 817               url:path+"/view/homework",
 818               type:"POST",
 819               contentType:"application/json; charset=utf-8",
 820               dataType:"json",
 821               data:{user_id:"e7be0886-81e3-4135-9603-649c22843388"},
 822               success: function(data){
 823                  alert( mini.encode(data));
 824               },
 825               error:function(){
 826                   alert("接口没有调用成功!");
 827               }
 828             }); 
 829     }
 830     
 831     
 832     //查看学生做完的试题接口(不含未知状态的试题信息)
 833     function  viewQuestion(){
 834          $.ajax({
 835               url:path+"/view/question",
 836               type:"POST",
 837               contentType:"application/json; charset=utf-8",
 838               dataType:"json",
 839               data:{homework_id:"be4a2cca-e17c-4deb-9650-1288d1cbf17d"},
 840               success: function(data){
 841                  alert( mini.encode(data));
 842               },
 843               error:function(){
 844                   alert("接口没有调用成功!");
 845               }
 846             }); 
 847     }
 848     
 849     //学生完善信息接口
 850     function  studentPerfect(){
 851          $.ajax({
 852               url:path+"/student/perfect",
 853               type:"POST",
 854               contentType:"application/json; charset=utf-8",
 855               dataType:"json",
 856               data:{user_id:"e3bd5953-a855-4496-89a6-d5e498df04ef",uname:"张小三2号",class_id:"1005"},
 857               success: function(data){
 858                  alert( mini.encode(data));
 859               },
 860               error:function(){
 861                   alert("接口没有调用成功!");
 862               }
 863             }); 
 864     }
 865     
 866     //加载学生个人信息接口
 867     function  studentLoaduser(){
 868          $.ajax({
 869               url:path+"/student/loaduser",
 870               type:"POST",
 871               contentType:"application/json; charset=utf-8",
 872               dataType:"json",
 873               data:{user_id:"13e013c7-bd79-4c1a-9e20-e9c7f3c1115d"},
 874               success: function(data){
 875                  alert( mini.encode(data));
 876               },
 877               error:function(){
 878                   alert("接口没有调用成功!");
 879               }
 880             }); 
 881     }
 882     
 883     
 884     //学生端获取学科接口,包含是否有新作业信息
 885     function studentSubject(){
 886          $.ajax({
 887               url:path+"/student/subject",
 888               type:"POST",
 889               contentType:"application/json; charset=utf-8",
 890               dataType:"json",
 891               data:{user_id:"9cf4c885-d565-471e-83fa-d976a4a80c4e"},
 892               success: function(data){
 893                  alert( mini.encode(data));
 894               },
 895               error:function(){
 896                   alert("接口没有调用成功!");
 897               }
 898             }); 
 899     }
 900     
 901     //学生端查看指定学科的作业信息
 902     function studentHomeworkByScuject(){
 903          $.ajax({
 904               url:path+"/student/scuject/homework",
 905               type:"POST",
 906               contentType:"application/json; charset=utf-8",
 907               dataType:"json",
 908               data:{user_id:"e6d07b7b-2473-4dc6-8051-4aedad1e50ca",subject_id:"03"},
 909               success: function(data){
 910                  alert( mini.encode(data));
 911               },
 912               error:function(){
 913                   alert("接口没有调用成功!");
 914               }
 915             }); 
 916     }
 917     
 918     //学生端查询指定作业下试题信息(答题使用)
 919     function studentQuestions(){
 920          $.ajax({
 921               url:path+"/student/questions",
 922               type:"POST",
 923               contentType:"application/json; charset=utf-8",
 924               dataType:"json",
 925               data:{ass_id:"7da88c87-ca48-4140-927c-114dc1f9b6b3",homework_id:"be4a2cca-e17c-4deb-9650-1288d1cbf17d"},
 926               success: function(data){
 927                  alert( mini.encode(data));
 928               },
 929               error:function(){
 930                   alert("接口没有调用成功!");
 931               }
 932             }); 
 933     }
 934     
 935     //学生保存作业接口
 936     function studentSaveHomework(){
 937          $.ajax({
 938               url:path+"/student/savehomework",
 939               type:"POST",
 940               contentType:"application/json; charset=utf-8",
 941               dataType:"json",
 942               data:{ass_id:"e1a1c239-6a74-4d43-bce1-bc71362f89d6",
 943                   questions:[{id:"15c7a680-de67-4ff7-a604-1ed74f2fe3a9",answers_text:"B",answers_pciture:""},
 944                     {id:"20011696-c8b4-4293-a1b8-a5eb13e6e804",answers_text:"A",answers_pciture:""},
 945                     {id:"c993acd2-9cea-4880-a3bf-2ade5c3485d5",answers_text:"测试测试",answers_pciture:""}]
 946                   },
 947               success: function(data){
 948                  alert( mini.encode(data));
 949               },
 950               error:function(){
 951                   alert("接口没有调用成功!");
 952               }
 953             }); 
 954     }
 955     //学生端加载教材、章、节、课时信息接口
 956     function studentSection(){
 957          $.ajax({
 958               url:path+"/student/section",
 959               type:"POST",
 960               contentType:"application/json; charset=utf-8",
 961               dataType:"json",
 962               data:{class_id:"1005",subject_id:"02"},
 963               success: function(data){
 964                  alert( mini.encode(data));
 965               },
 966               error:function(){
 967                   alert("接口没有调用成功!");
 968               }
 969             }); 
 970     }
 971     
 972     //学生端加载本次错题信息接口
 973     function studentError(){
 974          $.ajax({
 975               url:path+"/student/error",
 976               type:"POST",
 977               contentType:"application/json; charset=utf-8",
 978               dataType:"json",
 979               data:{user_id:"7fa1ebe0-92d0-4830-a8f4-95513bc98cad",textbook_id:"32",section_id:"129"},
 980               success: function(data){
 981                  alert( mini.encode(data));
 982               },
 983               error:function(){
 984                   alert("接口没有调用成功!");
 985               }
 986             }); 
 987     }
 988     
 989     
 990     
 991     //学生端加载当前错题信息接口
 992     function studentErrorByCurrent(){
 993          $.ajax({
 994               url:path+"/student/error/current",
 995               type:"POST",
 996               contentType:"application/json; charset=utf-8",
 997               dataType:"json",
 998               data:{user_id:"7fa1ebe0-92d0-4830-a8f4-95513bc98cad",textbook_id:"32",section_id:"129"},
 999               success: function(data){
1000                  alert( mini.encode(data));
1001               },
1002               error:function(){
1003                   alert("接口没有调用成功!");
1004               }
1005             }); 
1006     }
1007     
1008     //学生端加载历史错题信息接口
1009     function studentErrorByHistory(){
1010          $.ajax({
1011               url:path+"/student/error/history",
1012               type:"POST",
1013               contentType:"application/json; charset=utf-8",
1014               dataType:"json",
1015               data:{user_id:"d13ecee0-a719-408c-bae3-89d871178406",textbook_id:"84",section_id:"7320"},
1016               success: function(data){
1017                  alert( mini.encode(data));
1018               },
1019               error:function(){
1020                   alert("接口没有调用成功!");
1021               }
1022             }); 
1023     }
1024     
1025     
1026     //学生端将当前错题转移到历史错题中接口
1027     function studentCurren2history(){
1028          $.ajax({
1029               url:path+"/student/curren2history",
1030               type:"POST",
1031               contentType:"application/json; charset=utf-8",
1032               dataType:"json",
1033               data:{question_id:"80137426-c6f3-4a38-bd65-e1b4ae4c0d1b"},
1034               success: function(data){
1035                  alert( mini.encode(data));
1036               },
1037               error:function(){
1038                   alert("接口没有调用成功!");
1039               }
1040             }); 
1041     }
1042     
1043     //家长端完善信息接口
1044     function patriarchPerfect(){
1045          $.ajax({
1046               url:path+"/patriarch/perfect",
1047               type:"POST",
1048               contentType:"application/json; charset=utf-8",
1049               dataType:"json",
1050               data:{user_id:"e5b26662-8b37-4d8a-be26-a73703d97c83",uname:"张三家长",sex:"1",child_phone:"13800000000"},
1051               success: function(data){
1052                  alert( mini.encode(data));
1053               },
1054               error:function(){
1055                   alert("接口没有调用成功!");
1056               }
1057             }); 
1058     }
1059     
1060     //家长端给孩子退班接口
1061     function patriarchQuit(){
1062          $.ajax({
1063               url:path+"/patriarch/quit",
1064               type:"POST",
1065               contentType:"application/json; charset=utf-8",
1066               dataType:"json",
1067               data:{user_id:"0bac17a6-5614-4510-a30a-667a79110ce0"},
1068               success: function(data){
1069                  alert( mini.encode(data));
1070               },
1071               error:function(){
1072                   alert("接口没有调用成功!");
1073               }
1074             }); 
1075     }
1076     
1077     //家长端查看孩子作业接口
1078     function patriarchWork(){
1079          $.ajax({
1080               url:path+"/patriarch/work",
1081               type:"POST",
1082               contentType:"application/json; charset=utf-8",
1083               dataType:"json",
1084               data:{user_id:"e6d07b7b-2473-4dc6-8051-4aedad1e50ca"},
1085               success: function(data){
1086                  alert( mini.encode(data));
1087               },
1088               error:function(){
1089                   alert("接口没有调用成功!");
1090               }
1091             }); 
1092     }
1093     
1094     //家长端查看学生做完的试题接口
1095     function patriarchQuestion(){
1096          $.ajax({
1097               url:path+"/patriarch/question",
1098               type:"POST",
1099               contentType:"application/json; charset=utf-8",
1100               dataType:"json",
1101               data:{homework_id:"be4a2cca-e17c-4deb-9650-1288d1cbf17d"},
1102               success: function(data){
1103                  alert( mini.encode(data));
1104               },
1105               error:function(){
1106                   alert("接口没有调用成功!");
1107               }
1108             }); 
1109     }
1110     
1111     //统计接口
1112     function report(){
1113          $.ajax({
1114               url:path+"/report",
1115               type:"POST",
1116               contentType:"application/json; charset=utf-8",
1117               dataType:"json",
1118               data:{homework_id:"f8f5a8c2-89b8-46b9-b3f6-d37c8cef82ed",class_id:"1039"},
1119               success: function(data){
1120                  alert( mini.encode(data));
1121               },
1122               error:function(){
1123                   alert("接口没有调用成功!");
1124               }
1125             }); 
1126     }
1127     
1128     //查询历史消息接口
1129     function messageHistory(){
1130          $.ajax({
1131               url:path+"/message/history",
1132               type:"POST",
1133               contentType:"application/json; charset=utf-8",
1134               dataType:"json",
1135               data:{send_target:"01"},
1136               success: function(data){
1137                  alert( mini.encode(data));
1138               },
1139               error:function(){
1140                   alert("接口没有调用成功!");
1141               }
1142             }); 
1143     }
1144     
1145 </script>
调用的javacript

使用java类客户端调用发布的服务

  1 package cfs.wsdl.client;
  2 
  3 import java.io.BufferedReader;
  4 import java.io.IOException;
  5 import java.io.InputStreamReader;
  6 import java.io.OutputStream;
  7 import java.net.HttpURLConnection;
  8 import java.net.MalformedURLException;
  9 import java.net.URL;
 10 
 11 
 12 
 13 public class Cxfclient {
 14     
 15     
 16     private static final String targetURL = "http://127.0.0.1:8082/cfs/ws/rest/rest_MobileService/login";
 17     public static void main(String[] args) {
 18         String str = "{"phone":"1889999888","profile_name":"teacher"}";
 19         String json=postMethod(str);
 20         System.out.println(json);
 21     }
 22     public static String getMethod(String str){
 23         String json="";
 24         try {
 25 
 26              URL restServiceURL = new URL(targetURL);
 27 
 28              HttpURLConnection httpConnection = (HttpURLConnection) restServiceURL.openConnection();
 29              httpConnection.setRequestMethod("GET");
 30              httpConnection.setRequestProperty("Accept", "application/json;charset=utf-8");
 31 
 32              if (httpConnection.getResponseCode() != 200) {
 33                     throw new RuntimeException("HTTP GET Request Failed with Error code : "
 34                                   + httpConnection.getResponseCode());
 35              }
 36 
 37              BufferedReader responseBuffer = new BufferedReader(new InputStreamReader(
 38                     (httpConnection.getInputStream())));
 39 
 40              String output;
 41              while ((output = responseBuffer.readLine()) != null) {
 42                      json+=output;
 43                      json=new String(json.getBytes(),"UTF-8");
 44              }
 45 
 46              httpConnection.disconnect();
 47 
 48         } catch (MalformedURLException e) {
 49 
 50              e.printStackTrace();
 51 
 52         } catch (IOException e) {
 53 
 54              e.printStackTrace();
 55 
 56         }
 57         
 58         return json;
 59 
 60     }
 61     
 62     
 63     public static String postMethod(String str){
 64         String json="";
 65           try {
 66 
 67               URL targetUrl = new URL(targetURL);
 68 
 69               HttpURLConnection httpConnection = (HttpURLConnection) targetUrl.openConnection();
 70               httpConnection.setDoOutput(true);
 71               httpConnection.setRequestMethod("POST");
 72               httpConnection.setRequestProperty("Content-Type", "application/json;charset=utf-8");
 73               OutputStream outputStream = httpConnection.getOutputStream();
 74               outputStream.write(str.getBytes());
 75               outputStream.flush();
 76               if (httpConnection.getResponseCode() != 200) {
 77                      throw new RuntimeException("Failed : HTTP error code : "
 78                             + httpConnection.getResponseCode());
 79               }
 80               BufferedReader responseBuffer = new BufferedReader(new InputStreamReader(
 81                             (httpConnection.getInputStream())));
 82 
 83               String output;
 84               while ((output = responseBuffer.readLine()) != null) {
 85                        json+=output;
 86                        json=new String(json.getBytes(),"UTF-8");
 87               }
 88 
 89               httpConnection.disconnect();
 90          } catch (MalformedURLException e) {
 91               e.printStackTrace();
 92          } catch (IOException e) {
 93               e.printStackTrace();
 94         }
 95 
 96         return json;
 97 
 98     }
 99     
100 }
Cxfclient.java

七、最后总结注意事项:

CXF发布的restful 服务如果通过ajax方式传入到后台的话他会解析成这种格式:

1 user_id=1+1=2&homework_name=2&textbook_id=3&section_id=4&qut[0][stem_text]=11&qut[0][stem_picture]=22&qut[0][question_type]=1&qut[0][reference_answers_text]=111&qut[0][reference_answers_pciture]=222&qut[0][qpt][0][pack_no]=1&qut[0][qpt][0][reference_answers]=1111&qut[0][qpt][1][pack_no]=2&qut[0][qpt][1][reference_answers]=2222&qut[1][stem_text]=22&qut[1][stem_picture]=33&qut[1][question_type]=2&qut[1][reference_answers_text]=444&qut[1][reference_answers_pciture]=222&qut[1][qpt][0][pack_no]=1&qut[1][qpt][0][reference_answers]=1111&qut[1][qpt][1][pack_no]=2&qut[1][qpt][1][reference_answers]=2222
ajax、ios、android调用服务接口得到的参数格式

而使用java 去调用服务接口传递的参数则是json格式,如果以后各位在用的时候可以在服务端打个断点跟进去看一下 具体是什么格式,然后再对数据进行不同的处理就好了,OK,大功告成!

原文地址:https://www.cnblogs.com/mycifeng/p/5563651.html