Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
456 views
in Technique[技术] by (71.8m points)

jax rs - REST with Java (JAX-RS) using Jersey

I developed a restful web service via Jersey in Java (JAX-RS) : http://www.vogella.com/articles/REST/article.html

Then I used the Hibernate Technology to map the data to the database.

Finally I developed an android application to display data.

This is an example of a method in my Web Service :

    @GET
    @Path("/project_id/username/get/{projectId}/{username}/")
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces(MediaType.APPLICATION_JSON)
    public Response deliverableList(@PathParam("projectId") long projectId,
                            @PathParam("username") String username) {
                Session session = HibernateUtil.getSessionFactory().getCurrentSession();
                session.beginTransaction();
                List<Deliverable> list = null;
                try {
                    list= (List<Deliverable>) session.createQuery(
                            "from Deliverable as d where d.project.id= :id").setLong("id", projectId).list();   
                    } catch (HibernateException e) {
                        e.printStackTrace();
                        session.getTransaction().rollback();
                    }
                    session.getTransaction().commit();
                    return Response.status(201).entity(list).build();
                }

as you see I used "Response.status(201).entity(list).build()" to transfer the list of data. Is it a good way? if not what is your suggestion to transfer the data. Please support your explanation with some codes and examples.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
  1. Response.ok().enity(object).build() is the correct way to return data
  2. You really want to move your hibernate stuff to a data access tier... it's going to be hard to manage intermingled with your service tier
  3. I completely disagree with smcg about using a helper method to map the java to json. Use the jax-rs annotations on your beans to do it unless you have really complicated requirements: see http://wiki.fasterxml.com/JacksonAnnotations

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...