简体   繁体   中英

GET doesn't work for RESTful Webservice

My code looks like that:

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

If I try the following URI: http://localhost:8080/Webservice/rest/UserService/users , I get all users and that's right. My problem is that if I call http://localhost:8080/Webservice/rest/UserService/users/test (test is the email of one person in the database, nothing changes.

Do you know why that doesn't work?

More code:

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

In the userDao I have the following methods:

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

您已经有了答案,但是如果您想了解为什么要看一下如何比较Java中的字符串?

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