You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@cxf.apache.org by "Muller, Anthony" <an...@sap.com> on 2011/06/10 15:45:25 UTC

CXFNonSpringJaxrsServlet and JAX-RS questions

Hello,

I have quite basic questions about JAX-RS support in CXF (2.3.3) :

- If you look at servlet code and web.xml descriptor bellow, is it the good way to expose a REST ws (without using spring)
- How to remove "sf.setAddress("http://localhost:8080/")" ? => I don't know what will be the port number of the app server where my webapp will be deployed
- How to remove useless <init-param> for serviceClasses without runtime exception

Thanks and regards,
Anthony



Servlet code:

public class MyServlet extends CXFNonSpringJaxrsServlet {

	public MyServlet() {
        JAXRSServerFactoryBean sf = new JAXRSServerFactoryBean();
        sf.setTransportId("http://cxf.apache.org/transports/http");
        sf.setResourceClasses(MyResource.class);
        sf.setResourceProvider(MyResource.class, new SingletonResourceProvider(new MyResource()));
        sf.setAddress("http://localhost:8080/");
        sf.create();
	}
}

web.xml:

<?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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
  <servlet>
    <servlet-name>MyServlet</servlet-name>
    <servlet-class>com .... MyServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
 	<init-param>
  		<param-name>jaxrs.serviceClasses</param-name>
  		<param-value>com ... MyResource</param-value>
 	</init-param>
  </servlet>
    
  <servlet-mapping>
    <servlet-name> MyServlet </servlet-name>
    <url-pattern>/MyResource/*</url-pattern>
  </servlet-mapping>
</web-app>

RE: CXFNonSpringJaxrsServlet and JAX-RS questions

Posted by "Muller, Anthony" <an...@sap.com>.
Ok, I think I understand!

Thanks Sergey,
Anthony

-----Original Message-----
From: Sergey Beryozkin [mailto:sberyozkin@gmail.com] 
Sent: vendredi 10 juin 2011 17:02
To: users@cxf.apache.org
Subject: Re: CXFNonSpringJaxrsServlet and JAX-RS questions

Hi

On Fri, Jun 10, 2011 at 2:45 PM, Muller, Anthony <an...@sap.com> wrote:
> Hello,
>
> I have quite basic questions about JAX-RS support in CXF (2.3.3) :
>
> - If you look at servlet code and web.xml descriptor bellow, is it the good way to expose a REST ws (without using spring)

The code which is currently in MyServlet is usually used only when
creating standalone JAX-RS endpoints, backed up by embedded Jetty - in
such cases absolute URIs have to specified.
I'd recommend using CXFNonSpringJaxrsServlet directly, unless you need
to customize the way CXFNonSpringJaxrsServlet handles its init().

> - How to remove "sf.setAddress("http://localhost:8080/")" ? => I don't know what will be the port number of the app server where my webapp will be deployed

I think you do not need that code at all

> - How to remove useless <init-param> for serviceClasses without runtime exception
>

At the moment MyServlet does not really override
CXFNonSpringJaxrsServlet but rather proceeds with creating endpoints
on its own, thus a possible conflict, given that
CXFNonSpringJaxrsServlet does expect this parameter.
Consider removing MyServlet.

Hope it helps
Sergey

> Thanks and regards,
> Anthony
>
>
>
> Servlet code:
>
> public class MyServlet extends CXFNonSpringJaxrsServlet {
>
>        public MyServlet() {
>        JAXRSServerFactoryBean sf = new JAXRSServerFactoryBean();
>        sf.setTransportId("http://cxf.apache.org/transports/http");
>        sf.setResourceClasses(MyResource.class);
>        sf.setResourceProvider(MyResource.class, new SingletonResourceProvider(new MyResource()));
>        sf.setAddress("http://localhost:8080/");
>        sf.create();
>        }
> }
>
> web.xml:
>
> <?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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
>  <servlet>
>    <servlet-name>MyServlet</servlet-name>
>    <servlet-class>com .... MyServlet</servlet-class>
>    <load-on-startup>1</load-on-startup>
>        <init-param>
>                <param-name>jaxrs.serviceClasses</param-name>
>                <param-value>com ... MyResource</param-value>
>        </init-param>
>  </servlet>
>
>  <servlet-mapping>
>    <servlet-name> MyServlet </servlet-name>
>    <url-pattern>/MyResource/*</url-pattern>
>  </servlet-mapping>
> </web-app>
>



-- 
Sergey Beryozkin

Application Integration Division of Talend
http://sberyozkin.blogspot.com

Re: CXFNonSpringJaxrsServlet and JAX-RS questions

Posted by Sergey Beryozkin <sb...@gmail.com>.
Hi

On Fri, Jun 10, 2011 at 2:45 PM, Muller, Anthony <an...@sap.com> wrote:
> Hello,
>
> I have quite basic questions about JAX-RS support in CXF (2.3.3) :
>
> - If you look at servlet code and web.xml descriptor bellow, is it the good way to expose a REST ws (without using spring)

The code which is currently in MyServlet is usually used only when
creating standalone JAX-RS endpoints, backed up by embedded Jetty - in
such cases absolute URIs have to specified.
I'd recommend using CXFNonSpringJaxrsServlet directly, unless you need
to customize the way CXFNonSpringJaxrsServlet handles its init().

> - How to remove "sf.setAddress("http://localhost:8080/")" ? => I don't know what will be the port number of the app server where my webapp will be deployed

I think you do not need that code at all

> - How to remove useless <init-param> for serviceClasses without runtime exception
>

At the moment MyServlet does not really override
CXFNonSpringJaxrsServlet but rather proceeds with creating endpoints
on its own, thus a possible conflict, given that
CXFNonSpringJaxrsServlet does expect this parameter.
Consider removing MyServlet.

Hope it helps
Sergey

> Thanks and regards,
> Anthony
>
>
>
> Servlet code:
>
> public class MyServlet extends CXFNonSpringJaxrsServlet {
>
>        public MyServlet() {
>        JAXRSServerFactoryBean sf = new JAXRSServerFactoryBean();
>        sf.setTransportId("http://cxf.apache.org/transports/http");
>        sf.setResourceClasses(MyResource.class);
>        sf.setResourceProvider(MyResource.class, new SingletonResourceProvider(new MyResource()));
>        sf.setAddress("http://localhost:8080/");
>        sf.create();
>        }
> }
>
> web.xml:
>
> <?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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
>  <servlet>
>    <servlet-name>MyServlet</servlet-name>
>    <servlet-class>com .... MyServlet</servlet-class>
>    <load-on-startup>1</load-on-startup>
>        <init-param>
>                <param-name>jaxrs.serviceClasses</param-name>
>                <param-value>com ... MyResource</param-value>
>        </init-param>
>  </servlet>
>
>  <servlet-mapping>
>    <servlet-name> MyServlet </servlet-name>
>    <url-pattern>/MyResource/*</url-pattern>
>  </servlet-mapping>
> </web-app>
>



-- 
Sergey Beryozkin

Application Integration Division of Talend
http://sberyozkin.blogspot.com