我正在使用jersey restful webservice + hibernate框架和maven java项目创建一个注册页面。 注册类:
package com.data;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Entity;
@Entity
public class Registration {
@Id @GeneratedValue(strategy=GenerationType.AUTO)
private int id;
private String fristName;
private String lastName;
private String email;
private String password;
private String rePassword;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getFristName() {
return fristName;
}
public void setFristName(String fristName) {
this.fristName = fristName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getRePassword() {
return rePassword;
}
public void setRePassword(String rePassword) {
this.rePassword = rePassword;
}
}
RegistrationService类:
package com.dao;
import java.util.ArrayList;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import com.data.Registration;
public class RegistrationService {
private SessionFactory sessionFactory = null;
public Registration adddNewUser(Registration newUser){
try{
sessionFactory = new Configuration().configure().buildSessionFactory();
Session session = sessionFactory.openSession();
session.beginTransaction();
session.save(newUser);
session.getTransaction().commit();
session.close();
}catch(Exception ex){
ex.printStackTrace();
}
return newUser;
}
@SuppressWarnings("unchecked")
public ArrayList<Registration> getUsers(){
ArrayList<Registration> users = new ArrayList<Registration>();
try{
sessionFactory = new Configuration().configure().buildSessionFactory();
Session session = sessionFactory.openSession();
users = (ArrayList<Registration>) session.createCriteria(Registration.class).list();
session.close();
}catch(Exception ex){
ex.printStackTrace();
}
return users;
}
}
RegistrationResource类:
package com.resource;
import java.util.ArrayList;
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import com.dao.RegistrationService;
import com.data.Registration;
@Path("register")
public class RegistrationResource {
private RegistrationService registrationService = new RegistrationService();
@Path("new")
@POST
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public Registration addNewUser(Registration newUser){
return registrationService.adddNewUser(newUser);
}
@GET
@Path("all")
@Produces(MediaType.APPLICATION_JSON)
public ArrayList<Registration> getAllUsers(){
return registrationService.getUsers();
}
}
有人帮助....... 亲切地告诉我哪里有问题我会感谢你... :)