How to specify multiple package names for service classes in REST web service using Jersey
Asked Answered
A

3

17

How do I specify multiple <param-value> for a given <param-name> under servlet's <init-param> tag. Following is my web.xml file:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>com.vogella.jersey.first</display-name>
 <servlet>
    <servlet-name>Jersey REST Service</servlet-name>
    <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
     <!-- Register resources and providers under com.vogella.jersey.first package. -->
    <init-param>
        <param-name>jersey.config.server.provider.packages</param-name>
        <param-value>com.vogella.jersey.first</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>Jersey REST Service</servlet-name>
    <url-pattern>/rest/*</url-pattern>
  </servlet-mapping>
</web-app> 

The parameter jersey.config.server.provider.packages defines in which package Jersey will look for the web service classes.

I want to specify multiple package names here, as my service classes are in different packages. Something like following, but none of them works:

1) Multiple <param-value>

<init-param>
    <param-name>jersey.config.server.provider.packages</param-name>
    <param-value>com.vogella.jersey.first</param-value>
    <param-value>com.vogella.jersey.second</param-value>
</init-param>

2) Multiple <init-param>

<servlet>
    ...
    <init-param>
        <param-name>jersey.config.server.provider.packages</param-name>
        <param-value>com.vogella.jersey.first</param-value>
    </init-param>
    <init-param>
        <param-name>jersey.config.server.provider.packages</param-name>
        <param-value>com.vogella.jersey.second</param-value>
    </init-param>

How do I do this?

Aril answered 15/5, 2016 at 4:1 Comment(1)
FYI, the scanning is recursive. i.e. com.vogella.jersey will scan both the above packagesTope
T
28

You can't specify multiple <param-value> for a single <param-name>.

Use this instead:

<init-param>
    <param-name>jersey.config.server.provider.packages</param-name>
    <param-value>com.pack.first;com.pack.rest.second</param-value>
</init-param>

; and , can be used as a separators.

Theater answered 15/5, 2016 at 4:5 Comment(5)
+1 Perfect, thanks. Could you please suggest which tutorials can I follow/read to get more details like this? Would this be covered under Servlet tutorials?Aril
@Aril Best place is the Jersey documentationTope
@Aril youtube.com/user/koushks/playlists , there are 2 excellent playlists here for jax-rs.Theater
what if I want to include all packages ?Happ
what if I want to include packages as well as classes for <param-name>?Reginareginald
G
1

Credit to Ramanlfc's answer, you can use "," as package separator too. For example:

<init-param>
    <param-name>jersey.config.server.provider.packages</param-name>
    <param-value>com.pkg.first,com.pkg.second</param-value>
</init-param>

Tested under Ubuntu 16.04.2, tomcat-8.5.11

Gualterio answered 10/5, 2017 at 3:36 Comment(0)
T
0

There are several ways for you to specify multiple package names for service classes in REST web service using Jersey. In addition to the answer that mentioned by Ramanlfc, \n also works.

<servlet>
    <servlet-name>Jersey REST Service</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.demo1.resources
            com.demo2.resources
        </param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>Jersey REST Service</servlet-name>
    <url-pattern>/rest/*</url-pattern>
</servlet-mapping>
Treasure answered 22/12, 2017 at 2:8 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.