Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

REST services : how to specify annotatedMethod without using annotations

We are trying to take out all the annotations from our classes and configure it in a spring-config.xml.

spring-config.xml looks like

<jaxrs:server id="restServer" address="/rest/">
        <jaxrs:model id="restModel">
            <jaxrs:resource name="com.csc.fs.rest.contact.RetrieveContactHistoryBP" path="retrieveContactHistoryBP">
                <jaxrs:operation name="retrieve" path="{partyId}" consumes="application/json" produces="application/json" verb="GET">
                    <jaxrs:param name="req" type="CONTEXT"/>
                    <jaxrs:param name="partyId" type="PATH"/>
                </jaxrs:operation>
            </jaxrs:resource>
            <jaxrs:resource name="com.csc.fs.rest.contact.StartContactBP" path="startContactBP">
                <jaxrs:operation name="startContact" path="/" consumes="application/json" produces="application/json" verb="PUT">
                    <jaxrs:param name="req" type="CONTEXT"/>
                    <jaxrs:param name="startContact" type="REQUEST_BODY"/>
                </jaxrs:operation>
            </jaxrs:resource>
        </jaxrs:model>
        <jaxrs:serviceBeans>

now when i click on the exposed service : enter image description here

i get the following trace :

HTTP Status 500 - 
________________________________________
type Exception report
message 
description The server encountered an internal error () that prevented it from fulfilling this request.
exception 
java.lang.RuntimeException: org.apache.cxf.interceptor.Fault
    org.apache.cxf.interceptor.AbstractFaultChainInitiatorObserver.onMessage(AbstractFaultChainInitiatorObserver.java:102)
    org.apache.cxf.phase.PhaseInterceptorChain.doIntercept(PhaseInterceptorChain.java:315)
    org.apache.cxf.transport.ChainInitiationObserver.onMessage(ChainInitiationObserver.java:113)
    org.apache.cxf.transport.servlet.ServletDestination.invoke(ServletDestination.java:105)
    org.apache.cxf.transport.servlet.ServletController.invokeDestination(ServletController.java:461)
    org.apache.cxf.transport.servlet.ServletController.invoke(ServletController.java:188)
    org.apache.cxf.transport.servlet.AbstractCXFServlet.invoke(AbstractCXFServlet.java:148)
    org.apache.cxf.transport.servlet.AbstractHTTPServlet.handleRequest(AbstractHTTPServlet.java:179)
    org.apache.cxf.transport.servlet.AbstractHTTPServlet.doGet(AbstractHTTPServlet.java:108)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
    org.apache.cxf.transport.servlet.AbstractHTTPServlet.service(AbstractHTTPServlet.java:159)
root cause 
org.apache.cxf.interceptor.Fault
    org.apache.cxf.interceptor.AbstractFaultChainInitiatorObserver.onMessage(AbstractFaultChainInitiatorObserver.java:67)
    org.apache.cxf.phase.PhaseInterceptorChain.doIntercept(PhaseInterceptorChain.java:315)
    org.apache.cxf.transport.ChainInitiationObserver.onMessage(ChainInitiationObserver.java:113)
    org.apache.cxf.transport.servlet.ServletDestination.invoke(ServletDestination.java:105)
    org.apache.cxf.transport.servlet.ServletController.invokeDestination(ServletController.java:461)
    org.apache.cxf.transport.servlet.ServletController.invoke(ServletController.java:188)
    org.apache.cxf.transport.servlet.AbstractCXFServlet.invoke(AbstractCXFServlet.java:148)
    org.apache.cxf.transport.servlet.AbstractHTTPServlet.handleRequest(AbstractHTTPServlet.java:179)
    org.apache.cxf.transport.servlet.AbstractHTTPServlet.doGet(AbstractHTTPServlet.java:108)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
    org.apache.cxf.transport.servlet.AbstractHTTPServlet.service(AbstractHTTPServlet.java:159)
root cause 
java.lang.NullPointerException
    org.apache.cxf.jaxrs.model.wadl.WadlGenerator.handleOperation(WadlGenerator.java:310)
    org.apache.cxf.jaxrs.model.wadl.WadlGenerator.handleResource(WadlGenerator.java:253)
    org.apache.cxf.jaxrs.model.wadl.WadlGenerator.handleRequest(WadlGenerator.java:185)
    org.apache.cxf.jaxrs.impl.RequestPreprocessor.checkMetadataRequest(RequestPreprocessor.java:189)
 

I did debug into the cxf-jaxrs source and at line

java.lang.NullPointerException
        org.apache.cxf.jaxrs.model.wadl.WadlGenerator.handleOperation(WadlGenerator.java:310)

the method searches for the annotatedMethod but the object has annotatedMethod field as null which thus throws the error.

i could get it to work using annotations in the class above the method. But i want it to work through the xml config.

I have specified

<jaxrs:operation name="retrieve" path="{partyId}" consumes="application/json" produces="application/json" verb="GET">

the retrieve is the operation to be called. what other attribute needs to be set up specified?? or is it just a limitation of xml configuration?

PS: if you feel some more info needs to be added or clarified, do leave comments. New to RESTful services and not sure what all information is expected in this context.

UPDATE :

service class

//@Path("/startContactBP")
public class StartContactBP {
    //@PUT  
    //@Consumes(MediaType.APPLICATION_JSON)
    //@Produces(MediaType.APPLICATION_JSON)
    //@Path ("/")   
    //public com.csc.fs.ws.contact.StartContactResult startContact(@Context HttpServletRequest req, com.csc.fs.ws.contact.StartContact startContact){
    public com.csc.fs.ws.contact.StartContactResult startContact(HttpServletRequest req, com.csc.fs.ws.contact.StartContact startContact){

        //call login
            //call actual service
            //call logoff
    }
like image 416
Mukul Goel Avatar asked Nov 21 '12 11:11

Mukul Goel


1 Answers

From what I can see CXF is not necessarily expecting the method to be annotated, WadlGenerator is simply calling a method on an instance of the class OperationResourceInfo which happens to be called getAnnotatedMethod. This could be because the developers were just lazy and didn't name the method apprioriately.

My suggestion is to debug higher in the stack trace to see what is creating the instance of the class OperationResourceInfo, from that you should be able to see if this creation is going wrong.

In addition what version of CXf are you using? Ensure its the latest one.

Out of curiosity is there any reason why you'd be going to XML config as opposed to annotations?

like image 136
ramsinb Avatar answered Sep 24 '22 14:09

ramsinb