Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

 

 

 

...

Span
stylefont-size:2em;font-weight:bold
JAX-RS : Services Configuration

...

 

 


Table of Contents

Configuring JAX-RS services programmatically

...

Code Block
xml
xml
<beans xmlns="http://www.springframework.org/schema/beans"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xmlns:jaxrs="http://cxf.apache.org/jaxrs"
      xsi:schemaLocation="
         http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
         http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.xsd">

     <jaxrs:server id="customerService" address="/customers">
        <jaxrs:serviceBeans>
           <ref bean="serviceBean"/>
        </jaxrs:serviceBeans>
     </jaxrs:server>
      
     <bean id="serviceBean" class="service.CustomerService"/> 
</beans>

Spring Boot

Example1:

 

Code Block
languagejava
package sample.rs.service;
import org.apache.cxf.endpoint.Server;
import org.apache.cxf.jaxrs.JAXRSServerFactoryBean;
import org.apache.cxf.jaxrs.spring.JaxRsConfig;
import org.apache.cxf.transport.servlet.CXFServlet;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.embedded.ServletRegistrationBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Import;

@SpringBootApplication
@Import(JaxRsConfig.class)
public class SampleRestApplication {
    public static void main(String[] args) {
        SpringApplication.run(SampleRestApplication.class, args);
    }
 
    @Bean
    public ServletRegistrationBean servletRegistrationBean(ApplicationContext context) {
        return new ServletRegistrationBean(new CXFServlet(), "/services/*");
    }
 
    
    @Bean
    public Server rsServer() {
        JAXRSServerFactoryBean endpoint = new JAXRSServerFactoryBean();
        endpoint.setServiceBean(new HelloService());
        endpoint.setAddress("/helloservice");
        return endpoint.create();
    }
 
}

 

Example2:

 

Code Block
languagejava
package sample.rs.service;
import org.apache.cxf.jaxrs.spring.SpringComponentScanServer;
import org.apache.cxf.transport.servlet.CXFServlet;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.embedded.ServletRegistrationBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Import;


@SpringBootApplication
@Import(SpringComponentScanServer.class)
public class SampleScanRestApplication {
    public static void main(String[] args) {
        SpringApplication.run(SampleScanRestApplication.class, args);
    }


    @Bean
    public ServletRegistrationBean servletRegistrationBean(ApplicationContext context) {
        return new ServletRegistrationBean(new CXFServlet(), "/services/helloservice/*");
    }


    
    @Bean
    public HelloService helloService() {
        return new HelloService();
    }

}

 

Example3:

 

Code Block
languagejava
/**
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements. See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership. The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */
package sample.rs.service;
import java.util.Collections;
import java.util.Set;

import javax.servlet.ServletConfig;
import javax.ws.rs.ApplicationPath;
import javax.ws.rs.core.Application;

import org.apache.cxf.jaxrs.servlet.CXFNonSpringJaxrsServlet;
import org.apache.cxf.jaxrs.spring.JaxRsConfig;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.embedded.ServletRegistrationBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Import;

@SpringBootApplication
@Import(JaxRsConfig.class)
public class SampleScanRestApplication2 {
    public static void main(String[] args) {
        SpringApplication.run(SampleScanRestApplication2.class, args);
    }
 
    @Bean
    public ServletRegistrationBean servletRegistrationBean(ApplicationContext context) {
        Application app = (Application)context.getBean("helloApp");
        @SuppressWarnings("serial")
        CXFNonSpringJaxrsServlet servlet = new CXFNonSpringJaxrsServlet(app) {
            @Override
            protected boolean isIgnoreApplicationPath(ServletConfig servletConfig) {
                return false;
            }
            
        };
        return new ServletRegistrationBean(servlet, "/*");
    }
 
    
    @Bean
    public Application helloApp() {
        return new JaxrsApplication();
    }

    @ApplicationPath("/services/helloservice")
    public static class JaxrsApplication extends Application { 
        public Set<Object> getSingletons() {
            return Collections.<Object>singleton(new HelloService());
        }
    }
    
}

 

 

 

Please also check the classes in this package and this demo.

Please see CXF SpringBoot documentation. 

Please see JAX-RS Spring Boot and JAX-RS Spring Boot Scan demos.

Please see JAXRSClientSpringBoot documentation on how CXF JAX-RS Clients can be used in a SpringBoot Application. (Here is a demo for JAX-WS users).

 

Configuring JAX-RS endpoints programmatically without Spring

...

Code Block
xml
xml
<?xml version="1.0" encoding="ISO-8859-1"?>

