Java – how to build dynamic URLs in spring MVC

I tried to send a URL that I would generate based on some dynamic values But I don't want to hard code it, nor do I want to use response or request objects

Example:- http://localhost:8585/app/image/ {ID} / {publicurl} / {filename}

So I want to get the first part, that is http://localhost:8585/app/image/ Only from the spring framework I will provide other things, such as ID, publicurl, filename, so that it can generate a complete absolute URL

How to do this in spring MVC

I am using spring MVC, spring data, spring rest, hibernate

Solution

Are you trying to listen for URLs or trying to build URLs for external use?

If it is the latter, you can use the URI components builder to build dynamic URLs in spring Example:

UriComponents uri = UriComponentsBuilder
                    .fromHttpUrl("http://localhost:8585/app/image/{id}/{publicUrl}/{filename}")
                    .buildAndExpand("someId","somePublicUrl","someFilename");

String urlString = uri.toUriString();@H_404_19@
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>