|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
效率会有不少的变化。而实际上java是基于堆栈机器来设计,这和我们常见的基于寄存器的本地机器是差异比较大的。总体来说,这是一种虚拟机的设计思路。本文将经由过程一个复杂的实例,先容怎样创立一个Rest服务接口,和经由过程JQUERY往对它举行挪用;次要接纳两种体例分离为Get跟Post;个中将经由过程Post提交复杂范例(Sring)和庞大范例(自界说完成UserModel)与Rest服务举行交互;
一Rest服务创立
个中Web客户端(ClintWeb)不合错误别的层的援用,只经由过程Rest部署后的服务举行效互;
1:实体层(Model)- usingSystem.Runtime.Serialization;namespaceModel{[DataContract]publicclassUserModel{[DataMember]publicintID{get;set;}[DataMember]publicstringUserName{get;set;}[DataMember]publicstringPassWord{get;set;}[DataMember]publicintAge{get;set;}publicoverridestringToString(){returnstring.Format("ID:{0};姓名:{1};岁数:{2};暗码:{3}",ID,UserName,Age,PassWord);}}}
复制代码 此处要注重[DataContract],[DataMember]在定名空间usingSystem.Runtime.Serialization上面;
2:接口层(IServiceInterface)- usingSystem.ServiceModel.Web;usingSystem.ServiceModel;usingModel;namespaceIServiceInterface{[ServiceContract]publicinterfaceIUser{[WebGet(UriTemplate="/{ID}",RequestFormat=WebMessageFormat.Json,ResponseFormat=WebMessageFormat.Json)]UserModelGetUserFromID(stringID);[WebGet(UriTemplate="All",RequestFormat=WebMessageFormat.Json,ResponseFormat=WebMessageFormat.Json,BodyStyle=WebMessageBodyStyle.Bare)]List<UserModel>GetAllUser();[WebInvoke(UriTemplate="/User/UserName",Method="POST",RequestFormat=WebMessageFormat.Json,ResponseFormat=WebMessageFormat.Json,BodyStyle=WebMessageBodyStyle.WrappedRequest)]StringGetUserName(stringName);[WebInvoke(UriTemplate="/User/Post",Method="POST",RequestFormat=WebMessageFormat.Json,ResponseFormat=WebMessageFormat.Json,BodyStyle=WebMessageBodyStyle.WrappedRequest)]stringUpdateUser(UserModelmodel);}}
复制代码 3:逻辑层(ServiceBll)- usingIServiceInterface;usingModel;namespaceServiceBll{publicclassUserBll:IUser{publicstaticList<UserModel>GetUserList(){List<UserModel>list=newList<UserModel>(){newUserModel(){ID=1,UserName="踏浪帅",PassWord="123456",Age=27},newUserModel(){ID=2,UserName="wujunyang",PassWord="345678",Age=30},newUserModel(){ID=3,UserName="cnblogs",PassWord="987654",Age=33}};returnlist;}publicUserModelGetUserFromID(stringID){UserModelitem=GetUserList().Where(a=>a.ID==int.Parse(ID)).SingleOrDefault();if(item!=null){returnitem;}else{returnnewUserModel();}}publicList<UserModel>GetAllUser(){returnGetUserList();}publicstringUpdateUser(UserModelmodel){returnmodel.ToString();}publicStringGetUserName(stringName){return"您好:"+Name;}}}
复制代码 前面创立的客户端传参数要跟下面各个办法的参数不异,好比:Name,model等
4:Rest服务(RestService)
此处新建一个文本文件把它修正成.svc格局,在其内里写进:- <%@ServiceHostLanguage="C#"Debug="true"Service="ServiceBll.UserBll"%>
复制代码 web.config文件内容:- <?xmlversion="1.0"encoding="utf-8"?><configuration><system.web><compilationdebug="true"targetFramework="4.0"/></system.web><system.serviceModel><behaviors><endpointBehaviors><behaviorname="webHttp"><webHttphelpEnabled="true"/></behavior></endpointBehaviors><serviceBehaviors><behaviorname="MapConfigBehavior"><!--为制止泄露元数据信息,请在部署前将以下值设置为false并删除下面的元数据闭幕点--><serviceMetadatahttpGetEnabled="true"/><!--要吸收妨碍非常具体信息以举行调试,请将以下值设置为true。在部署前设置为false以免泄露非常信息--><serviceDebugincludeExceptionDetailInFaults="true"/><dataContractSerializermaxItemsInObjectGraph="2147483647"/></behavior></serviceBehaviors></behaviors><bindings><webHttpBinding><bindingname="webHttpBindConfig"receiveTimeout="00:30:00"sendTimeout="00:30:00"maxReceivedMessageSize="104857600"><readerQuotasmaxStringContentLength="2147483647"maxArrayLength="2147483647"/><securitymode="None"></security></binding></webHttpBinding></bindings><services><servicename="ServiceBll.UserBll"behaviorConfiguration="MapConfigBehavior"><endpointbinding="webHttpBinding"contract="IServiceInterface.IUser"bindingConfiguration="webHttpBindConfig"behaviorConfiguration="webHttp"/></service></services></system.serviceModel></configuration>
复制代码 <p>那做企业软件是不是最好用J2EE? |
|