Using MvcUriComponentsBuilder::fromMethodCall with String as the return type
spring, spring-mvc
Solution
fromMethodCall uses CGLIB proxy in the process which is why you run into the issue. This article details why.https://github.com/spring-projects/spring-hateoas/issues/155. Try using fromMethodName if you want to maintain the String return types.
MvcUriComponentsBuilder.fromMethodName(MyController.class, "foo", new Object()).build();
Problem
I'd like to use the `MvcUriComponentsBuilder::fromMethodCall` method to build URLs from my controllers. I normally have a String return type (which returns the view name) and a Model instance as method parameter in my controller methods like: ``` @Controller public class MyController { @RequestMapping("/foo") public String foo(Model uiModel) { uiModel.addAttribute("pi", 3.1415); return "fooView"; } } ``` I try to generate a URL e.g. like: ``` String url = MvcUriComponentsBuilder.fromMethodCall(on(MyController.class).foo(null)).build().toUriString(); ``` This leads to this exception: ``` org.springframework.web.util.NestedServletException: Request processing failed; nested exception is java.lang.IllegalArgumentException: Cannot subclass final class class java.lang.String at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:978) ~[spring-webmvc-4.1.4.RELEASE.jar:4.1.4.RELEASE] ``` This happens because the String return type wants to get proxied, but can't as a final class. What's a way to overcome this? I'd like to keep the String as a return type and get the Model as input from a parameter in my controller methods because IMHO it's way easier than handling a ModelAndView instance in every controller method.