Reputation: 1239
I have seen this question, I am not able to get Groovy working in in my Spring MVC project. 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. Groovy is available in Spring 4.0 Snapshot and according to official site, final version should be live in December. But for now, is it advisable to use version 4.0 ? (it has got builtin support for 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 :
//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. Make sure from command line, that version of Groovy is same as version added by Class path. And it worked. There is hardly any configuration required. Just created a .groovy class instead of Java like (thanks to +kunal for enlightening.)
@Controller
class TestController {
@Autowired
@RequestMapping(value = "/welcome", method = RequestMethod.GET)
String People() {
return "people"
}
}
Upvotes: 1
Views: 8618
Reputation: 9
Write spring-mvc
controller using groovy directly should cause some cglib
error when you reload groovy.
so you can try this solution that use jdk
proxy controller interface which is alone with spring annotation e.g. @RequestMapping
@RestController
@ResponseBody
.
This jdk
proxy can link a groovy object to execute.
you can create a spring scan to scan the interface to a proxy
you can see the example from micro-mvc lib.
https://github.com/jeffreyning/micro-mvc https://github.com/jeffreyning/nh-micro
the scan is showed in GroovyScanner
the proxy is showed in InjectGroovyProxy
Upvotes: 1
Reputation: 9868
I had asked that question then as I was figuring out a way to create Groovy controllers.
Groovy controllers worked excellently for me in a real app that is now in production for more than an year. 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. Please checkout the project here:
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. 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).
Upvotes: 4