使用Java(JAX-RS)减少REStful中的URI路径

使用Java(JAX-RS)减少REStful中的URI路径,java,apache,rest,jax-rs,web.xml,Java,Apache,Rest,Jax Rs,Web.xml,我是java和JAX-RS的新手。我已经为简单的应用程序开发了工作示例 <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns

我是java和JAX-RS的新手。我已经为简单的应用程序开发了工作示例

     <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
         version="3.1">
         <servlet>
           <servlet-name>Eshipper API Application</servlet-name>
           <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
              <init-param>
                 <param-name>jersey.config.server.provider.packages</param-name>
                 <param-value>com.eshipper.client.request</param-value>
              </init-param>
           </servlet>
        <servlet-mapping>
        <servlet-name>Eshipper API Application</servlet-name>
           <url-pattern>/apis/*</url-pattern>
        </servlet-mapping>  
     </web-app>
现在我想在RESTFul中减少我的url

     <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
         version="3.1">
         <servlet>
           <servlet-name>Eshipper API Application</servlet-name>
           <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
              <init-param>
                 <param-name>jersey.config.server.provider.packages</param-name>
                 <param-value>com.eshipper.client.request</param-value>
              </init-param>
           </servlet>
        <servlet-mapping>
        <servlet-name>Eshipper API Application</servlet-name>
           <url-pattern>/apis/*</url-pattern>
        </servlet-mapping>  
     </web-app>
目前我有
http://localhost:8084/eshipper_api/apis/api/get_quote
此url

     <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
         version="3.1">
         <servlet>
           <servlet-name>Eshipper API Application</servlet-name>
           <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
              <init-param>
                 <param-name>jersey.config.server.provider.packages</param-name>
                 <param-value>com.eshipper.client.request</param-value>
              </init-param>
           </servlet>
        <servlet-mapping>
        <servlet-name>Eshipper API Application</servlet-name>
           <url-pattern>/apis/*</url-pattern>
        </servlet-mapping>  
     </web-app>
它的工作和输出

     <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
         version="3.1">
         <servlet>
           <servlet-name>Eshipper API Application</servlet-name>
           <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
              <init-param>
                 <param-name>jersey.config.server.provider.packages</param-name>
                 <param-value>com.eshipper.client.request</param-value>
              </init-param>
           </servlet>
        <servlet-mapping>
        <servlet-name>Eshipper API Application</servlet-name>
           <url-pattern>/apis/*</url-pattern>
        </servlet-mapping>  
     </web-app>
我想减少它,想把它变成
http://localhost:8084/eshipper_api/apis/get_quote

     <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
         version="3.1">
         <servlet>
           <servlet-name>Eshipper API Application</servlet-name>
           <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
              <init-param>
                 <param-name>jersey.config.server.provider.packages</param-name>
                 <param-value>com.eshipper.client.request</param-value>
              </init-param>
           </servlet>
        <servlet-mapping>
        <servlet-name>Eshipper API Application</servlet-name>
           <url-pattern>/apis/*</url-pattern>
        </servlet-mapping>  
     </web-app>
Myweb.xml

     <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
         version="3.1">
         <servlet>
           <servlet-name>Eshipper API Application</servlet-name>
           <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
              <init-param>
                 <param-name>jersey.config.server.provider.packages</param-name>
                 <param-value>com.eshipper.client.request</param-value>
              </init-param>
           </servlet>
        <servlet-mapping>
        <servlet-name>Eshipper API Application</servlet-name>
           <url-pattern>/apis/*</url-pattern>
        </servlet-mapping>  
     </web-app>
请帮我做这个。 提前感谢。

将您的
@Path(“api”)
更改为
@Path(“”)

     <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
         version="3.1">
         <servlet>
           <servlet-name>Eshipper API Application</servlet-name>
           <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
              <init-param>
                 <param-name>jersey.config.server.provider.packages</param-name>
                 <param-value>com.eshipper.client.request</param-value>
              </init-param>
           </servlet>
        <servlet-mapping>
        <servlet-name>Eshipper API Application</servlet-name>
           <url-pattern>/apis/*</url-pattern>
        </servlet-mapping>  
     </web-app>
或者,您可以将web.xml更改为:

     <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
         version="3.1">
         <servlet>
           <servlet-name>Eshipper API Application</servlet-name>
           <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
              <init-param>
                 <param-name>jersey.config.server.provider.packages</param-name>
                 <param-value>com.eshipper.client.request</param-value>
              </init-param>
           </servlet>
        <servlet-mapping>
        <servlet-name>Eshipper API Application</servlet-name>
           <url-pattern>/apis/*</url-pattern>
        </servlet-mapping>  
     </web-app>
/*
并设置您的
@路径(“/api”)
,它也会做同样的事情

     <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
         version="3.1">
         <servlet>
           <servlet-name>Eshipper API Application</servlet-name>
           <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
              <init-param>
                 <param-name>jersey.config.server.provider.packages</param-name>
                 <param-value>com.eshipper.client.request</param-value>
              </init-param>
           </servlet>
        <servlet-mapping>
        <servlet-name>Eshipper API Application</servlet-name>
           <url-pattern>/apis/*</url-pattern>
        </servlet-mapping>  
     </web-app>
你有你的
http://servername/
上下文
/
web.xml servlet路径
/
类注释路径
/
方法注释路径

     <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
         version="3.1">
         <servlet>
           <servlet-name>Eshipper API Application</servlet-name>
           <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
              <init-param>
                 <param-name>jersey.config.server.provider.packages</param-name>
                 <param-value>com.eshipper.client.request</param-value>
              </init-param>
           </servlet>
        <servlet-mapping>
        <servlet-name>Eshipper API Application</servlet-name>
           <url-pattern>/apis/*</url-pattern>
        </servlet-mapping>  
     </web-app>