简体   繁体   中英

Jersey Service and Persistence Layers

I'm am developing a simple Question and Answer service (Jersey JAX-RS). With this service, I've come up with the following resource so far (might increase).

  • GET|POST -------------/questions
  • GET|PUT|DELETE -- /questions/{id}
  • GET|POST ------------ /questions/{id}/answers
  • GET|PUT|DELETE - /questions/{questionId}/answers/{answerId}

This is my resource class with caters all of the above paths.

@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>
}

This has corresponding service and persistence layers - QuestionService/QuestionServiceImpl and QuestionRepository/QuestionRepositoryImpl. But, I'm bit confused as to which service and reposipotory I should put the methods that would be called to process the request of the last five. Should I put them all also to Question service and repository or to another classess - Answer service and repository?

I'm considering the latter due to Many-To-One relationship of Answer and Question (JPQL NamedQuery - SELECT a FROM Answer a WHERE a.question.id = :questionId). It would mean that I would have also AnswerService aside from QuestionService in my QuestionResource. Would that be alright.

Please enlighten me. Thank you.

  • In restful APIs everything is a resource and when it comes to relation you consider a main resource and other resources or in another words resource and subresource.

  • In your case the answer is the subresource because your answer resource can't be a main resource without question or in another words which one of your resources depends on the other one. Absolutely your answer depends on question

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM