したがって、RESTful Web サービスのレイヤーに変換する必要があるサービスのレイヤーを備えた動作中の Spring MVC 3 アプリケーションがあります。
いくつかの調査の後、アノテーションを使用して 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
ますか?
これは、すべてのコントローラーが拡張する汎用コントローラーであることに注意してください。このままでいられたら最高ですが、それが無理ならもちろん必要なことはやります。
ありがとう!