如何在Java中构建URL或URI?是否有惯用的方式,或者很容易做到这一点的库?
我需要允许从请求字符串开始,解析/更改各种URL部分(方案,主机,路径,查询字符串)并支持添加和自动编码查询参数.
我正在Spring MVC控制器中构建一个新的URL,以传递回客户端.目前我正在尝试这个:
// httpRequest is the current HttpServletRequest
new URL(httpRequest.getProtocol(),
httpRequest.getServerName(),
httpRequest.getServerPort(),
httpRequest.getContextPath().concat("/foo/bar.html"));
Run Code Online (Sandbox Code Playgroud)
问题是httpRequest.getProtocol()给我"HTTP/1.1"而不仅仅是"HTTP".我可以修剪它,但想知道是否有更优雅的方式.