在运行时将注释添加到现有类不会生效

时间:2019-02-06 14:32:22

标签: java jax-rs jersey-2.0 javassist

我的问题是我在运行时(使用javassist)但在服务器启动和jersey servlet初始化之前,正在将注解更具体地@Path注解添加到jersey资源类中,并且我还可以验证注解是否正确获取添加到类中,但是当我尝试通过调用资源类的端点来调用Web服务时,出现404错误,而且我似乎无法弄清楚我在做什么错。这是代码

 public static void main(String[] args) throws Exception {
            ClassPool pool = ClassPool.getDefault();
            CtClass ccd = pool.get("org.demonking.JettyServiceApp2");
            ClassFile cfile = ccd.getClassFile();
            ConstPool cpool = cfile.getConstPool();
             AnnotationsAttribute attr =
            new AnnotationsAttribute(cpool, AnnotationsAttribute.visibleTag);
            //AnnotationsAttribute attr =  (AnnotationsAttribute) cfile.getAttribute(AnnotationsAttribute.visibleTag);
            Annotation annot = new Annotation(javax.ws.rs.Path.class.getCanonicalName(), cpool);
            annot.addMemberValue("value", new StringMemberValue("/hello2", cpool));
            attr.addAnnotation(annot);
            ccd.getClassFile().addAttribute(attr);
            ccd.toClass(JavassistDemoMain.class.getClass().getClassLoader(),
                    JavassistDemoMain.class.getClass().getProtectionDomain());
            Class.forName("org.demonking.JettyServiceApp2");
            java.lang.annotation.Annotation[] annotations=org.demonking.JettyServiceApp2.class.getAnnotations();
            for(java.lang.annotation.Annotation an:annotations)
            {
                System.out.println("annotated with "+an.annotationType());

            }
            Class<?> cls=org.demonking.JettyServiceApp2.class;

            java.lang.annotation.Annotation[] annotations2=cls.getMethod("test").getAnnotations();
            for(java.lang.annotation.Annotation an:annotations2)
            {
                System.out.println("annotated with "+an.annotationType());

            }
            javax.ws.rs.Path path=org.demonking.JettyServiceApp2.class.getAnnotation(javax.ws.rs.Path.class);
            System.out.println(path.value());
    ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
            context.setContextPath("/JettyApp");
            ServletHolder holder = new ServletHolder();
            holder.setName("jettyApp");
            holder.setClassName(org.glassfish.jersey.servlet.ServletContainer.class.getName());
            holder.setInitParameter("javax.ws.rs.Application", "org.demonking.AppConfig");
            System.out.println(holder.getInitParameter("javax.ws.rs.Application"));
            holder.setInitOrder(0);
            context.getServletHandler().addServlet(holder);
            ServletMapping mapping = new ServletMapping();
            mapping.setServletName("jettyApp");
            mapping.setPathSpec("/*");
            context.getServletHandler().addServletMapping(mapping);
            Server jettyServer = new Server(9081);
            jettyServer.setHandler(context);
            jettyServer.start();

        }

这是我的JettyServiceApp2类,没有@Path注释

public class JettyServiceApp2 {
    @GET
    @Produces(MediaType.APPLICATION_JSON)
    public Message test() {
        Message msg = new Message();
        msg.id = 101;
        msg.message = "hello this is test message indicating every thing is fine";
        return msg;
    }
}

0 个答案:

没有答案