title: Spring MVC REST Web services: post from PHP using JSON
body: <p>So I have a working Spring MVC 3 application with a layer of services I need to turn into a layer of RESTful Web services.</p>
  
  <p>After a few researches, I found it was really easy to do GET Web services using annotations. But I still can't find a relevant POST example from something else than Java.</p>
  
  <p>So, to make things "simple" let's take this generic controller:</p>
  
  <pre><code>public abstract class GenericControllerImpl&lt;T, F extends GenericForm&gt; implements GenericController&lt;T, F&gt; {
  
      protected String name;
      protected String root;
      protected GenericService&lt;T, F&gt; service;
  
      public GenericControllerImpl(final String root, final String name, final GenericService&lt;T, F&gt; service) {
          this.root = root;
          this.name = name;
          this.service = service;
      }
  
      @Override
      @RequestMapping(method = RequestMethod.GET)
      public String defaultHome(final Model model) {
          this.loadEntities(model);
          this.populateLists(model);
          return this.root;
      }
  
      @Override
      @RequestMapping(value = "/{id}", method = RequestMethod.GET)
      public String display(@PathVariable final int id, final Model model) {
          this.loadEntityContext(model, id);
          return "display" + this.name;
      }
  
      @Override
      @RequestMapping(value = "/create", method = RequestMethod.GET)
      public String displayCreate(final Model model) {
          this.loadCreateContext(model);
          this.populateLists(model);
  
          return "create" + this.name;
      }
  
      @Override
      @RequestMapping(value = "/create", method = RequestMethod.POST)
      public String createAction(@Valid @ModelAttribute("createForm") final F form, final BindingResult result, final Model model) {
  
          try {
              if (!result.hasErrors()) {
                  this.service.create(form);
                  return "redirect:/" + this.root + '/';
              } else {
                  this.populateLists(model);
                  return "create" + this.name;
              }
          } catch (final Exception exception) {
              this.populateLists(model);
              this.loadErrorContext(model, exception);
              return "create" + this.name;
          }
      }
  
      @Override
      @RequestMapping(value = "/delete", method = RequestMethod.POST)
      public String deleteAction(final Model model, @RequestParam(value = "id", required = true) final int id) {
  
          try {
              this.service.deleteFromId(id);
              return "redirect:/" + this.root + '/';
          } catch (final Exception exception) {
              this.loadErrorContext(model, exception);
              return this.root;
          }
      }
  
      /**
       * Load all the entities of the entity type handled by the controller into the context model.
       * 
       * @param model
       *            the context model
       */
      protected void loadEntities(final Model model) {
          model.addAttribute("list", this.service.list());
      }
  
      /**
       * Load a specific entity into the context model.
       * 
       * @param model
       *            the context model
       * @param id
       *            the id of the entity to load
       */
      protected T loadEntity(final Model model, final int id) {
          T item = this.service.findById(id);
          model.addAttribute("item", item);
          return item;
      }
  
      /**
       * Load a specific entity context into the context model. By default it loads only the entity but for some entities, some other
       * entities might be useful.
       * 
       * @param model
       *            the context model
       * @param id
       *            the id of the entity to load
       */
      protected void loadEntityContext(final Model model, final int id) {
          this.loadEntity(model, id);
      }
  
      /**
       * Populate the lists of the other entities referenced by the current entity.
       * 
       * @param model
       *            the context model
       */
      protected abstract void populateLists(Model model);
  }
  </code></pre>
  
  <p>So a simple method such as <code>display</code> would give something like this (if I got it):</p>
  
  <pre><code>@Override
  @RequestMapping(value = "/{id}", method = RequestMethod.GET)
  @ResponseBody
  public T display(@PathVariable final int id) {
      return this.service.findById(id);
  }
  </code></pre>
  
  <p>But how could I change the method <code>createAction</code> ?</p>
  
  <p>Note that this is a generic controller which all the controllers extend. It would be awesome if this could stay this way, but if it's not possible then I'll do what's needed of course.</p>
  
  <p>Thanks!</p>