2013-08-06 89 views
0

因此,我有一个工作的Spring MVC 3应用程序,其中有一层服务需要转化为一层RESTful Web服务。Spring MVC REST Web服务:使用JSON从PHP发布

经过一些研究,我发现使用注释做GET Web服务真的很容易。但是我仍然无法从Java以外的东西找到相关的POST示例。

因此,为了使事情变得“简单”,让我们这个通用控制器:

public abstract class GenericControllerImpl<T, F extends GenericForm> implements GenericController<T, F> { 

    protected String name; 
    protected String root; 
    protected GenericService<T, F> service; 

    public GenericControllerImpl(final String root, final String name, final GenericService<T, F> 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); 
} 

因此,一个简单的方法,如display会给这样的事情(如果我得到了它):

@Override 
@RequestMapping(value = "/{id}", method = RequestMethod.GET) 
@ResponseBody 
public T display(@PathVariable final int id) { 
    return this.service.findById(id); 
} 

但是我怎么能改变方法createAction

注意,这是一个通用控制器,其中所有的控制器延伸。如果这样可以保持这种状态,那将是非常棒的,但如果不可能,那么我会做当然需要的。

谢谢!

回答

0

虽然有点晚回答,这是我的代码。你可以找到很好的解释here

@RequestMapping(method = RequestMethod.POST) 
public 
@ResponseBody 
Transaction createTransaction(MyModel myModel, 
     @RequestParam("secKey") String secKey) { 

    log.debug(" create MyModel {}", mymodel); 
    return new MyModel(); 

} 

我想你应该再看看你的删除操作。它使用post请求,如果你可以使用,放入或删除它会更好。