Spring 3.1 / Hibernate / Jackson を使用して Restfull API サーバーを作成しています。
私は '購入' コントローラー/モデル/Dao を持っています。
ここで、購入を「タグ付け」する機能を追加する必要があります。
したがって、「購入」にリンクされた「tagId」と「tagName」を持つ単純なクラスです。
「購入」は複数の「タグ」を持つことができ、「タグ」は 1 つの「購入」にのみ属することができます。
追加しなければならないこの新しい「タグ」クラスを表す最良の方法は何ですか? いえ
- 'Tag' モデルに purchaseId 属性を追加して、何らかの方法で注釈を付ける必要がありますか?
- 「タグリスト」属性を「購入」モデルに追加する必要がありますか?
- 「PurchaseController」のサブクラスとなる「タグ」コントローラーを作成しますか?
- 等...
基本的に、Spring を使用してこれを設計するためのベスト プラクティスの方法を探しています。
さらに、私が採用できるデザインパターンに関する提案は大歓迎です。
ここでデコレータパターンが適用されるのでしょうか?
もちろん、すべての購入とタグはデータベースに保持する必要があります。
ありがとう
購入コントローラー:
@Controller
public class PurchaseController
{
@Autowired
private IPurchaseService purchaseService;
@RequestMapping(value = "purchase", method = RequestMethod.GET)
@ResponseBody
public final List<Purchase> getAll()
{
return purchaseService.getAll();
}
@RequestMapping(value = "purchase/{id}", method = RequestMethod.GET)
@ResponseBody
public final Purchase get(@PathVariable("id") final Long id)
{
return RestPreconditions.checkNotNull(purchaseService.getById(id));
}
@RequestMapping(value = "purchase/tagged", method = RequestMethod.GET)
@ResponseBody
public final List<Purchase> getTagged()
{
return RestPreconditions.checkNotNull(purchaseService.getTagged());
}
@RequestMapping(value = "purchase/pending", method = RequestMethod.GET)
@ResponseBody
public final List<Purchase> getPending()
{
return RestPreconditions.checkNotNull(purchaseService.getPending());
}
@RequestMapping(value = "purchase", method = RequestMethod.POST)
@ResponseStatus(HttpStatus.CREATED)
public void create(@RequestBody final Purchase entity)
{
RestPreconditions.checkRequestElementNotNull(entity);
purchaseService.addPurchase(entity);
}
}
購入モデル:
@Entity
@XmlRootElement
public class Purchase implements Serializable
{
/**
*
*/
private static final long serialVersionUID = 6603477834338392140L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private Long pan;
public Long getId()
{
return id;
}
public void setId(Long id)
{
this.id = id;
}
public Long getPan()
{
return pan;
}
public void setPan(Long pan)
{
this.pan = pan;
}
}