【问题标题】:Spring MVC REST Web services: post from PHP using JSONSpring MVC REST Web 服务:使用 JSON 从 PHP 发布
【发布时间】:2013-08-07 04:24:59
【问题描述】:

所以我有一个可以工作的 Spring MVC 3 应用程序,它带有一层服务,我需要将其转换为一层 RESTful Web 服务。

经过一些研究,我发现使用注释来做 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的方法呢?

请注意,这是一个所有控制器都扩展的通用控制器。如果可以保持这种状态,那就太棒了,但如果不可能,那么我当然会做需要做的事情。

谢谢!

【问题讨论】:

    标签: php json web-services post spring-mvc


    【解决方案1】:

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

    @RequestMapping(method = RequestMethod.POST)
    public
    @ResponseBody
    Transaction createTransaction(MyModel myModel,
            @RequestParam("secKey") String secKey) {
    
        log.debug(" create MyModel {}", mymodel);
        return new MyModel();
    
    }
    

    我认为你应该再看看你的删除操作。它使用post请求,如果您可以使用,放置或删除,那就更好了。

    【讨论】:

      猜你喜欢
      • 2013-10-19
      • 2015-08-04
      • 1970-01-01
      • 1970-01-01
      • 2014-12-25
      • 1970-01-01
      • 1970-01-01
      • 2012-10-21
      • 1970-01-01
      相关资源
      最近更新 更多