简体   繁体   English

我应该如何在Spring MVC中创建Groovy控制器

[英]How should I create Groovy controller in Spring MVC

I have seen this question, I am not able to get Groovy working in in my Spring MVC project. 我已经看到了这个问题,我无法在我的Spring MVC项目中使用Groovy。 Where should I keep and it and what should be the full path ? 我应该保留在哪里,完整路径应该是什么? Please excuse me for basic question, but I have to get started with using Groovy in Spring MVC. 请原谅我一个基本的问题,但是我必须在Spring MVC中开始使用Groovy。 Groovy is available in Spring 4.0 Snapshot and according to official site, final version should be live in December. Groovy在Spring 4.0 Snapshot中可用,根据官方网站,最终版本应该在12月发布。 But for now, is it advisable to use version 4.0 ? 但是现在,建议使用4.0版吗? (it has got builtin support for Groovy) (它具有对Groovy的内置支持)

Referenced code : 参考代码:

    <beans .........
...
...
      <bean class="full.qualified.name.of.ProxyAwareAnnotationMethodHandlerAdapter" />
...
...
      <lang:groovy script-source="classpath:com/example/mysample.groovy refresh-check-delay="1000" />
</beans>

And the java class is below : Java类如下:

//ProxyAwareAnnotationMethodHandlerAdapter.java

    package name.assafberg.spring;

    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;

    import org.springframework.aop.TargetSource;
    import org.springframework.aop.framework.Advised;
    import org.springframework.web.servlet.ModelAndView;
    import org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter;

    /**
     * Add proxy awareness to <code>AnnotationMethodHandlerAdapter</code>.
     * 
     * @author assaf
     */
    public class ProxyAwareAnnotationMethodHandlerAdapter extends AnnotationMethodHandlerAdapter {

        /**
         * @param request
         * @param response
         * @param handler
         * @return
         * @throws Exception
         * @see org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter#handle(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse, java.lang.Object)
         */
        @Override
        public ModelAndView handle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
            handler = unwrapHandler(handler);

            return super.handle(request, response, handler);
        }

        /**
         * @param handler
         * @return
         * @see org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter#supports(java.lang.Object)
         */
        @Override
        public boolean supports(Object handler) {
            handler = unwrapHandler(handler);

            return super.supports(handler);
        }

        /**
         * Attempt to unwrap the given handler in case it is an AOP proxy
         * 
         * @param handler
         * @return Object
         */
        private Object unwrapHandler(Object handler) {
            if (handler instanceof Advised) {
                try {
                    TargetSource targetSource = ((Advised) handler).getTargetSource();
                    return targetSource.getTarget();

                } catch (Exception x) {
                    throw new RuntimeException(x);
                }

            } else {
                return handler;     
            }       
        }

    }

Edit 编辑

There is NO NEED to deal with above files. 无需处理上述文件。 Only thing I was to take care of version of Groovy. 我唯一要注意的是Groovy版本。 Make sure from command line, that version of Groovy is same as version added by Class path. 确保从命令行确保Groovy的版本与“类路径”添加的版本相同。 And it worked. 而且有效。 There is hardly any configuration required. 几乎不需要任何配置。 Just created a .groovy class instead of Java like (thanks to +kunal for enlightening.) 刚刚创建了一个.groovy类而不是Java类(感谢+ kunal的启发。)

@Controller
class TestController {
     @Autowired
     @RequestMapping(value = "/welcome", method = RequestMethod.GET)
     String People() {

                return "people"
            }

    }

I had asked that question then as I was figuring out a way to create Groovy controllers. 然后我在想出一种创建Groovy控制器的方法时就问了这个问题

Groovy controllers worked excellently for me in a real app that is now in production for more than an year. Groovy控制器在一个实际的应用程序中为我出色地工作,该应用程序现已投入生产一年以上。 Just to help people see value of groovy in a Spring MVC project I had forked Heroku's sample java app and rewrote the controllers in groovy. 为了帮助人们在Spring MVC项目中看到groovy的价值,我分叉了Heroku的示例Java应用程序,并重写了groovy中的控制器。 Please checkout the project here: 请在此处签出项目:

https://github.com/kdabir/groovy-springmvc-sample https://github.com/kdabir/groovy-springmvc-sample

Also it would be interesting for you to check the history to see how groovy was introduced in a existing java project. 同样,查看历史记录以了解在现有的Java项目中如何引入groovy也会很有趣。 Although I have not touched that project since then, it should give you a fair idea for how to get started. 尽管从那时起我还没有涉及那个项目,但是它应该为您提供一个入门的思路。

EDIT: 编辑:

Just to clarify, you simply convert your .java files to .groovy and it should work (as long as it's valid groovy syntax). 澄清一下,您只需将.java文件转换为.groovy ,它就可以工作(只要它是有效的groovy语法)。

Write spring-mvc controller using groovy directly should cause some cglib error when you reload groovy. 在重新加载groovy时,直接使用groovy编写spring-mvc控制器应该会导致一些cglib错误。 so you can try this solution that use jdk proxy controller interface which is alone with spring annotation eg @RequestMapping @RestController @ResponseBody . 因此,您可以尝试使用使用jdk代理控制器接口的解决方案,该接口与spring注释一起单独使用,例如@RequestMapping @RestController @ResponseBody

This jdk proxy can link a groovy object to execute. 这个jdk代理可以链接一个Groovy对象来执行。 you can create a spring scan to scan the interface to a proxy you can see the example from micro-mvc lib. 您可以创建弹簧扫描以扫描代理接口,您可以从micro-mvc lib中查看示例。

https://github.com/jeffreyning/micro-mvc https://github.com/jeffreyning/nh-micro https://github.com/jeffreyning/micro-mvc https://github.com/jeffreyning/nh-micro

the scan is showed in GroovyScanner the proxy is showed in InjectGroovyProxy 扫描显示在GroovyScanner ,代理显示在InjectGroovyProxy

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

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