2017-08-13 114 views
0

我正在開發一個簡單的問答服務(Jersey JAX-RS)。有了這項服務,我已經拿出了以下資源(可能會增加)。Jersey服務和持久層

  • GET | POST ------------- /問題
  • GET | PUT | DELETE - /問題/(編號)
  • GET | POST ---- -------- /問題/ {ID} /回答
  • GET | PUT | DELETE - /問題/ {} questionId /答案/ {} answerId

這是迎合我的資源類所有上述路徑。

@Path("/questions") 
public class QuestionResource { 
    @Inject 
    private QuestionService questionService; 

    @GET 
    ...<list of questions> 

    @POST 
    ...<a new question> 

    @Path("{id}") 
    ...<a question> 

    @PUT  
    @Path("{id}") 
    ...<update a question> 

    @DELETE 
    @Path("{id}") 
    ...<delete a question> 

    @GET 
    @Path("{id}/answers") 
    ...<list of answers> 

    @POST 
    @Path("{id}/answers") 
    ...<a new answer for a question> 

    @GET 
    @Path("{questionId}/answers/{answerId}") 
    ...<an answer for a question> 

    @PUT 
    @Path("{questionId}/answers/{answerId}") 
    ...<update an answer for a question> 

    @DELETE 
    @Path("{questionId}/answers/{answerId}") 
    ...<delete an answer for a question> 
} 

這有相應的服務和持久層 - QuestionService/QuestionServiceImpl和QuestionRepository/QuestionRepositoryImpl。但是,我有些困惑,我應該把哪些服務和重新投入用來處理最後五個請求的方法。我是否應該將它們都放到問題服務和存儲庫或其他類別中 - 回答服務和存儲庫?

我正在考慮後者由於答案和問題的多對一關係(JPQL NamedQuery - 選擇一個FROM答案WHERE a.question.id = questionId)。這意味着我的QuestionResource中除了QuestionService之外還會有AnswerService。那會沒事的。

請賜教。謝謝。

+0

我覺得你應該把你方法的主要資源,就像如果你想那麼一個特定問題的答案,你的主要資源是問題,你應該把方法的問題類等。 –

+0

謝謝你的快速回答。你介意解釋一下爲什麼? –

+0

我想我不應該包含這個「我正在考慮後者由於答案和問題的多對一關係(JPQL NamedQuery - 選擇一個FROM回答一個WHERE a.question.id = questionId)」。對我的問題。它看起來不相關。 –

回答

0
  • 在RESTful API中的一切是一個資源,當談到 關係你認爲是主要的資源和其他資源或者 換句話說資源和子資源。

  • 在你的情況答案是子資源,因爲你的答案資源不可能沒有問題或在 換句話說,你的資源的一個依賴於其他 一個主要資源。絕對是你的答案取決於問題