简体   繁体   English

REST服务:如何在不使用注释的情况下指定annotatedMethod

[英]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中进行配置。

spring-config.xml looks like spring-config.xml看起来像

<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 : 现在当我点击暴露的服务时: 在此输入图像描述

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 我调试了cxf-jaxrs源码并在线上调试

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. 该方法搜索annotatedMethod但该对象的annotatedMethod字段为null ,从而抛出错误。

i could get it to work using annotations in the class above the method. 我可以使用方法上方的类中的注释来使用它。 But i want it to work through the xml config. 但我希望它通过xml配置工作。

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. retrieve是要调用的操作。 what other attribute needs to be set up specified?? 还需要设置哪些其他属性? or is it just a limitation of xml configuration? 或者它只是xml配置的限制?

PS: if you feel some more info needs to be added or clarified, do leave comments. PS:如果您觉得需要添加或澄清更多信息,请留下评论。 New to RESTful services and not sure what all information is expected in this context. RESTful服务的新功能,不确定在此上下文中所需的所有信息。

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
    }

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 . 从我所看到的,CXF不一定期望该方法被注释, WadlGenerator只是在类OperationResourceInfo的实例上调用一个方法,该实例恰好被称为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. 我的建议是在堆栈跟踪中调高,看看是什么创建了类OperationResourceInfo的实例,从中你应该能够看到这个创建是否出错。

In addition what version of CXf are you using? 另外你使用的是什么版本的CXf? Ensure its the latest one. 确保它是最新的。

Out of curiosity is there any reason why you'd be going to XML config as opposed to annotations? 出于好奇,有没有理由为什么要进行XML配置而不是注释?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM