How to mix server-side Jax-rs call with native files without prefix?

前端 未结 2 1313
攒了一身酷
攒了一身酷 2021-01-15 00:27

We are currently using a Jersey JAX-RS implementation to handle our REST requests (server-side Jersey). Our web.xml file is configured so all /rest/* requests are handled by

2条回答
  •  滥情空心
    2021-01-15 00:55

    First, when it comes to servlet mapping, priority goes like this:

    1. Path mapping: /rest/*
    2. Extension mapping: *.png
    3. Default: /

    Path mapping + extension mapping: bad idea

    So you can't map static resources by their file extension within an existing path mapping (that would be a bad idea anyway, managing all static file extensions in your web.xml).

    Path mapping's priority > ext mapping.

    Using a filter

    Set up an UrlRewriteFilter in your web.xml

    
        UrlRewriteFilter
        org.tuckey.web.filters.urlrewrite.UrlRewriteFilter
    
    
        UrlRewriteFilter
        /*
    
    

    And in your urlrewrite.xml configuration file:

    
        
            /rest/images/**
            /images/$1
        
    
    

    Not mapping static resources in /rest

    This may not suit your needs, but this is my favorite! Mapping static resources within /rest says "Hey, developer, come GET/POST/PUT/DELETE thoses static resources, it's okay":

    • If you can't GET/PUT/POST/DELETE those resources, then you shouldn't map them in /rest. It gives the wrong impression.
    • If ou actually want to manipulate those resources through a REST webservice, then let Jersey do the heavy lifting and serve those resources (check out the jersey-samples for an example).

提交回复
热议问题