Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>What you can do is having a <code>MyRestController</code> just wrapping the result in a <code>Response</code> like this:</p> <pre><code>@Controller @RequestMapping(value = "/mycontroller") public class MyRestController { @Autowired private MyController myController; @RequestMapping(value = "/details") public @ResponseBody Response&lt;ServiceDetails&gt; getServiceDetails() { return new Response(myController.getServiceDetails(),"Operation OK"); } } </code></pre> <p>This solution keep your original <code>MyController</code> independant from your REST code. It seems you need to include Jackson in your classpath so that Spring will auto-magically serialize to JSON (see <a href="http://www.mkyong.com/spring-mvc/spring-3-mvc-and-json-example/" rel="nofollow">this</a> for details)</p> <p><strong>EDIT</strong></p> <p>It seems you need something more generic... so here is a suggestion.</p> <pre><code>@Controller @RequestMapping(value = "/mycontroller") public class MyGenericRestController { @Autowired private MyController myController; //this will match all "/myController/*" @RequestMapping(value = "/{operation}") public @ResponseBody Response getGenericOperation(String @PathVariable operation) { Method operationToInvoke = findMethodWithRequestMapping(operation); Object responseBody = null; try{ responseBody = operationToInvoke.invoke(myController); }catch(Exception e){ e.printStackTrace(); return new Response(null,"operation failed"); } return new Response(responseBody ,"Operation OK"); } private Method findMethodWithRequestMapping(String operation){ //TODO //This method will use reflection to find a method annotated //@RequestMapping(value=&lt;operation&gt;) //in myController return ... } } </code></pre> <p>And keep your original "myController" almost as it was:</p> <pre><code>@Controller public class MyController { //this method is not expected to be called directly by spring MVC @RequestMapping(value = "/details") public ServiceDetails getServiceDetails() { return new ServiceDetails("MyService"); } } </code></pre> <p>Major issue with this : the @RequestMapping in <code>MyController</code> need probably to be replaced by some custom annotation (and adapt <code>findMethodWithRequestMapping</code> to perform introspection on this custom annotation).</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload