سؤال

Can somebody please explain in plain English how this works (or at least why my structure doesn't)? I want a function called ExportLicenseInfo in my Jersey servlet resource, which I have called ExportResource, to map to http://example.com/myApp/export/software_licenses.{year}-{month}.{format}, for example: http://example.com/myApp/export/software_licenses_2013-10.csv

Servlet config in web.xml:

<servlet>
    <servlet-name>ExportServlet</servlet-name>
    <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>

    <init-param>
        <param-name>javax.ws.rs.Application</param-name>
        <param-value>com.mycompany.app.ExportApplication</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>

My servlet mapping in web.xml:

<servlet-mapping>
    <servlet-name>ExportServlet</servlet-name>
    <url-pattern>/export</url-pattern>
</servlet-mapping>

My code with @Path annotations:

@Path("/export")
public class ExportResource {

    ...

    @GET
    @Produces({"text/csv", "application/json"})
    @Path("/software_licenses_{year: [0-9][0-9][0-9][0-9]}-{month: [0-1][0-9]}.{format}")
    public String ExportLicenseInfo( ... ) {
    ...
    }

When I try to access the resource, I get a 404. Jersey trace log:

Feb 21, 2014 2:41:46 PM org.glassfish.jersey.filter.LoggingFilter log
INFO: 6 * LoggingFilter - Request received on thread http-bio-8080-exec-67
6 > GET http://localhost:8080/app/export/software_licenses_2013-10.csv
6 > host: localhost:8080
6 > connection: keep-alive
6 > cache-control: max-age=0
6 > accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
6 > user-agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/32.0.1700.107 Safari/537.36
6 > accept-encoding: gzip,deflate,sdch
6 > accept-language: en
6 > cookie: JSESSIONID=DB64E0B066BDEE8CABFC94686AD6ACDC.test; JSESSIONIDSSO=61B51CEED61F8F6CE8A8DB46B38BAC9F; i18next=en

Feb 21, 2014 2:41:46 PM org.glassfish.jersey.filter.LoggingFilter log
INFO: 6 * LoggingFilter - Response received on thread http-bio-8080-exec-67
6 < 404
هل كانت مفيدة؟

المحلول

Try mapping the jersey servlet like this:

<servlet-mapping>
<servlet-name>ExportServlet</servlet-name>
<url-pattern>/export/*</url-pattern>
</servlet-mapping>
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top