Skip to content

Instantly share code, notes, and snippets.

@joshlong
Last active October 11, 2023 20:07
Show Gist options
  • Select an option

  • Save joshlong/6431608 to your computer and use it in GitHub Desktop.

Select an option

Save joshlong/6431608 to your computer and use it in GitHub Desktop.

Revisions

  1. joshlong revised this gist Sep 4, 2013. 1 changed file with 3 additions and 1 deletion.
    4 changes: 3 additions & 1 deletion MvcRestApplicationInitializer.java
    Original file line number Diff line number Diff line change
    @@ -37,8 +37,10 @@ class RestConfiguration {
    */
    @Controller
    class StatusController {

    @RequestMapping(value= "/customers/{customerId}", method= RequestMethod.GET)
    public @ResponseBody Customer loadCustomer(@PathVariable long customerId ) {
    return customerRepository.findCustomerById ( customerId ); // any POJO
    // return a POJO Customer somehow (perhaps look it up in a database?)
    return someCustomer ;
    }
    }
  2. joshlong revised this gist Sep 4, 2013. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion MvcRestApplicationInitializer.java
    Original file line number Diff line number Diff line change
    @@ -37,7 +37,7 @@ class RestConfiguration {
    */
    @Controller
    class StatusController {
    @RequestMapping(value="/customers/{customerId}", method= RequestMethod.GET)
    @RequestMapping(value= "/customers/{customerId}", method= RequestMethod.GET)
    public @ResponseBody Customer loadCustomer(@PathVariable long customerId ) {
    return customerRepository.findCustomerById ( customerId ); // any POJO
    }
  3. joshlong created this gist Sep 4, 2013.
    44 changes: 44 additions & 0 deletions MvcRestApplicationInitializer.java
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,44 @@

    /**
    * <p>This will be picked up in a Servlet 3 environment like Apache Tomcat 7.
    *
    * <p>This replaces <code>web.xml</code>.
    *
    */
    public class MvcRestApplicationInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {

    @Override protected Class<?> [] getRootConfigClasses() {
    return new Class<?>[]{};
    }

    @Override protected Class<?>[] getServletConfigClasses() {
    return new Class<?>[]{ RestConfiguration.class };
    }

    @Override protected String[] getServletMappings(){ return new String[]{"/"}; }

    }

    /**
    * Installs Spring MVC and all the required support for JSON assuming
    * Jackson's on the CLASSPATH
    */
    @EnableWebMvc
    @ComponentScan
    @Configuration
    class RestConfiguration {
    // that's all!
    }

    /**
    * A simple Spring MVC REST controller. The returned value (Customer) will
    * be converted into JSON if the client signals that it can read JSON (using the
    * <CODE>ACCEPT</CODE> header.)
    */
    @Controller
    class StatusController {
    @RequestMapping(value=&quot;/customers/{customerId}&quot;, method= RequestMethod.GET)
    public @ResponseBody Customer loadCustomer(@PathVariable long customerId ) {
    return customerRepository.findCustomerById ( customerId ); // any POJO
    }
    }