<!DOCTYPE web-app
    PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
    "http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
	<servlet>
		<servlet-name>CXFServlet1</servlet-name>
		<display-name>CXF Servlet1</display-name>
		<servlet-class>
			org.apache.cxf.transport.servlet.CXFServlet
		</servlet-class>
                <init-param>
                   <param-name>config-location</param-name>
                   <param-value>/WEB-INF/beans1.xml</param-value>
                </init-param>
		<load-on-startup>1</load-on-startup>
	</servlet>

        <servlet>
		<servlet-name>CXFServlet2</servlet-name>
		<display-name>CXF Servlet2</display-name>
		<servlet-class>
			org.apache.cxf.transport.servlet.CXFServlet
		</servlet-class>
                <init-param>
                   <param-name>config-location</param-name>
                   <param-value>/WEB-INF/beans2.xml</param-value>
                </init-param>
		<load-on-startup>1</load-on-startup>
	</servlet>


	<servlet-mapping>
		<servlet-name>CXFServlet1</servlet-name>
		<url-pattern>/1/*</url-pattern>
	</servlet-mapping>

        <servlet-mapping>
		<servlet-name>CXFServlet2</servlet-name>
		<url-pattern>/2/*</url-pattern>
	</servlet-mapping>
</web-app>

...

Code Block
xml
xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">

  <import resource="classpath:META-INF/cxf/cxf.xml" />
  <import resource="classpath:META-INF/cxf/cxf-servlet.xml" />

  <bean class="org.apache.cxf.jaxrs.JAXRSServerFactoryBean" init-method="create">
    <property name="address" value="/service1"/>
    <property:serviceBeans> name="serviceBeans">
      <ref bean="customerBean" />
    </property:serviceBeans>property>
  </jaxrs:server>bean>

  <bean id="customerBean" class="demo.jaxrs.server.CustomerService" />
</beans>

...

Prototype beans will have their postconstruct and predestroy method called before a resource method is invoked and immediately after the invocation has returned but before the response has actually been serialized. You can indicate that the predestroy method has to be called after the request has completely gone out of scope (that is after the response body if any has been written to the output stream) by adding an "org.apache.cxf.jaxrs.service.scope" property with the value set to "request".

...

Code Block
xml
xml
<beans>
  
  <cxf:bus>
        <cxf:properties>
            <entry key="javax.ws.rs.ext.ExceptionMapper" value-ref="exceptionMapper"/>
        </cxf:properties>
  </cxf:bus

  <bean id="exceptionMapper" class="org.apache.cxf.systest.jaxrs.RuntimeExceptionMapper"/>

  <jaxrs:server id="customerService" address="/service1">
    <jaxrs:serviceBeans>
      <bean class="org.apache.cxf.systest.jaxrs.CustomerService"/>
    </jaxrs:serviceBeans>
  </jaxrs:server>

  <jaxrs:server id="customerService" address="/service2">
    <jaxrs:serviceBeans>
      <bean class="org.apache.cxf.systest.jaxrs.CustomerService2"/>
    </jaxrs:serviceBeans>
  </jaxrs:server>
  
  </beans>

...

Code Block
xml
xml
<beans xmlns:util="http://www.springframework.org/schema/util">
  
  <cxf:bus>
        <cxf:properties>
            <entry key="org.apache.cxf.jaxrs.bus.providers" value-ref="busProviders"/>
        </cxf:properties>
  </cxf:bus

  <util:list id="busProviders">
    <ref bean="exceptionMapper"/>
    <ref bean="customMessageBodyReader"/>
    <ref bean="customMessageBodyWriter"/>
  </util:list>

  <bean id="exceptionMapper" class="org.apache.cxf.systest.jaxrs.RuntimeExceptionMapper"/>
  <bean id="customMessageBodyReader" class="org.apache.cxf.systest.jaxrs.CustomReader"/>
  <bean id="customMessageBodyWriter" class="org.apache.cxf.systest.jaxrs.CustomWriter"/> 

  <jaxrs:server id="customerService" address="/service1">
    <jaxrs:serviceBeans>
      <bean class="org.apache.cxf.systest.jaxrs.CustomerService"/>
    </jaxrs:serviceBeans>
  </jaxrs:server>

  <jaxrs:server id="customerService" address="/service2">
    <jaxrs:serviceBeans>
      <bean class="org.apache.cxf.systest.jaxrs.CustomerService2"/>
    </jaxrs:serviceBeans>
  </jaxrs:server>
  
  </beans>

...

Code Block
xml
xml
{code:xml}
<servlet>
 <servlet-name>CXFServlet</servlet-name>
 <display-name>CXF Servlet</display-name>
 <servlet-class>
     org.apache.cxf.transport.servlet.CXFServlet
 </servlet-class>
 <init-param>
    <param-name>config-location</param-name>
    <param-value>/WEB-INF/beans1.xml</param-value>
 </init-param>
 <init-param>
    <param-name>disable-address-updates</param-name>
    <param-value>true</param-value>
 </init-param>
 <load-on-startup>1</load-on-startup>
</servlet>
Code Block
 


Auto-discovery of root resources and providers

...