我的代码看起来像这样:
@Path("/UserService")
public class UserService {
UserDAO userDao = new UserDAO();
private static final String SUCCESS_RESULT = "<result>success</result>";
private static final String FAILURE_RESULT = "<result>failure</result>";
@GET
@Path("/users")
@Produces(MediaType.APPLICATION_XML)
public List<User> getUsers() throws SQLException {
return userDao.getAllUsers();
}
@GET
@Path("/users/{email}")
@Produces(MediaType.APPLICATION_XML)
public User getUser(@PathParam("email") String email) throws SQLException {
return userDao.getUser(email);
}
}
如果我尝试使用以下URI:http://localhost:8080/Webservice/rest/UserService/users
,我会收到所有用户,这是对的。我的问题是如果我打电话给http://localhost:8080/Webservice/rest/UserService/users/test
(测试是数据库中一个人的电子邮件,没有任何变化。
你知道为什么那不起作用吗?
更多代码:
@XmlRootElement(name = "user")
public class User implements Serializable {
private static final long serialVersionUID = 1L;
private String email = null;
private String vorname = null;
private String nachname = null;
private String gebDatum = null;
private String password = null;
private float kontostand = -1;
public User() {
super();
}
public User(String email, String vorname, String nachname, String gebDatum, String password, float kontostand) {
super();
this.email = email;
this.vorname = vorname;
this.nachname = nachname;
this.gebDatum = gebDatum;
this.password = password;
this.kontostand = kontostand;
}
public String getEmail() {
return email;
}
@XmlElement
public void setEmail(String email) {
this.email = email;
}
public String getVorname() {
return vorname;
}
@XmlElement
public void setVorname(String vorname) {
this.vorname = vorname;
}
public String getNachname() {
return nachname;
}
@XmlElement
public void setNachname(String nachname) {
this.nachname = nachname;
}
public String getGebDatum() {
return gebDatum;
}
@XmlElement
public void setGebDatum(String gebDatum) {
this.gebDatum = gebDatum;
}
public String getPassword() {
return password;
}
@XmlElement
public void setPassword(String password) {
this.password = password;
}
public float getKontostand() {
return kontostand;
}
@XmlElement
public void setKontostand(float kontostand) {
this.kontostand = kontostand;
}
在userDao
我有以下方法:
public List<User> getAllUsers() throws SQLException {
openConnection(
"select u.email from user u") ;
if (rs != null) {
while (rs.next()) {
User u = new User(rs.getObject(1).toString(), rs.getObject(2).toString(), rs.getObject(3).toString(),
rs.getObject(4).toString(), rs.getObject(5).toString(),
Float.parseFloat(rs.getObject(6).toString()));
users.add(u);
}
}
closeConnection();
return users;
}
public User getUser(String email) throws SQLException {
List<User> users = getAllUsers();
for (User user : users) {
if (user.getEmail() == email) {
return user;
}
}
return null;
}