vendredi 17 avril 2015

Apache CXF servlet /w Eclipse

Why can't I get CXF servlet to work. I started a dynamic web project in Eclipse. Tutorial I followed used Maven archetype for webapp, however, for some reason the same archetype i use doesn't provide the same folder structure and has an error (but that aside, not part of my question), so I just user dynamic web project. My class definition should be simple GET method to return the string entered:



package com.marko.server;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;

import org.springframework.stereotype.Service;


@Service
@Path("/return")
public class MyServ {


@GET
@Produces("text/plain")
@PathParam("/{str}")
public String why(@PathParam("str")String str){
return "why won't you work " + str;
}


}


web.xml contains standard code from CXF user guide, with just changed param-value pointing to beans.xml



<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE web-app
PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://ift.tt/HeF78r">
<web-app>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/beans.xml
</param-value>
</context-param>

<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>

<servlet>
<servlet-name>CXFServlet</servlet-name>
<display-name>CXF Servlet</display-name>
<servlet-class>
org.apache.cxf.transport.servlet.CXFServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
<servlet-name>CXFServlet</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>

</web-app>


And lastly my beans.xml referring JAX-RS to my created class



<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://ift.tt/GArMu6"
xmlns:xsi="http://ift.tt/ra1lAU"
xmlns:cxf="http://ift.tt/1iXPlPh"
xmlns:jaxrs="http://ift.tt/1kNxCZA"
xmlns:context="http://ift.tt/GArMu7"
xsi:schemaLocation="
http://ift.tt/1iXPlPh http://ift.tt/1iXPj9S
http://ift.tt/GArMu6
http://ift.tt/1jdM0fG
http://ift.tt/1kNxCZA http://ift.tt/1iXPm5B
http://ift.tt/GArMu7 http://ift.tt/1jdLYo7">

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

<context:component-scan base-package="edu.neumont.csc380"></context:component-scan>

<jaxrs:server id="restContainer" address="/">
<jaxrs:serviceBeans>
<ref bean="MyServ" />
</jaxrs:serviceBeans>
</jaxrs:server>

</beans>


I have imported all the CXF and Spring JARs to my lib folder, and am using Tomcat 7.0


Link I use to provide a GET request is of course http://localhost:8080/com.marko/rest/return/blabla


Aucun commentaire:

Enregistrer un commentaire