自定义@service注释

时间:2012-12-14 13:38:24

标签: java-ee

I tried with xml configuration it is working fine.I am trying to customize @service tag for RMI.I created a jar with the classes, by the below link.I got the code from the below link.
        https://github.com/JamesEarlDouglas/barebones-spring-mvc/tree/master/reference/spring-remoting-annotation.

添加了客户端代码。 我没有创建一个httpinvoker服务bean,而是通过jar创建bean并使用@service annotaion注释该类。当我启动服务器时,我没有在我的jar类中获取日志。所以可能是我的jar没有在运行时加载并给我错误。

    My code:
    Client side :

    1) web.xml: Code for web.xml

          <servlet>
                <servlet-name>remoting</servlet-name>
                <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
                <load-on-startup>1</load-on-startup>
            </servlet>

            <servlet-mapping>
                <servlet-name>remoting</servlet-name>
                <url-pattern>/remoting/*</url-pattern>
            </servlet-mapping>`

    2) remoting-servlet.xml: Code for remoting servlet

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

                <context:component-scan base-package="com.apple.*" />  


                <bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"></bean>
                <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />

                <bean id="viewResolver"
                        class="org.springframework.web.servlet.view.InternalResourceViewResolver"
                        p:prefix="/WEB-INF/jsp/" p:suffix=".jsp" />


            </beans>


    3) Bean class:Code forbean class
        package com.apple.helloClientBean;

        import java.io.Serializable;

        public class ClientBean implements Serializable{

            /**
             * 
             */
            private static final long serialVersionUID = 1L;
            private String fName;
            private String lName;

            public void setfName(String fName) {
                this.fName = fName;
            }
            public String getfName() {
                return fName;
            }
            public void setlName(String lName) {
                this.lName = lName;
            }
            public String getlName() {
                return lName;
            }
        }


    4) Interface: Code for interface

        package com.apple.helloworldService;

        import com.apple.helloClientBean.ClientBean;


        public interface HelloWorldClient {

            public ClientBean getDetails();
        }


    5) class :Code for class
        package com.apple.helloworldServiceImpl;

        import org.springframework.remoting.Service;
        import org.springframework.remoting.ServiceType;
        import org.springframework.stereotype.Component;
        import org.springframework.web.bind.annotation.RequestMapping;
        import org.springframework.web.bind.annotation.RequestMethod;

        import com.apple.helloClientBean.ClientBean;
        import com.apple.helloworldService.HelloWorldClient;

        @Component("HelloWorldClient")
        @RequestMapping("/getRmiDetails")
        @Service(serviceInterface = HelloWorldClient.class,serviceType = ServiceType.HTTP)
        public class HelloWorldClientImpl implements HelloWorldClient{

            @RequestMapping(method = RequestMethod.GET)
            public ClientBean getDetails(){
                System.out.println("I am in HelloWorldClientImpl");
                ClientBean cBean =  new ClientBean();
                cBean.setfName("MahaveerP");
                cBean.setlName("Agrawal");
                return cBean;
            }
        }




 1. Client application context:

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


            <context:component-scan base-package="com.apple.*" />  


            <bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"></bean>
            <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />

            <bean id="ProxyHelloWorld" class="org.springframework.remoting.httpinvoker.HttpInvokerProxyFactoryBean">
                <property name="serviceUrl">
                <value>http://localhost:8080/HelloWorldClient/remoting/getRmiDetails</value></property>
                <property name="serviceInterface" value="com.apple.helloworldService/HelloWorldClient"/>
            </bean> 


            <bean id="viewResolver"
                    class="org.springframework.web.servlet.view.InternalResourceViewResolver"
                    p:prefix="/WEB-INF/jsp/" p:suffix=".jsp" />


    </beans>


    2. client class:
    package com.apple.HelloWorldMTImpl;

    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Component;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestMethod;

    import com.apple.HelloWorldMT.HelloWorldMT;
    import com.apple.helloClientBean.ClientBean;
    import com.apple.helloworldService.HelloWorldClient;

    @Component
    @RequestMapping("/getRMI")
    public class HelloWorldMTImpl implements HelloWorldMT{

        @Autowired
        HelloWorldClient hello = null;

        @Override
        @RequestMapping(method = RequestMethod.GET)
        public void getRmiDetails() {
            System.out.println("i am in Helloworld MT");
            ClientBean cb = hello.getDetails();
            System.out.println("fName: "+cb.getfName()+"  "+ "lName: "+cb.getlName());
        }
    }


    3. Client web.xml
    <web-app id="WebApp_ID" version="2.4" 
        xmlns="http://java.sun.com/xml/ns/j2ee" 
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
        xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">



        <context-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>
                /WEB-INF/context/applicationContext.xml
            </param-value>
        </context-param>

        <context-param>
            <param-name>webAppRootKey</param-name>
            <param-value>
                com.apple.HelloWorldMT
            </param-value>
        </context-param>

            <!-- Listeners -->
        <listener>
            <listener-class>org.springframework.web.util.WebAppRootListener</listener-class>
        </listener>

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


        <servlet>
            <servlet-name>ViewRendererServlet</servlet-name>
            <servlet-class>org.springframework.web.servlet.ViewRendererServlet</servlet-class>
            <load-on-startup>1</load-on-startup>
        </servlet>

        <servlet-mapping>
            <servlet-name>ViewRendererServlet</servlet-name>
            <url-pattern>/WEB-INF/servlet/view</url-pattern>
        </servlet-mapping>

        <servlet>
            <servlet-name>InitialScreenServlet</servlet-name>
            <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
            <load-on-startup>1</load-on-startup>
        </servlet>

        <servlet-mapping>
            <servlet-name>InitialScreenServlet</servlet-name>
            <url-pattern>/*</url-pattern>
        </servlet-mapping>

    </web-app>




    I am getting the below exception:

        18:59:29,829 ERROR [org.apache.catalina.core.ContainerBase.[jboss.web].[localhost].[/HelloWorldMT].[InitialScreenServlet]] Servlet.service() for servlet InitialScreenServlet threw exception: java.io.IOException: Did not receive successful HTTP response: status code = 405, status message = [Method Not Allowed]
            at org.springframework.remoting.httpinvoker.SimpleHttpInvokerRequestExecutor.validateResponse(SimpleHttpInvokerRequestExecutor.java:179) [:3.1.1.RELEASE]
            at org.springframework.remoting.httpinvoker.SimpleHttpInvokerRequestExecutor.doExecuteRequest(SimpleHttpInvokerRequestExecutor.java:92) [:3.1.1.RELEASE]
            at org.springframework.remoting.httpinvoker.AbstractHttpInvokerRequestExecutor.executeRequest(AbstractHttpInvokerRequestExecutor.java:136) [:3.1.1.RELEASE]
            at org.springframework.remoting.httpinvoker.HttpInvokerClientInterceptor.executeRequest(HttpInvokerClientInterceptor.java:192) [:3.1.1.RELEASE]
            at org.springframework.remoting.httpinvoker.HttpInvokerClientInterceptor.executeRequest(HttpInvokerClientInterceptor.java:174) [:3.1.1.RELEASE]
            at org.springframework.remoting.httpinvoker.HttpInvokerClientInterceptor.invoke(HttpInvokerClientInterceptor.java:142) [:3.1.1.RELEASE]
            at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172) [:3.1.1.RELEASE]
            at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:202) [:3.1.1.RELEASE]
            at $Proxy120.getDetails(Unknown Source) at com.apple.HelloWorldMTImpl.HelloWorldMTImpl.getRmiDetails(HelloWorldMTImpl.java:23) [:]
            at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) [:1.6.0_31]
            at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) [:1.6.0_31]
            at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) [:1.6.0_31]
            at java.lang.reflect.Method.invoke(Method.java:597) [:1.6.0_31]
            at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:176) [:3.1.1.RELEASE]
            at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:436) [:3.1.1.RELEASE]
            at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:424) [:3.1.1.RELEASE]
            at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:923) [:3.1.1.RELEASE]
            at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:852) [:3.1.1.RELEASE]
            at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:882) [:3.1.1.RELEASE]
            at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:778) [:3.1.1.RELEASE]
            at javax.servlet.http.HttpServlet.service(HttpServlet.java:734) [:1.0.0.Final]
            at javax.servlet.http.HttpServlet.service(HttpServlet.java:847) [:1.0.0.Final]
            at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:324) [:6.1.0.Final]
            at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:242) [:6.1.0.Final]
            at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:275) [:6.1.0.Final]
            at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:161) [:6.1.0.Final]
            at org.jboss.web.tomcat.security.SecurityAssociationValve.invoke(SecurityAssociationValve.java:181) [:6.1.0.Final]
            at org.jboss.modcluster.catalina.CatalinaContext$RequestListenerValve.event(CatalinaContext.java:285) [:1.1.0.Final]
            at org.jboss.modcluster.catalina.CatalinaContext$RequestListenerValve.invoke(CatalinaContext.java:261) [:1.1.0.Final]
            at org.jboss.web.tomcat.security.JaccContextValve.invoke(JaccContextValve.java:88) [:6.1.0.Final]
            at org.jboss.web.tomcat.security.SecurityContextEstablishmentValve.invoke(SecurityContextEstablishmentValve.java:100) [:6.1.0.Final]
            at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:159) [:6.1.0.Final]
            at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102) [:6.1.0.Final]
            at org.jboss.web.tomcat.service.jca.CachedConnectionValve.invoke(CachedConnectionValve.java:158) [:6.1.0.Final]
            at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109) [:6.1.0.Final]
            at org.jboss.web.tomcat.service.request.ActiveRequestResponseCacheValve.invoke(ActiveRequestResponseCacheValve.java:53) [:6.1.0.Final]
            at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:362) [:6.1.0.Final]
            at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:877) [:6.1.0.Final]
            at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:654) [:6.1.0.Final]
            at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:951) [:6.1.0.Final]
            at java.lang.Thread.run(Thread.java:680) [:1.6.0_31]



        Thanks & Regards
        Mahaveer

1 个答案:

答案 0 :(得分:0)

首先要检查一下。

  1. 尝试通过浏览器访问链接http://localhost:8080/HelloWorldClient/remoting/getRmiDetails,并确保其执行所需操作。
  2. 如果它有效,那么你至少可以解决问题与你的客户有关。
  3. 在客户端应用程序上下文xml中,您声明了

    <property name="serviceInterface" value="com.apple.helloworldService/HelloWorldClient"/>

    这是错误的,

    <property name="serviceInterface" value="com.apple.helloworldService.HelloWorldClient"/>

    然后对您的客户进行类似的检查,尝试通过浏览器访问您的客户端链接。