SSH集成cxf 发布restful webservice

时间:2023-07-03 12:57:20

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

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

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

一、配置maven依赖包

 <dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-rs-security-cors</artifactId>
<version>3.0.1</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-core</artifactId>
<version>3.0.1</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-jaxrs</artifactId>
<version>3.0.1</version>
</dependency>
<dependency>
<groupId>javax.ws.rs</groupId>
<artifactId>javax.ws.rs-api</artifactId>
<version>2.0</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-transports-http</artifactId>
<version>3.0.1</version>
</dependency>
<dependency>
<groupId>org.osgi</groupId>
<artifactId>org.osgi.core</artifactId>
<version>4.2.0</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-ws-policy</artifactId>
<version>3.0.1</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-wsdl</artifactId>
<version>3.0.1</version>
</dependency>
<dependency>
<groupId>wsdl4j</groupId>
<artifactId>wsdl4j</artifactId>
<version>1.6.3</version>
</dependency>
<dependency>
<groupId>org.osgi</groupId>
<artifactId>org.osgi.compendium</artifactId>
<version>4.2.0</version>
</dependency>
<dependency>
<groupId>xml-resolver</groupId>
<artifactId>xml-resolver</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>asm</groupId>
<artifactId>asm</artifactId>
<version>3.3.1</version>
</dependency>

maven配置

二、配置web.xml

     <!-- webservice配置 start -->
<servlet-mapping>
<servlet-name>CXFServlet</servlet-name>
<url-pattern>/ws/*</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>CXFServlet</servlet-name>
<servlet-class>
org.apache.cxf.transport.servlet.CXFServlet
</servlet-class>
</servlet> <!-- webservice配置 end -->

web.xml配置

三、配置applicationContext.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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:jaxws="http://cxf.apache.org/jaxws"
xmlns:soap="http://cxf.apache.org/bindings/soap" xmlns:jaxrs="http://cxf.apache.org/jaxrs"
xsi:schemaLocation="http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd
http://cxf.apache.org/bindings/soap http://cxf.apache.org/schemas/configuration/soap.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.xsd"> <!-- 配置发布服务用的实现类 -->
<bean id="rest_MobileServiceImpl" class="cfs.wsdl.mobile.MobileServiceImpl"
scope="prototype">
<property name="dao">
<ref bean="commonBaseDaoHib" />
</property>
</bean> <jaxrs:server address="/rest">
<jaxrs:serviceBeans>
<ref bean="rest_MobileServiceImpl" />
</jaxrs:serviceBeans>
<jaxrs:providers>
<bean class="org.codehaus.jackson.jaxrs.JacksonJsonProvider" />
<bean
class="org.apache.cxf.rs.security.cors.CrossOriginResourceSharingFilter">
<property name="allowOrigins" value="*"></property>
<property name="allowHeaders" value="*"></property>
<property name="allowCredentials" value="true"></property>
</bean>
</jaxrs:providers>
<jaxrs:extensionMappings>
<entry key="json" value="application/json" />
<entry key="xml" value="application/xml" />
</jaxrs:extensionMappings>
<jaxrs:languageMappings>
<entry key="en" value="en-gb" />
</jaxrs:languageMappings>
</jaxrs:server> </beans>

applicationContext-wsdl.xml

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

 package cfs.wsdl.mobile;

 import javax.ws.rs.Consumes;
import javax.ws.rs.FormParam;
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 javax.ws.rs.core.MediaType; import cfs.wsdl.cache.Mycache; @Path("/rest_MobileService")
public interface MobileService { /**
* 外部注册接口
*/
@POST
@Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
@Path("/external/register")
public String ExternalRegistration(String param); /**
* 注册接口
*/
@POST
@Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
@Path("/register")
public String Register(String param); /**
* 登录接口
*/
/*@GET
@Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
@Path("/login/{param}")
public String Login(@PathParam("param")String param);*/ /**
* 登录接口
*/
@POST
@Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
@Path("/login")
public String Login(String param);
/**
* 根据pid加载地区接口
*/
@POST
@Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
@Path("/perfect/province")
public String Province(String param); /**
* 根据地区id加载学校接口
*/
@POST
@Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
@Path("/perfect/school")
public String School(String param); /**
* 获取学制接口
*/
@POST
@Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
@Path("/perfect/edusystem")
public String EduSystem(); /**
* 根据学校id加载年级信息接口
*/
@POST
@Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
@Path("/perfect/grade")
public String Grade(String param); /**
* 根据学校id和年级id加载学科信息接口
*/
@POST
@Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
@Path("/perfect/subject")
public String Subject(String param); /**
* 加载全部学科信息接口
*/
@POST
@Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
@Path("/perfect/allsubject")
public String AllSubject(String param); /**
* 加载用户信息接口
*/
@POST
@Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
@Path("/load/user")
public String LoadUser(String param); /**
* 根据学校id和年级id和学科id加载版本信息接口
*/
@POST
@Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
@Path("/perfect/master")
public String Master(String param); /**
* 根据年级id加载版本信息接口(实际是小学、初中都用人教版,高中去教材里去查,只要是高中(年级为11、12、13)去数据库里查教材 能查到的版本都列出来
* )
*/
@POST
@Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
@Path("/perfect/masterbygrade")
public String MasterByGrade(String param); /**
* 完善信息保存提交接口
*/
@POST
@Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
@Path("/perfect/submit")
public String Perfect(String param); /**
* 验证登录用户是否有班级信息接口
*/
@POST
@Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
@Path("/class/validate")
public String ClassValidate(String param); /**
* 新建班级信息接口
*/
@POST
@Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
@Path("/class/add")
public String NewClass(String param); /**
* 根据用户id和班级号加载学科信息接口
*/
@POST
@Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
@Path("/class/subject")
public String ClassSubject(String param); /**
* 生成邀请码接口
*/
@POST
@Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
@Path("/class/code")
public String ClassCode(String param); /**
* 接受邀请码接口
*/
@POST
@Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
@Path("/class/receivecode")
public String ReceiveCode(String param); /**
* 移除老师接口
*/
@POST
@Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
@Path("/class/removeteacher")
public String RemoveTeacher(String param); /**
* 转让管理权接口
*/
@POST
@Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
@Path("/class/attornmanage")
public String AttornManage(String param); /**
* 解散班级接口
*/
@POST
@Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
@Path("/class/dismiss")
public String Dismiss(String param); /**
* 查询指定班级学生接口
*/
@POST
@Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
@Path("/class/student")
public String Student(String param); /**
* 删除指定班级学生接口
*/
@POST
@Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
@Path("/class/delstudent")
public String DelStudent(String param); /**
* 查询教材、章节课时接口
*/
@POST
@Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
@Path("/work/section")
public String Section(String param); /**
* 查询作业类型接口
*/
@POST
@Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
@Path("/work/type")
public String workType(String param); /**
* 新建作业接口
*/
@POST
@Consumes(MediaType.APPLICATION_JSON)
@Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
@Path("/work/add")
public String workAdd(String param); /**
* 查询课时作业接口
*/
@POST
@Consumes(MediaType.APPLICATION_JSON)
@Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
@Path("/work/classhour")
public String workClassHour(String param); /**
* 查询指定老师创建的作业接口
*/
@POST
@Consumes(MediaType.APPLICATION_JSON)
@Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
@Path("/work/gethomework")
public String gethomework(String param); /**
* 保存试题接口
*/
@POST
@Consumes(MediaType.APPLICATION_JSON)
@Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
@Path("/work/addquestions")
public String workAddQuestions(String param); /**
* 获取试题信息接口
*/
@POST
@Consumes(MediaType.APPLICATION_JSON)
@Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
@Path("/work/getquestions")
public String getquestions(String param); /**
* 修改试题信息接口
*/
@POST
@Consumes(MediaType.APPLICATION_JSON)
@Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
@Path("/work/eidtquestions")
public String editQuestions(String param); /**
* 移除试题信息接口
*/
@POST
@Consumes(MediaType.APPLICATION_JSON)
@Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
@Path("/work/delquestions")
public String delQuestions(String param); /**
* 移除作业信息接口
*/
@POST
@Consumes(MediaType.APPLICATION_JSON)
@Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
@Path("/work/delhomework")
public String delhomework(String param); /**
* 布置作业接口
*/
@POST
@Consumes(MediaType.APPLICATION_JSON)
@Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
@Path("/work/assign")
public String workAssign(String param); /**
* 修改布置作业接口
*/
@POST
@Consumes(MediaType.APPLICATION_JSON)
@Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
@Path("/work/editassign")
public String workEditAssign(String param); /**
* 移除布置作业接口
*/
@POST
@Consumes(MediaType.APPLICATION_JSON)
@Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
@Path("/work/delassign")
public String workDelAssign(String param); /**
* 查看我布置的作业(历次作业)接口
*/
@POST
@Consumes(MediaType.APPLICATION_JSON)
@Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
@Path("/work/assignlist")
public String workGetAssignList(String param); /**
* 查看布置作业详细信息接口
*/
@POST
@Consumes(MediaType.APPLICATION_JSON)
@Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
@Path("/work/getassign")
public String workGetAssign(String param); /**
* 查看批改作业信息接口
*/
@POST
@Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
@Path("/work/correct/homework")
public String correctHomeWork(String param); /**
* 查看批改试题信息接口
*/
@POST
@Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
@Path("/work/correct/question")
public String correcQuestion(String param); /**
* 批改作业保存错题接口
*/
@POST
@Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
@Path("/work/correct/submit")
public String correcSubmit(String param); /**
* 批改作业保存错题给平板的接口
*/
@POST
@Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
@Path("/work/correct/submitpostil")
public String correcSubmitPostil(String param); /**
* 查看学生做完的作业接口
*/
@POST
@Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
@Path("/view/homework")
public String viewHomework(String param); /**
* 查看学生做完的试题接口
*/
@POST
@Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
@Path("/view/question")
public String viewQuestion(String param); /**
* 统计接口
*/
@POST
@Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
@Path("/report")
public String report(String param); /**
* 学生端完善信息接口
*/
@POST
@Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
@Path("/student/perfect")
public String studentPerfect(String param); /**
* 加载学生信息接口
*/
@POST
@Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
@Path("/student/loaduser")
public String studentLoaduser(String param); /**
* 学生端查看科目,并查询新作业信息
*/
@POST
@Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
@Path("/student/subject")
public String studentSubject(String param); /**
* 学生端查看指定学科的作业信息
*/
@POST
@Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
@Path("/student/scuject/homework")
public String studentHomeworkByScuject(String param); /**
* 学生端查询指定作业下试题信息(答题使用)
*/
@POST
@Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
@Path("/student/questions")
public String studentQuestions(String param); /**
* 学生保存作业信息接口
*/
@POST
@Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
@Path("/student/savehomework")
public String studentSaveHomework(String param); /**
* 学生端加载教材、章、节、课时信息接口
*/
@POST
@Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
@Path("/student/section")
public String studentSection(String param); /**
* 学生端加载本次错题信息接口
*/
@POST
@Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
@Path("/student/error")
public String studentError(String param); /**
* 学生端加载当前错题(过滤本次,未掌握的错题)信息接口
*/
@POST
@Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
@Path("/student/error/current")
public String studentErrorByCurrent(String param); /**
* 学生端加载历史错题(已掌握的错题)信息接口
*/
@POST
@Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
@Path("/student/error/history")
public String studentErrorByHistory(String param); /**
* 学生端将当前错题转移到历史错题中接口
*/
@POST
@Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
@Path("/student/curren2history")
public String studentCurren2history(String param); /**
* 家长完善信息接口
*/
@POST
@Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
@Path("/patriarch/perfect")
public String patriarchPerfect(String param); /**
* 家长端给孩子退班接口
*/
@POST
@Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
@Path("/patriarch/quit")
public String patriarchQuit(String param); /**
* 家长端查询孩子作业接口
*/
@POST
@Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
@Path("/patriarch/work")
public String patriarchWork(String param); /**
* 家长端查看学生做完的试题接口
*/
@POST
@Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
@Path("/patriarch/question")
public String patriarchQuestion(String param); /**
* 家长端查看家长信息
*/
@POST
@Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
@Path("/patriarch/loaduser")
public String patriarchLoaduser(String param); /**
* 查看历史消息接口
*/
@POST
@Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
@Path("/message/history")
public String messageHistory(String param); /**
* 更改密码接口
*/
@POST
@Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
@Path("/repwd")
public String RePwd(String param); /**
* 收藏试题接口
*/
@POST
@Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
@Path("/collect/question")
public String collectQuestion(String param); /**
* 查询我收藏的试题接口
*/
@POST
@Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
@Path("/collect/view")
public String collectView(String param); }

MobileService

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

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

六、个人使用心得:

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

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

 <script type="text/javascript">
var path="http://127.0.0.1:8082/cfs/ws/rest/rest_MobileService";
//外部注册接口
function ExternalRegistration(){
$.ajax({
url:path+"/external/register",
type:"POST",
contentType:"application/json; charset=utf-8",
dataType:"json",
data:{phone:"13844444444",type:"teacher",uname:"张三",
password:"bed3026624c120367f155dcf431a3eed",school_id:"1000121",grade_id:"6",
subject_id:"04",tatted_code:"63259",proclaimed_pwd:"88888888"},
success: function(data){
alert( mini.encode(data));
},
error:function(){
alert("接口没有调用成功!");
}
});
} //注册接口
function register(){
//调用此接口必须要传递的参数有:
//手机号:phone
//角色 :profile_name 注意:角色只能是三种(teacher、student、parent)
$.ajax({
url:path+"/register",
type:"POST",
contentType:"application/json; charset=utf-8",
dataType:"json",
data:{phone:"13844444444",profile_name:"parent"},
success: function(data){
alert( mini.encode(data));
},
error:function(){
alert("接口没有调用成功!");
}
});
} //登录接口
function login(){
//调用此接口必须要传递的参数有:
//手机号:phone
//角色 :profile_name 注意:角色只能是三种(teacher、student、parent)
$.ajax({
url:path+"/login",
type:"POST",
contentType:"application/json; charset=utf-8",
dataType:"json",
data:{phone:"13800000000",profile_name:"student"},
success: function(data){
alert( mini.encode(data));
},
error:function(){
alert("接口没有调用成功!");
}
}); } //修改密码接口
function RePwd(){
$.ajax({
url:path+"/repwd",
type:"POST",
contentType:"application/json; charset=utf-8",
dataType:"json",
data:{user_id:"49cd3115-db52-4645-bf87-2aac8d05c33b",pwd:"123",newpwd:"111"},
success: function(data){
alert( mini.encode(data));
},
error:function(){
alert("接口没有调用成功!");
}
});
} //根据地区pid加载地区接口
function perfectProvince(){
//调用此接口必须要传递的参数有:
//地区父级id:pid 注意:查询省地区的pid是0,第一次调用可以传0
$.ajax({
url:path+"/perfect/province",
type:"POST",
contentType:"application/json; charset=utf-8",
dataType:"json",
data:{pid:"0"},
success: function(data){
alert( mini.encode(data));
},
error:function(){
alert("接口没有调用成功!");
}
});
} //根据地区id加载学校接口
function perfectSchool(){
//调用此接口必须要传递的参数有:
//地区id:area
$.ajax({
url:path+"/perfect/school",
type:"POST",
contentType:"application/json; charset=utf-8",
dataType:"json",
data:{area:"15072"},
success: function(data){
alert( mini.encode(data));
},
error:function(){
alert("接口没有调用成功!");
}
});
} //获取学制接口
function perfectEdusystem(){
$.ajax({
url:path+"/perfect/edusystem",
type:"POST",
contentType:"application/json; charset=utf-8",
dataType:"json",
data:{},
success: function(data){
alert( mini.encode(data));
},
error:function(){
alert("接口没有调用成功!");
}
});
} //根据学校id加载年级信息接口
function perfectGrade(){
//调用此接口必须要传递的参数有:
//学校id:school_id
$.ajax({
url:path+"/perfect/grade",
type:"POST",
contentType:"application/json; charset=utf-8",
dataType:"json",
data:{school_id:"111"},
success: function(data){
alert( mini.encode(data));
},
error:function(){
alert("接口没有调用成功!");
}
});
} //根据学校id和年级id加载学科信息接口
function perfectSubject(){
//调用此接口必须要传递的参数有:
//学校id:school_id
//年级id:grade_id
$.ajax({
url:path+"/perfect/subject",
type:"POST",
contentType:"application/json; charset=utf-8",
dataType:"json",
data:{school_id:"111",grade_id:"3"},
success: function(data){
alert( mini.encode(data));
},
error:function(){
alert("接口没有调用成功!");
}
});
} //查询全部学科信息接口
function perfectAllSubject(){
$.ajax({
url:path+"/perfect/allsubject",
type:"POST",
contentType:"application/json; charset=utf-8",
dataType:"json",
data:{},
success: function(data){
alert( mini.encode(data));
},
error:function(){
alert("接口没有调用成功!");
}
});
} //根据学校id和年级id和学科id加载版本信息接口
function perfectMaster(){
//调用此接口必须要传递的参数有:
//学校id:school_id
//年级id:grade_id
//学科id:subject_id
$.ajax({
url:path+"/perfect/master",
type:"POST",
contentType:"application/json; charset=utf-8",
dataType:"json",
data:{school_id:"111",grade_id:"3",subject_id:"03"},
success: function(data){
alert( mini.encode(data));
},
error:function(){
alert("接口没有调用成功!");
}
});
} //根据年级id加载版本信息接口
function MasterByGrade(){
//调用此接口必须要传递的参数有:
//年级id:grade_id
$.ajax({
url:path+"/perfect/masterbygrade",
type:"POST",
contentType:"application/json; charset=utf-8",
dataType:"json",
data:{grade_id:"4"},
success: function(data){
alert( mini.encode(data));
},
error:function(){
alert("接口没有调用成功!");
}
});
} //完善信息保存提交接口
function perfectSubmit(){
//调用此接口必须要传递的参数有:
//用户id:user_id
//用户名称:uname
//学校id:school_id
//学制id:edu_system_id
//年级id:grade_id
//学科id:subject_id
//版本id:master_id
$.ajax({
url:path+"/perfect/submit",
type:"POST",
contentType:"application/json; charset=utf-8",
dataType:"json",
data:{user_id:"e1cf1db9-0688-4a4e-9bf3-09801048bcce",uname:"李四",
school_id:"111",edu_system_id:"1",grade_id:"3",subject_id:"03",master_id:"01"},
success: function(data){
alert( mini.encode(data));
},
error:function(){
alert("接口没有调用成功!");
}
});
} //加载用户信息接口
function loadUser(){
$.ajax({
url:path+"/load/user",
type:"POST",
contentType:"application/json; charset=utf-8",
dataType:"json",
data:{user_id:"3f16ecbe-7d4b-4ace-bb05-cefbe602e0c4"},
success: function(data){
alert( mini.encode(data));
},
error:function(){
alert("接口没有调用成功!");
}
});
} //验证登录用户是否有班级信息接口
function classValidate(){
//调用此接口必须要传递的参数有:
//用户id:user_id
$.ajax({
url:path+"/class/validate",
type:"POST",
contentType:"application/json; charset=utf-8",
dataType:"json",
data:{user_id:"3f16ecbe-7d4b-4ace-bb05-cefbe602e0c4"},
success: function(data){
alert( mini.encode(data));
},
error:function(){
alert("接口没有调用成功!");
}
});
} //新建班级信息接口
function classAdd(){
//调用此接口必须要传递的参数有:
//用户id:user_id
//班级名称:class_name
$.ajax({
url:path+"/class/add",
type:"POST",
contentType:"application/json; charset=utf-8",
dataType:"json",
data:{user_id:"e7be0886-81e3-4135-9603-649c22843388",class_name:"测试班级1"},
success: function(data){
alert( mini.encode(data));
},
error:function(){
alert("接口没有调用成功!");
}
});
} //获取当前用户下指定班级的学科接口
function classSubject(){
//调用此接口必须要传递的参数有:
//用户id:user_id
//班级号:class_id
$.ajax({
url:path+"/class/subject",
type:"POST",
contentType:"application/json; charset=utf-8",
dataType:"json",
data:{user_id:"85de5fa7-d281-4b5b-b98f-d352a21d9b39",class_id:"1005"},
success: function(data){
alert( mini.encode(data));
},
error:function(){
alert("接口没有调用成功!");
}
});
} // 生成邀请码接口
function classCode(){
//调用此接口必须要传递的参数有:
//被邀请人的手机号:phone
//班级号:class_id
//学科id:subject_id
$.ajax({
url:path+"/class/code",
type:"POST",
contentType:"application/json; charset=utf-8",
dataType:"json",
data:{phone:"13812345678",class_id:"6",subject_id:"04"},
success: function(data){
alert( mini.encode(data));
},
error:function(){
alert("接口没有调用成功!");
}
});
} // 接受邀请接口
function classReceivecode(){
//调用此接口必须要传递的参数有:
//被邀请人用户id:user_id
//邀请码:code
$.ajax({
url:path+"/class/receivecode",
type:"POST",
contentType:"application/json; charset=utf-8",
dataType:"json",
data:{user_id:"e1cf1db9-0688-4a4e-9bf3-09801048bcce",code:"929404"},
success: function(data){
alert( mini.encode(data));
},
error:function(){
alert("接口没有调用成功!");
}
});
} // 移除老师接口
function classRemoveteacher(){
//调用此接口必须要传递的参数有:
//要移除的用户id:user_id
//班级号:class_id
//学科id:subject_id
$.ajax({
url:path+"/class/removeteacher",
type:"POST",
contentType:"application/json; charset=utf-8",
dataType:"json",
data:{user_id:"e1cf1db9-0688-4a4e-9bf3-09801048bcce",class_id:"6",subject_id:"04"},
success: function(data){
alert( mini.encode(data));
},
error:function(){
alert("接口没有调用成功!");
}
});
} // 转让管理权接口
function classAttornmanage(){
//调用此接口必须要传递的参数有:
//要转让的用户id:user_id
//班级号:class_id
$.ajax({
url:path+"/class/attornmanage",
type:"POST",
contentType:"application/json; charset=utf-8",
dataType:"json",
data:{user_id:"e1cf1db9-0688-4a4e-9bf3-09801048bcce",class_id:"6"},
success: function(data){
alert( mini.encode(data));
},
error:function(){
alert("接口没有调用成功!");
}
});
} // 解散班级接口
function classDismiss(){
//调用此接口必须要传递的参数有:
//班级号:class_id
$.ajax({
url:path+"/class/dismiss",
type:"POST",
contentType:"application/json; charset=utf-8",
dataType:"json",
data:{class_id:"6"},
success: function(data){
alert( mini.encode(data));
},
error:function(){
alert("接口没有调用成功!");
}
});
} // 查询指定班级学生接口
function classStudent(){
//调用此接口必须要传递的参数有:
//班级号:class_id
$.ajax({
url:path+"/class/student",
type:"POST",
contentType:"application/json; charset=utf-8",
dataType:"json",
data:{class_id:"1005"},
success: function(data){
alert( mini.encode(data));
},
error:function(){
alert("接口没有调用成功!");
}
});
} // 删除指定班级学生接口
function classDelstudent(){
//调用此接口必须要传递的参数有:
//班级号:class_id
//要删除的学生用户id:user-id
$.ajax({
url:path+"/class/delstudent",
type:"POST",
contentType:"application/json; charset=utf-8",
dataType:"json",
data:{class_id:"5",user_id:"0bac17a6-5614-4510-a30a-667a79110ce0"},
success: function(data){
alert( mini.encode(data));
},
error:function(){
alert("接口没有调用成功!");
}
});
} //查询教材、章节课时接口
function workSection(){
$.ajax({
url:path+"/work/section",
type:"POST",
contentType:"application/json; charset=utf-8",
dataType:"json",
data:{user_id:"762d11af-55b1-4abb-81e6-607d8ac90351"},
success: function(data){
alert( mini.encode(data));
},
error:function(){
alert("接口没有调用成功!");
}
});
} //查询作业类型接口
function workType(){
$.ajax({
url:path+"/work/type",
type:"POST",
contentType:"application/json; charset=utf-8",
dataType:"json",
data:{},
success: function(data){
alert( mini.encode(data));
},
error:function(){
alert(111);
}
});
} //查询课时作业试题接口
function workClassHour(){
$.ajax({
url:path+"/work/classhour",
type:"POST",
contentType:"application/json; charset=utf-8",
dataType:"json",
data:{textbook_id:"2",section_id:"01-04-03"},
success: function(data){
alert( mini.encode(data));
},
error:function(){
alert("接口没有调用成功!");
}
});
} //新建作业接口
function workAdd(){
$.ajax({
url:path+"/work/add",
type:"POST",
contentType:"application/json; charset=utf-8",
dataType:"json",
data:{user_id:"e7be0886-81e3-4135-9603-649c22843388",homework_name:"第一课时",textbook_id:"1",section_id:"21"},
success: function(data){
alert( mini.encode(data));
},
error:function(){
alert("接口没有调用成功!");
}
});
} //查询指定用户创建的作业接口
function gethomeWork(){
$.ajax({
url:path+"/work/gethomework",
type:"POST",
contentType:"application/json; charset=utf-8",
dataType:"json",
data:{user_id:"e7be0886-81e3-4135-9603-649c22843388" },
success: function(data){
alert( mini.encode(data));
},
error:function(){
alert("接口没有调用成功!");
}
});
} //保存试题接口
function workAddqut(){
$.ajax({
url:path+"/work/addquestions",
type:"POST",
contentType:"application/json; charset=utf-8",
dataType:"json",
data:{homework_id:"be4a2cca-e17c-4deb-9650-1288d1cbf17d",question_no:"5",question_type:"3",
stem_text:"这是题干文字信息",stem_picture:$("#mydiv").text()+","+$("#mydiv").text(),
reference_answers_text:"填空1#填空2#填空3#填空4",
reference_answers_pciture:"",
},
success: function(data){
alert( mini.encode(data));
},
error:function(){
alert("接口没有调用成功!");
}
});
} //获取试题信息接口
function getquestions(){
$.ajax({
url:path+"/work/getquestions",
type:"POST",
contentType:"application/json; charset=utf-8",
dataType:"json",
data:{homework_id:"be4a2cca-e17c-4deb-9650-1288d1cbf17d"},
success: function(data){
alert( mini.encode(data));
},
error:function(){
alert("接口没有调用成功!");
}
});
} //修改试题接口
function workEditqut(){
$.ajax({
url:path+"/work/eidtquestions",
type:"POST",
contentType:"application/json; charset=utf-8",
dataType:"json",
data:{questions_id:"15c7a680-de67-4ff7-a604-1ed74f2fe3a9",question_no:"6",question_type:"2",
stem_text:"这是题干文字信息测试测试",stem_picture:$("#mydiv").text()+","+$("#mydiv").text(),
reference_answers_text:"填空1#填空2",
reference_answers_pciture:"",
},
success: function(data){
alert( mini.encode(data));
},
error:function(){
alert("接口没有调用成功!");
}
});
} //移除试题接口
function workDelqut(){
$.ajax({
url:path+"/work/delquestions",
type:"POST",
contentType:"application/json; charset=utf-8",
dataType:"json",
data:{questions_id:"15c7a680-de67-4ff7-a604-1ed74f2fe3a9"},
success: function(data){
alert( mini.encode(data));
},
error:function(){
alert("接口没有调用成功!");
}
});
} //移除作业接口
function workDelhomework(){
$.ajax({
url:path+"/work/delhomework",
type:"POST",
contentType:"application/json; charset=utf-8",
dataType:"json",
data:{homework_id:"be4a2cca-e17c-4deb-9650-1288d1cbf17d"},
success: function(data){
alert( mini.encode(data));
},
error:function(){
alert("接口没有调用成功!");
}
});
} //布置作业接口
function workAssign(){
$.ajax({
url:path+"/work/assign",
type:"POST",
contentType:"application/json; charset=utf-8",
dataType:"json",
data:{homework_id:"be4a2cca-e17c-4deb-9650-1288d1cbf17d",release_time:"2016-04-10 13:33:22",
end_time:"2016-04-10 15:33:22",answershow_time:"2016-04-10 15:33:22",assign_classes:"8"},
success: function(data){
alert( mini.encode(data));
},
error:function(){
alert("接口没有调用成功!");
}
});
} //修改布置作业接口
function workEditAssign(){
$.ajax({
url:path+"/work/editassign",
type:"POST",
contentType:"application/json; charset=utf-8",
dataType:"json",
data:{assign_id:"4b86160b-4c8e-4b8a-993d-ec67462eaff8",release_time:"2016-05-10 13:33:22",
end_time:"2016-05-10 15:33:22",answershow_time:"2016-05-10 15:33:22",assign_classes:"8"},
success: function(data){
alert( mini.encode(data));
},
error:function(){
alert("接口没有调用成功!");
}
});
} //移除布置作业接口
function workDelAssign(){
$.ajax({
url:path+"/work/delassign",
type:"POST",
contentType:"application/json; charset=utf-8",
dataType:"json",
data:{assign_id:"4b86160b-4c8e-4b8a-993d-ec67462eaff8"},
success: function(data){
alert( mini.encode(data));
},
error:function(){
alert("接口没有调用成功!");
}
});
} //查看我布置的作业(历次作业)接口
function workGetAssignList(){
$.ajax({
url:path+"/work/assignlist",
type:"POST",
contentType:"application/json; charset=utf-8",
dataType:"json",
data:{user_id:"c7a3d486-c4e0-454a-9d9a-84f21fa5c560"},
success: function(data){
alert( mini.encode(data));
},
error:function(){
alert("接口没有调用成功!");
}
});
} //查看布置作业详细信息接口
function workGetAssign(){
$.ajax({
url:path+"/work/getassign",
type:"POST",
contentType:"application/json; charset=utf-8",
dataType:"json",
data:{assign_id:"ade06ab5-946d-49d0-9f3b-162431e797f4"},
success: function(data){
alert( mini.encode(data));
},
error:function(){
alert("接口没有调用成功!");
}
});
} //查看批改作业信息接口
function correctHomeWork(){
$.ajax({
url:path+"/work/correct/homework",
type:"POST",
contentType:"application/json; charset=utf-8",
dataType:"json",
data:{user_id:"93301552-1834-4816-9c4c-795dc46b5c99"},
success: function(data){
alert( mini.encode(data));
},
error:function(){
alert("接口没有调用成功!");
}
});
} //查看批改试题信息接口
function correcQuestion(){
$.ajax({
url:path+"/work/correct/question",
type:"POST",
contentType:"application/json; charset=utf-8",
dataType:"json",
data:{assignmen_id:"ee8c7553-7613-4c02-b9d0-e603f098e4c8"},
success: function(data){
alert( mini.encode(data));
},
error:function(){
alert("接口没有调用成功!");
}
});
} //批改作业保存错题接口
function correcSubmit(){
$.ajax({
url:path+"/work/correct/submit",
type:"POST",
contentType:"application/json; charset=utf-8",
dataType:"json",
data:{question_id:"f2ce60d2-5d30-4100-a988-0d8f28084788",result:"1"},
success: function(data){
alert( mini.encode(data));
},
error:function(){
alert("接口没有调用成功!");
}
});
} //查看学生做完的作业接口(不含全是未知试题的作业)
function viewHomework(){
$.ajax({
url:path+"/view/homework",
type:"POST",
contentType:"application/json; charset=utf-8",
dataType:"json",
data:{user_id:"e7be0886-81e3-4135-9603-649c22843388"},
success: function(data){
alert( mini.encode(data));
},
error:function(){
alert("接口没有调用成功!");
}
});
} //查看学生做完的试题接口(不含未知状态的试题信息)
function viewQuestion(){
$.ajax({
url:path+"/view/question",
type:"POST",
contentType:"application/json; charset=utf-8",
dataType:"json",
data:{homework_id:"be4a2cca-e17c-4deb-9650-1288d1cbf17d"},
success: function(data){
alert( mini.encode(data));
},
error:function(){
alert("接口没有调用成功!");
}
});
} //学生完善信息接口
function studentPerfect(){
$.ajax({
url:path+"/student/perfect",
type:"POST",
contentType:"application/json; charset=utf-8",
dataType:"json",
data:{user_id:"e3bd5953-a855-4496-89a6-d5e498df04ef",uname:"张小三2号",class_id:"1005"},
success: function(data){
alert( mini.encode(data));
},
error:function(){
alert("接口没有调用成功!");
}
});
} //加载学生个人信息接口
function studentLoaduser(){
$.ajax({
url:path+"/student/loaduser",
type:"POST",
contentType:"application/json; charset=utf-8",
dataType:"json",
data:{user_id:"13e013c7-bd79-4c1a-9e20-e9c7f3c1115d"},
success: function(data){
alert( mini.encode(data));
},
error:function(){
alert("接口没有调用成功!");
}
});
} //学生端获取学科接口,包含是否有新作业信息
function studentSubject(){
$.ajax({
url:path+"/student/subject",
type:"POST",
contentType:"application/json; charset=utf-8",
dataType:"json",
data:{user_id:"9cf4c885-d565-471e-83fa-d976a4a80c4e"},
success: function(data){
alert( mini.encode(data));
},
error:function(){
alert("接口没有调用成功!");
}
});
} //学生端查看指定学科的作业信息
function studentHomeworkByScuject(){
$.ajax({
url:path+"/student/scuject/homework",
type:"POST",
contentType:"application/json; charset=utf-8",
dataType:"json",
data:{user_id:"e6d07b7b-2473-4dc6-8051-4aedad1e50ca",subject_id:"03"},
success: function(data){
alert( mini.encode(data));
},
error:function(){
alert("接口没有调用成功!");
}
});
} //学生端查询指定作业下试题信息(答题使用)
function studentQuestions(){
$.ajax({
url:path+"/student/questions",
type:"POST",
contentType:"application/json; charset=utf-8",
dataType:"json",
data:{ass_id:"7da88c87-ca48-4140-927c-114dc1f9b6b3",homework_id:"be4a2cca-e17c-4deb-9650-1288d1cbf17d"},
success: function(data){
alert( mini.encode(data));
},
error:function(){
alert("接口没有调用成功!");
}
});
} //学生保存作业接口
function studentSaveHomework(){
$.ajax({
url:path+"/student/savehomework",
type:"POST",
contentType:"application/json; charset=utf-8",
dataType:"json",
data:{ass_id:"e1a1c239-6a74-4d43-bce1-bc71362f89d6",
questions:[{id:"15c7a680-de67-4ff7-a604-1ed74f2fe3a9",answers_text:"B",answers_pciture:""},
{id:"20011696-c8b4-4293-a1b8-a5eb13e6e804",answers_text:"A",answers_pciture:""},
{id:"c993acd2-9cea-4880-a3bf-2ade5c3485d5",answers_text:"测试测试",answers_pciture:""}]
},
success: function(data){
alert( mini.encode(data));
},
error:function(){
alert("接口没有调用成功!");
}
});
}
//学生端加载教材、章、节、课时信息接口
function studentSection(){
$.ajax({
url:path+"/student/section",
type:"POST",
contentType:"application/json; charset=utf-8",
dataType:"json",
data:{class_id:"1005",subject_id:"02"},
success: function(data){
alert( mini.encode(data));
},
error:function(){
alert("接口没有调用成功!");
}
});
} //学生端加载本次错题信息接口
function studentError(){
$.ajax({
url:path+"/student/error",
type:"POST",
contentType:"application/json; charset=utf-8",
dataType:"json",
data:{user_id:"7fa1ebe0-92d0-4830-a8f4-95513bc98cad",textbook_id:"32",section_id:"129"},
success: function(data){
alert( mini.encode(data));
},
error:function(){
alert("接口没有调用成功!");
}
});
} //学生端加载当前错题信息接口
function studentErrorByCurrent(){
$.ajax({
url:path+"/student/error/current",
type:"POST",
contentType:"application/json; charset=utf-8",
dataType:"json",
data:{user_id:"7fa1ebe0-92d0-4830-a8f4-95513bc98cad",textbook_id:"32",section_id:"129"},
success: function(data){
alert( mini.encode(data));
},
error:function(){
alert("接口没有调用成功!");
}
});
} //学生端加载历史错题信息接口
function studentErrorByHistory(){
$.ajax({
url:path+"/student/error/history",
type:"POST",
contentType:"application/json; charset=utf-8",
dataType:"json",
data:{user_id:"d13ecee0-a719-408c-bae3-89d871178406",textbook_id:"84",section_id:"7320"},
success: function(data){
alert( mini.encode(data));
},
error:function(){
alert("接口没有调用成功!");
}
});
} //学生端将当前错题转移到历史错题中接口
function studentCurren2history(){
$.ajax({
url:path+"/student/curren2history",
type:"POST",
contentType:"application/json; charset=utf-8",
dataType:"json",
data:{question_id:"80137426-c6f3-4a38-bd65-e1b4ae4c0d1b"},
success: function(data){
alert( mini.encode(data));
},
error:function(){
alert("接口没有调用成功!");
}
});
} //家长端完善信息接口
function patriarchPerfect(){
$.ajax({
url:path+"/patriarch/perfect",
type:"POST",
contentType:"application/json; charset=utf-8",
dataType:"json",
data:{user_id:"e5b26662-8b37-4d8a-be26-a73703d97c83",uname:"张三家长",sex:"1",child_phone:"13800000000"},
success: function(data){
alert( mini.encode(data));
},
error:function(){
alert("接口没有调用成功!");
}
});
} //家长端给孩子退班接口
function patriarchQuit(){
$.ajax({
url:path+"/patriarch/quit",
type:"POST",
contentType:"application/json; charset=utf-8",
dataType:"json",
data:{user_id:"0bac17a6-5614-4510-a30a-667a79110ce0"},
success: function(data){
alert( mini.encode(data));
},
error:function(){
alert("接口没有调用成功!");
}
});
} //家长端查看孩子作业接口
function patriarchWork(){
$.ajax({
url:path+"/patriarch/work",
type:"POST",
contentType:"application/json; charset=utf-8",
dataType:"json",
data:{user_id:"e6d07b7b-2473-4dc6-8051-4aedad1e50ca"},
success: function(data){
alert( mini.encode(data));
},
error:function(){
alert("接口没有调用成功!");
}
});
} //家长端查看学生做完的试题接口
function patriarchQuestion(){
$.ajax({
url:path+"/patriarch/question",
type:"POST",
contentType:"application/json; charset=utf-8",
dataType:"json",
data:{homework_id:"be4a2cca-e17c-4deb-9650-1288d1cbf17d"},
success: function(data){
alert( mini.encode(data));
},
error:function(){
alert("接口没有调用成功!");
}
});
} //统计接口
function report(){
$.ajax({
url:path+"/report",
type:"POST",
contentType:"application/json; charset=utf-8",
dataType:"json",
data:{homework_id:"f8f5a8c2-89b8-46b9-b3f6-d37c8cef82ed",class_id:"1039"},
success: function(data){
alert( mini.encode(data));
},
error:function(){
alert("接口没有调用成功!");
}
});
} //查询历史消息接口
function messageHistory(){
$.ajax({
url:path+"/message/history",
type:"POST",
contentType:"application/json; charset=utf-8",
dataType:"json",
data:{send_target:"01"},
success: function(data){
alert( mini.encode(data));
},
error:function(){
alert("接口没有调用成功!");
}
});
} </script>

