How to insert data from database with Web Service in java using JAX - RS

后端 未结 2 1639
时光取名叫无心
时光取名叫无心 2020-12-13 08:01

I am new to web services. Please give suggestions how to insert and retrieve data from database using jersey JAX - RS in java?

2条回答
  •  难免孤独
    2020-12-13 08:28

    Have a look at this link, there is a video tutorial that explains one way how it can be done.

    So the video is explaining how a web service retrieves some value to tell the client that the given credentials match against some data in a database.

    This is how the method that handle the client request looks like:

    @POST
        @Produces(MediaType.TEXT_PLAIN)
        @Path("/login")
        public Response login(@FormParam("email") String email, @FormParam("password") String password) {
    
            Response.ResponseBuilder responseBuilder = null;
            
            boolean result = entityFacade.doLogin(email, password);
            if (result) {
                 responseBuilder = Response.ok("Success");//Login good!
            }
            else {            
                responseBuilder = Response.ok("Wrong credentials!");//Login bad!
            }
    
            return responseBuilder.build();
        }
    

    This Web service transfers the credentials to the business layer where an EJB will perform a select operation in the database:

    @Stateless
    public class EntityFacade implements EntityFacadeLocal {
    
        @Resource
        private UserTransaction ut;
        private CredentialJpaController controller;
    
        @PostConstruct
        public void init() {        
            controller = new CredentialJpaController(ut, Persistence.createEntityManagerFactory("ROLEProject-ejbPU"));
        }
    
    
        public boolean doLogin(String email, String password){        
            return controller.loginWithParameters(email,password);
        }
    }
    

    The EJB relies in a service class that will do the task of interact with the database

    public class CredentialJpaController implements Serializable {
    
        public CredentialJpaController(UserTransaction utx, EntityManagerFactory emf) {
            this.utx = utx;
            this.emf = emf;
        }
        private UserTransaction utx = null;
        private EntityManagerFactory emf = null;
    
        public EntityManager getEntityManager() {
            return emf.createEntityManager();
        }
        public boolean loginWithParameters(String email, String password) {
            boolean result = false;
            EntityManager em = getEntityManager();
            Long l = (Long) em.createNamedQuery("loginquery").setParameter("emailParam", email).setParameter("passwordParam", password).getSingleResult();
            if (l == 1) {
                result = true;
            }
            return result;
        }
    }
    

    Finally to be able to interact with the DB, the data must be represented as a JPA entity. Also to be able to marshal the selected row back to the client as whatever MediaType is desired, the entity must contain some JaxB annotations:

    @XmlRootElement
    @Entity
    @Table(name = "CREDENTIALS")
    @NamedQuery(name="loginquery", query="SELECT COUNT(c) FROM Credential c WHERE c.email = :emailParam AND c.password = :passwordParam")
    public class Credential implements Serializable {
    
        @Id
        private String email;
        @Column(nullable = false)
        private String password;
    
        @XmlElement
        public String getEmail() {
            return email;
        }
    
        public void setEmail(String email) {
            this.email = email;
        }
    
         @XmlElement
        public String getPassword() {
            return password;
        }
    
        public void setPassword(String password) {
            this.password = password;
        }
    }
    

    Here is some sample client code so you can see one of the ways how the web-service can be called:

    
        
            TODO supply a title
        
        
            
    TODO write content
    Email Password

    I hope this helps to give you an idea.

    To do insert data the process is almost the same, just use the method persist() instead of find() from the entity manager.

提交回复
热议问题