tomcatweb.xmlwebapitomcat9

Tomcat 9 add webapi at runtime


I don't know if it's possibile . When I create a webapi I must declare in web.xml the related class/url

  <servlet-mapping>
        <servlet-name>myServlet</servlet-name>
        <url-pattern>/myServlet.htm</url-pattern>
        <!-- WebApi -->
        <url-pattern>/Timbrature2/*</url-pattern>
        <url-pattern>/Timbrature2.json/*</url-pattern>
        <url-pattern>/Timbrature2.xml/*</url-pattern>
        <url-pattern>/Timbrature/*</url-pattern>
        <url-pattern>/Timbrature.json/*</url-pattern>
        <url-pattern>/Timbrature.xml/*</url-pattern>
        <url-pattern>/TimbratureMobile/*</url-pattern>
        <url-pattern>/TimbratureMobile.json/*</url-pattern>
        <url-pattern>/TimbratureMobile.xml/*</url-pattern>
        <url-pattern>/$metadata</url-pattern>
    </servlet-mapping>

but if my webapp load from jars dynamic code can I add a "new" class to be used as a REST endpoint?

Read Apache documentation, tryied various configurations but nothing


Solution

  • If you want one servlet to handle everything, you can register it to broad URL patterns like

    <url-pattern>/api/*</url-pattern>
    

    It's worth reading the Servlet Specification and specifically section 12.2 (in the 4.0 release of the spec) which covers "Specification of Mappings". This describes the kinds of url-mappings which are possible.

    Another option is to use @WebServlet annotations in your code (covered in section 8.1.1 of the above document) to establish a mapping from within your code. I have a preference for the declarative mapping found in web.xml because all mappings are co-located rather than spread out across arbitrary source files whose source may not always be available.