Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 720
Learner Avatar asked May 15 '16 04:05

Learner


2 Answers

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.

like image 144
Ramanlfc Avatar answered Oct 23 '22 05:10

Ramanlfc


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

like image 21
Jonathan L Avatar answered Oct 23 '22 03:10

Jonathan L