1: public class UriTemplate 2: { 3: //其他成员 4: public UriTemplate(string template); 5: public UriTemplate(string template, bool ignoreTrailingSlash); 6: public UriTemplate(string UriTemplate对象关联。 而MatchSingle方法被执行的时候会在内部调用Match方法,如果没有匹配的UriTemplate,返回Null;如果只有唯一匹配的UriTemplate,则返回对应的UriTemplateMatch <UriTemplate, object>(new UriTemplate(template),operation.Name)); 22: } 23: } 24:
** public List<String> getVariables() { ... } } 该类并不是抽象类,并且它还有4个子类,分别对应着可以使用模版语法的几个注解: ---- UriTemplate 顾名思义,它是用于处理URI的模版,用于处理@RequestLine的模版 public class UriTemplate extends Template { public static UriTemplate create(String template, Charset charset) { return new UriTemplate(template, true, charset 使用示例: @Test public void fun1() { UriTemplate template = UriTemplate.create("http://example.com/ // 最重要的方法:用于把请求模版组装、加上Base Url、转换为一个真正的Request // 此input模版里包含有很多的:QueryTemplate/HeaderTemplate/UriTemplate
WCF Service》中的web.config,因为需要AJAX,endpointBehaviors用了<enableWebScript />,但是enableWebScript 和REST需要的UriTemplate return Guid.NewGuid(); } [OperationContract] [WebInvoke(Method = "DELETE", UriTemplate { return; } [OperationContract] [WebInvoke(Method = "POST", UriTemplate Employee employee) { return; } [OperationContract] [WebGet(UriTemplate return Guid.NewGuid(); } [OperationContract] [WebInvoke(Method = "DELETE", UriTemplate
篇幅所限,我们不能对WCF REST的UriTemplate作详细的介绍,有兴趣的读者可以参考《UriTemplate、UriTemplateTable与WebHttpDispatchOperationSelector [源代码从这里下载] 我们创建一个新的ASP.NET Web应用,并且添加针对程序集System.ServiceModel.dll的引用(UriTemplate定义在该程序集中),然后创建如下一个针对UriTemplate 1: public class UriTemplateRoute:RouteBase 2: { 3: public UriTemplate UriTemplate { get; 在匹配的情况下通过调用UriTemplate的BindByName方法得到一个完整的Uri。 我个人具有基于UriTemplate的URI模板比针对Route的URL模板更好用,其中一点就是它在定义默认值方法更为直接。
所以Link还可以通过UriTemplate来构建: public static Link of(UriTemplate template, String relation) { return new Link(template, relation); } public static Link of(UriTemplate template, LinkRelation relation) { return new Link(template, relation); } UriTemplate是对URI模板的封装,我们看一个使用的例子: Link 除了直接使用string之外,还可以传入UriTemplate: UriTemplate template = UriTemplate.of("/{segment}/something") .with
所以Link还可以通过UriTemplate来构建: public static Link of(UriTemplate template, String relation) { return new Link(template, relation); } public static Link of(UriTemplate template, LinkRelation relation) { return new Link(template, relation); } UriTemplate是对URI模板的封装,我们看一个使用的例子: Link 除了直接使用string之外,还可以传入UriTemplate: UriTemplate template = UriTemplate.of("/{segment}/something") .with
可以把该类理解为UriTemplate的一个增强版,处理URI外,还有Headers、Body等更多的信息来支持一个标准的Http请求。 uriTemplate; // 注意:此处target表示的是最终准备请求的URL,并非feign.Target private String target; // 拼接在url后面的片段,一般表示锚点 = UriTemplate.create(this.uriTemplate.toString(), ! this.decodeSlash, this.charset); return this; } // 该方法作用:给request设置一个uri // 这里会解析UriTemplate ,·uriTemplate.expand(variables)· // 2、解析查询参数模版,这块相对复杂。
requests in /Users/yinzhuoqun/.pyenv/joyoo2/lib/python3.6/site-packages (from coreapi) (2.18.4) Collecting uritemplate Downloading uritemplate-3.0.1-py2.py3-none-any.whl (15 kB) Collecting coreschema Downloading coreschema e9bdef7dca4b276f7f28a03943f4059466188def81a65b1b44 Successfully built coreschema Installing collected packages: itypes, uritemplate coreapi Successfully installed MarkupSafe-1.1.1 coreapi-2.3.3 coreschema-0.0.4 itypes-1.2.0 jinja2-2.11.2 uritemplate
1: [ServiceContract] 2: public interface IEmployees 3: { 4: [WebGet(UriTemplate = "all") : [Description("获取所有员工列表")] 6: IEnumerable<Employee> GetAll(); 7: 8: [WebGet(UriTemplate 9: [Description("获取指定ID的员工")] 10: Employee Get(string id); 11: 12: [WebInvoke(UriTemplate Description("创建一个新的员工")] 14: Employee Create(Employee employee); 15: 16: [WebInvoke(UriTemplate 1: [ServiceContract] 2: public interface IEmployees 3: { 4: //其他成员 5: [WebGet(UriTemplate
Downloading github3.py-0.9.5-py2.py3-none-any.whl (109kB): 109kB downloaded Downloading/unpacking uritemplate.py >=0.2.0 (from github3.py) Downloading uritemplate.py-0.3.0.tar.gz Running setup.py (path:/tmp/pip-build-LRgVNm /uritemplate.py/setup.py) egg_info for package uritemplate.py Requirement already satisfied (use --upgrade 2.0 in /usr/lib/python2.7/dist-packages (from github3.py) Installing collected packages: github3.py, uritemplate.py Running setup.py install for uritemplate.py Successfully installed github3.py uritemplate.py Cleaning
对于SOAP协议来说,操作的选择是通过<Action>来决定的,而在这儿时通过UriTemplate属性表示的一个URI模板来决定的,常见的路由例子如接下来的,/filename. ="all")] 7 IEnumerable<Employee> GetAll(); 8 9 [WebGet(UriTemplate = "{id}")] 10 Employee Get (String id); 11 12 [WebInvoke(UriTemplate = "/", Method = "POST")] 13 void Create(Employee employee); 14 15 [WebInvoke(UriTemplate = "/", Method = "PUT")] 16 void Update(Employee employee); 17 18 [WebInvoke( UriTemplate = "{id}", Method = "DELETE")] 19 void Delete(string id); 20 } 21 } Service的实现 1 public class
一切都很理想
后来因为项目需要,有人觉得Test这个名称太土,想换个名字,而程序员又不乐意,于是用UriTemplate起了个别名HelloWorld应付,如下:
/**////
UriTemplateHandler uriTemplateHandler = new DefaultUriBuilderFactory(); @Override public URI expand(String uriTemplate 构建请求的链接,也是拼参数的方法 UriComponentsBuilder builder = UriComponentsBuilder .fromHttpUrl(uriTemplate uriTemplateHandler.expand(uriString , uriVariables); } @Override public URI expand(String uriTemplate , Object... uriVariables) { return uriTemplateHandler.expand(uriTemplate, uriVariables);
= "all")] 5: IEnumerable<Employee> GetAll(); 6: 7: [WebGet(UriTemplate = "{id}")] 8: Employee Get(string id); 9: 10: [WebInvoke(UriTemplate = "/", Method = "POST")] WebGetAttribute和WebInvokeAttribute和均具有相同的属性UriTemplate,该属性用于定义作为最终操作URI的模板。 我们不仅可以通过UriTemplate属性为操作指定一个相对于终结点地址的静态路径,还可以通过占位符实现路径中的动态部分与参数之间的映射。 用于返回指定员工ID的Get操作的UriTemplate被设置成“{id}”,意味着我们直接在表示请求地址的URI中指定员工的ID,而它会自动映射为该操作方法的参数id。
[OperationContract] [WebInvoke(ResponseFormat = WebMessageFormat.Json, Method = "*", UriTemplate [OperationContract] [WebInvoke(Method = "*", ResponseFormat = WebMessageFormat.Json, UriTemplate [OperationContract] [WebInvoke(Method = "*", ResponseFormat = WebMessageFormat.Json, UriTemplate SaveMyData用于Get方法,SaveMyData2用于Post方法 需要说明的是[WebInvoke(Method = "*", ResponseFormat = WebMessageFormat.Json, UriTemplate id={id}&text={text}")]这一行,这里标明该方法可用Get/Post来处理数据,输出格式是Json字符串,UriTemplate表示该方法支持RESTFul风格,可用/MyService.svc
JavaScript,甚至是 HTML FORM,或者直接在浏览器地址栏输入 WCF 中通过 WebGetAttribute、WebInvokeAttribute (GET/PUT/POST/DELETE)、UriTemplate (Name="user")] public interface IService1 { [OperationContract] [WebInvoke(UriTemplate WebMessageBodyStyle.Bare)] string GetData(string value); [OperationContract] [WebInvoke(UriTemplate WebMessageBodyStyle.Bare)] string addPeople(People p); [OperationContract] [WebInvoke(UriTemplate WebMessageBodyStyle.Bare)] List<People> GetList(string value); } } 注意:通过WebInvoke属性的Method值说明该请求的类型,UriTemplate
Downloading github3.py-0.9.5-py2.py3-none-any.whl (109kB): 109kB downloaded Downloading/unpacking uritemplate.py >=0.2.0 (from github3.py) Downloading uritemplate.py-0.3.0.tar.gz Running setup.py (path:/tmp/pip-build-LRgVNm /uritemplate.py/setup.py) egg_info for package uritemplate.py Requirement already satisfied (use --upgrade 2.0 in /usr/lib/python2.7/dist-packages (from github3.py) Installing collected packages: github3.py, uritemplate.py Running setup.py install for uritemplate.py Successfully installed github3.py uritemplate.py Cleaning
request.getURI().toString()))); } /** * Creates a {@code uri} {@code Tag} from the given {@code uriTemplate * @param uriTemplate the template * @return the uri tag */ public static Tag uri( String uriTemplate) { String uri = (StringUtils.hasText(uriTemplate) ? uriTemplate : "none"); return Tag.of("uri", ensureLeadingSlash(stripUri(uri))); 其余的还有 status
打开ContactsAp.cs 复制以下方法 [WebGet(UriTemplate="{id}")] public Contact GetItem(int id) { var contact Close {"ContactId":1,"Name":"Phil Haack"} 3、添加对POST的支持 以下代码是添加一个新的Post方法,添加一个新的Contract [WebInvoke(UriTemplate 添加PUT的支持 添加对PUT和DELETE的支持是非常容易的,像POST一样也是使用WebInvoke 制定PUT和DELETE 打开ConactApi.cs把以下代码拷进去 [WebInvoke(UriTemplate : Close {"ContactId":1,"Name":"Updated Contact" 8、添加对DELETE的支持 切换到ContactApi.cs并复制以下内容: [WebInvoke(UriTemplate
)新建几个方法: Code //取得页面布局数据 [OperationContract] [WebInvoke(ResponseFormat = WebMessageFormat.Json, UriTemplate return ms; } //保存布局 [OperationContract] [WebInvoke(ResponseFormat = WebMessageFormat.Json, UriTemplate Result; } //取得新闻 [OperationContract] [WebInvoke(ResponseFormat = WebMessageFormat.Json, UriTemplate ms; } //取得图片地址 [OperationContract] [WebInvoke(ResponseFormat = WebMessageFormat.Json, UriTemplate