调用的javacript

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

 package cfs.wsdl.client;

 import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL; public class Cxfclient { private static final String targetURL = "http://127.0.0.1:8082/cfs/ws/rest/rest_MobileService/login";
public static void main(String[] args) {
String str = "{\"phone\":\"1889999888\",\"profile_name\":\"teacher\"}";
String json=postMethod(str);
System.out.println(json);
}
public static String getMethod(String str){
String json="";
try { URL restServiceURL = new URL(targetURL); HttpURLConnection httpConnection = (HttpURLConnection) restServiceURL.openConnection();
httpConnection.setRequestMethod("GET");
httpConnection.setRequestProperty("Accept", "application/json;charset=utf-8"); if (httpConnection.getResponseCode() != 200) {
throw new RuntimeException("HTTP GET Request Failed with Error code : "
+ httpConnection.getResponseCode());
} BufferedReader responseBuffer = new BufferedReader(new InputStreamReader(
(httpConnection.getInputStream()))); String output;
while ((output = responseBuffer.readLine()) != null) {
json+=output;
json=new String(json.getBytes(),"UTF-8");
} httpConnection.disconnect(); } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return json; } public static String postMethod(String str){
String json="";
try { URL targetUrl = new URL(targetURL); HttpURLConnection httpConnection = (HttpURLConnection) targetUrl.openConnection();
httpConnection.setDoOutput(true);
httpConnection.setRequestMethod("POST");
httpConnection.setRequestProperty("Content-Type", "application/json;charset=utf-8");
OutputStream outputStream = httpConnection.getOutputStream();
outputStream.write(str.getBytes());
outputStream.flush();
if (httpConnection.getResponseCode() != 200) {
throw new RuntimeException("Failed : HTTP error code : "
+ httpConnection.getResponseCode());
}
BufferedReader responseBuffer = new BufferedReader(new InputStreamReader(
(httpConnection.getInputStream()))); String output;
while ((output = responseBuffer.readLine()) != null) {
json+=output;
json=new String(json.getBytes(),"UTF-8");
} httpConnection.disconnect();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} return json; } }

Cxfclient.java

七、最后总结注意事项:

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

 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,大功告成!