Spring的UriComponents
UriComponents对象是不可变的,expand和encode方法可以返回新的URIComponents
@RequestMapping(value = "/user/{name}")public ResponseEntity<String> uri(HttpServletRequest req, @PathVariable String name, @RequestParam int id) {UriComponents uriComponents = null;uriComponents = UriComponentsBuilder.fromUriString("http://example.com/hotels/{hotel}/bookings/{booking}").build();URI uri = uriComponents.expand("42", "21").encode().toUri();System.out.println(uri.toString());uriComponents = UriComponentsBuilder.newInstance().scheme("http").host("example.com").path("/hotels/{hotel}/bookings/{booking}").build().expand("42", "21").encode();System.out.println(uriComponents.toUri().toString());return null;}
ServletUriComponentsBuilder
@RequestMapping(value = "/user/{name}")public ResponseEntity<String> uri(HttpServletRequest req, @PathVariable String name, @RequestParam int id) {UriComponents uriComponents = null;//重用 host, scheme, port, path 和 queryuriComponents = ServletUriComponentsBuilder.fromRequest(req).replaceQueryParam("id", "{id}")//替换id参数,没有添加上.build().expand("1").encode();System.out.println(uriComponents.toUri().toString());重用 host,porturiComponents = ServletUriComponentsBuilder.fromContextPath(req).path("/accounts")// 用/accouts替换换来的path部分.build();System.out.println(uriComponents.toUri().toString());uriComponents = ServletUriComponentsBuilder.fromServletMapping(req).path("/accounts")//用/accouts替换换来的path部分.build();System.out.println(uriComponents.toUri().toString());return null;}
MvcUriComponentsBuilder
@RequestMapping(value = "/user/{name}")public ResponseEntity<String> uri(HttpServletRequest req, @PathVariable String name, @RequestParam int id) {UriComponents uriComponents = null;uriComponents = MvcUriComponentsBuilder.fromMethodName(URIComponetsController.class, "uri", null, "devin", 21).build();System.out.println(uriComponents.toUri().toString());uriComponents = MvcUriComponentsBuilder.fromMethodCall(on(URIComponetsController.class).uri(null, "devin", 21)).build();System.out.println(uriComponents.toUri().toString());return null;}
如果handler方法返回String,MvcUriComponnetsBuilder的on静态会报错Cannot subclass final class class java.lang.String因为on方法为返回值创建代理对象,String类final的无法代理。
JSP中构建URL(s:mvcUrl方法)
<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false"%>
<%@ taglib uri="http://www.springframework.org/tags" prefix="s" %><html>
<head><title>Home</title>
</head>
<body><a href="${s:mvcUrl('URICC#home').build()}">Click Me</a>
</body>
</html>
@Controller
@RequestMapping(value = "/uri")
public class URIComponentsController {@RequestMapping(value = "/index")public String index(){return "mvc_url";}}
源代码https://github.com/duansiman/SpringMVC
Spring的UriComponents相关推荐
- Spring RestTemplate中几种常见的请求方式
关注公众号[江南一点雨],专注于 Spring Boot+微服务以及前后端分离等全栈技术,定期视频教程分享,关注后回复 Java ,领取松哥为你精心准备的 Java 干货! 在Spring Cloud ...
- UriComponentsBuilder和UriComponents url编码
Spring MVC 提供了一种机制,可以构造和编码URI -- 使用UriComponentsBuilder和UriComponents. 功能相当于 urlencode()函数,对url进行编码, ...
- Spring RestTemplate中几种常见的请求方式GET请求 POST请求 PUT请求 DELETE请求
Spring RestTemplate中几种常见的请求方式 原文地址: https://blog.csdn.net/u012702547/article/details/77917939 版权声明:本 ...
- spring restTemplate使用方法
https://github.com/lenve/SimpleSpringCloud/tree/master/RestTemplate在Spring Cloud中服务的发现与消费一文中,当我们从服务消 ...
- 关于Spring框架(官方文档)
反应性堆栈上的Web Version 5.1.0.RELEASE 目录 1. Spring WebFlux 1.9.反应铁心 1.10.使用DispatcherHandler 1.11.带注释控制器 ...
- Spring Web(第一部分)
1. Spring Web MVC Spring Web MVC是在Servlet API上构建的原始Web框架,从一开始就包含在Spring框架中.其正式名称"Spring Web MVC ...
- Spring系列之Spring Web MVC-20
目录 Spring Web MVC DispatcherServlet 上下文层次结构 特殊Bean Web MVC 配置 程序配置 工作原理 异常 视图解析 配置 重定向 转发 内容协商 过滤器 F ...
- 使用Spring boot开发一个接口 - TODO应用中新建一个TASK
spring initializr spring boot官方提供了很方便的初始化项目的方法,即spring initializr.可以直接在IDEA中创建,或在https://start.sprin ...
- Spring Boot: SpringFox Swagger原理解析及简单实用
文章目录 简介 一.Swagger简单使用 二.Swagger原理 三.Swagger架构分析及组成 其他 简介 API的全称是应用编程接口(Application Programming Inter ...
最新文章
- hp远程桌面服务器,hp服务器通过ilo远程安装操作系统
- 青海省计算机应用能力考试,青海省2015年职称计算机应用能力考试西宁考区四月份考试安排通知...
- java 搜索机制_Java爬虫搜索原理实现
- 152.信道划分及其典型应用
- 万字长文详解文本抽取:从算法理论到实践
- ReactNative--React简介
- 设置su为不需要密码切换为root
- 前端学习(3204):复习类相关知识2
- Android Webservices 返回多行多列数据(Dataset)
- Android 内存泄漏分析与解决方法
- wireshark抓包QQ查IP
- 阿里云企业邮箱的imap和pop3设置
- C#支付宝扫码支付代码完整版
- Kruskal vs Borůvka
- Predicting drug–disease associations through layer attention graph convolutional network 论文解析
- arc用matlab表示,arctanx在matlab中怎么表示?
- ubuntu16 octomap安装(解决安装octomap后没有octovis问题)
- lol服务器维护8月6日,LOL8月6日维护到几点 英雄联盟9.15新版本更新维护公告
- 一点Scala学习资源
- 科技幻想(一)——便捷人机交互
热门文章
- linux下安装python3报错_Linux中安装python3
- python美化excel_简单介绍python在CMD界面读取excel所有数据
- 华为开发者大会主题演讲:3D建模服务让内容高效生产
- 华硕电脑重装系统后fn组合键部分屏幕无显示
- 计算机视觉 开源_年轻的计算机科学家分享了她的开源故事
- centos7是linux内核,在CentOS 7上用源代码编译最新的Linux内核
- 智能手机查看上网IP地址的多种方法
- mysql主从和redis主从
- iPhone 5s 如何进入 DFU 模式,macOS 无法进入 DFU 模式
- Linux 基础总结,这一篇就够了!