Remove webjars version from url

We use webjars with maven in our project. So, we have hundreds of JSPs with code like this:

<%--JSP CODE--%> <script src="<c:url value="/webjars/jquery/2.2.0/jquery.min.js" />" type="text/javascript"></script> <script src="<c:url value="/webjars/bootstrap/3.3.6/js/bootstrap.min.js" />" type="text/javascript"></script> ......... 

And as you might guess, this is a cumbersome job of upgrading to newer versions of webjars. Therefore, I am looking for a solution that will allow me to import scripts as follows:

 <%--JSP CODE--%> <script src="<c:url value="/webjars/jquery/jquery.min.js" />" type="text/javascript"></script> <script src="<c:url value="/webjars/bootstrap/js/bootstrap.min.js" />" type="text/javascript"></script> ......... 

Basically, I want to remove the webjar version from url. Can you offer a good and simple solution to this problem?

So far, I have come up with a resource servlet that can guess which file needs to be returned at the URL. But this solution includes a full scan of resources at the beginning of the application.

+5
source share
1 answer

Take a look at the webjars-locator project, you can use it to create the right query controller.

In case of using Spring MVC it will be:

 @ResponseBody @RequestMapping("/webjarslocator/{webjar}/**") public ResponseEntity locateWebjarAsset(@PathVariable String webjar, HttpServletRequest request) { try { String mvcPrefix = "/webjarslocator/" + webjar + "/"; // This prefix must match the mapping path! String mvcPath = (String) request.getAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE); String fullPath = assetLocator.getFullPath(webjar, mvcPath.substring(mvcPrefix.length())); return new ResponseEntity(new ClassPathResource(fullPath), HttpStatus.OK); } catch (Exception e) { return new ResponseEntity<>(HttpStatus.NOT_FOUND); } } 

Disclaimer: This is the code from the WebJars documentation (section Making dependencies version agnostic ).

In this case, you can request js libraries as follows:

 <link rel='stylesheet' href='/webjarslocator/bootstrap/css/bootstrap.min.css'> 

Please note that there is no version in this URL.

You can also try to optimize these requests (and therefore scan the file system) using the cache, but I'm pretty sure that some kind of cache is already involved in the webjars-locator (I did not check this).

+5
source

Source: https://habr.com/ru/post/1243506/


All Articles