Witam,
testuje swoje API REST'owe "napisane" za pomocą Springa.
API działa poprawnie, sprawdzałem za pomocą curl'a. Jednak mam problem z przekazaniem obiektu w GWT'owym kliencie.
Niestety technologia została narzucona dlatego taki klient, a nie inny. Starałem się robić z pomocą dokumentacji, ale albo coś przeoczyłem albo po prostu od ciągłego wpatrywania się nie widzę błędu.
Klasa z dziedziny
package pl.connectis.pu.gwt.shared;
import java.io.Serializable;
import java.sql.Date;
public class User implements Serializable {
private static final long serialVersionUID = 1L;
private Long id;
private String name;
private String username;
private String login;
private Date dob;
private String email;
public User() { }
public User(String name, String username, String login, Date dob, String email) {
this.name = name;
this.username = username;
this.login = login;
this.dob = dob;
this.email = email;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getLogin() {
return login;
}
public void setLogin(String login) {
this.login = login;
}
public Date getDob() {
return dob;
}
public void setDob(Date dob) {
this.dob = dob;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
@Override
public String toString() {
return "User [id=" + id + ", name=" + name + ", username=" + username + ", login=" + login + ", dob=" + dob
+ ", email=" + email + "]";
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((dob == null) ? 0 : dob.hashCode());
result = prime * result + ((email == null) ? 0 : email.hashCode());
result = prime * result + ((id == null) ? 0 : id.hashCode());
result = prime * result + ((login == null) ? 0 : login.hashCode());
result = prime * result + ((name == null) ? 0 : name.hashCode());
result = prime * result + ((username == null) ? 0 : username.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
User other = (User) obj;
if (dob == null) {
if (other.dob != null)
return false;
} else if (!dob.equals(other.dob))
return false;
if (email == null) {
if (other.email != null)
return false;
} else if (!email.equals(other.email))
return false;
if (id == null) {
if (other.id != null)
return false;
} else if (!id.equals(other.id))
return false;
if (login == null) {
if (other.login != null)
return false;
} else if (!login.equals(other.login))
return false;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
if (username == null) {
if (other.username != null)
return false;
} else if (!username.equals(other.username))
return false;
return true;
}
}
UserService
package pl.connectis.pu.gwt.client;
import java.util.List;
import com.google.gwt.user.client.rpc.RemoteService;
import com.google.gwt.user.client.rpc.RemoteServiceRelativePath;
import pl.connectis.pu.gwt.shared.User;
@RemoteServiceRelativePath("user")
public interface UserService extends RemoteService {
void addUser(User user);
void deleteUser (Long id);
//void updatePerson (User user);
List<User> getAllUsers();
}
UserServiceAsyns
package pl.connectis.pu.gwt.client;
import com.google.gwt.user.client.rpc.AsyncCallback;
import pl.connectis.pu.gwt.shared.User;
public interface UserServiceAsync {
void addUser(User user, AsyncCallback<Void> callback);
void deleteUser (Long id, AsyncCallback<User> callback);
void getAllUsers(AsyncCallback<User> callback);
//void updatePerson (User user, AsyncCallback<User> callback);
}
UserServiceImpl
package pl.connectis.pu.gwt.server;
import java.util.ArrayList;
import java.util.List;
import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.Entity;
import javax.ws.rs.client.Invocation;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.GenericType;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import com.google.gwt.user.server.rpc.RemoteServiceServlet;
import pl.connectis.pu.gwt.client.UserService;
import pl.connectis.pu.gwt.shared.User;
public class UserServiceImpl extends RemoteServiceServlet implements UserService {
private static final long serialVersionUID = 1L;
private final String url = "http://localhost/8080";
private final String path = "users";
@Override
public void addUser(User user) {
Client client = ClientBuilder.newClient();
WebTarget webTarget = client.target(url).path(path);
Invocation.Builder invocationBuilder = webTarget.request(MediaType.APPLICATION_JSON);
Response response = invocationBuilder.post(Entity.entity(user, MediaType.APPLICATION_JSON));
}
@Override
public void deleteUser(Long id) {
Client client = ClientBuilder.newClient();
WebTarget target = client.target(url).path(path).path("delete").path(id.toString());
Invocation.Builder builder = target.request(MediaType.APPLICATION_JSON);
Response response = builder.delete();
}
@Override
public List<User> getAllUsers() {
List<User> result = new ArrayList<>();
Client client = ClientBuilder.newClient();
WebTarget webTarget = client.target("http://localhost/8080").path("users");
Invocation.Builder builder = webTarget.request(MediaType.APPLICATION_JSON);
Response response = builder.get();
result = response.readEntity(new GenericType<List<User>>() {
});
return result;
}
}
Klasa głowna
package pl.connectis.pu.gwt.client;
import java.sql.Date;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.RootLayoutPanel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.datepicker.client.DatePicker;
import pl.connectis.pu.gwt.shared.User;
public class GWTClient implements EntryPoint {
UserServiceAsync userServiceAsync = GWT.create(UserService.class);
private final Label nameLbl = new Label("Name");
private final Label usernameLbl = new Label("Username");
private final Label loginLbl = new Label("Login");
private final Label dobLbl = new Label("Date of birth");
private final Label emailLbl = new Label("Email");
private final TextBox nameTxt = new TextBox();
private final TextBox usernameTxt = new TextBox();
private final TextBox loginTxt = new TextBox();
private final DatePicker dobTxt = new DatePicker();
private final TextBox emailTxt = new TextBox();
private final Button addBtn = new Button("Add user");
private final Button getBtn = new Button("Get users");
private final Button updateBtn = new Button("Update user");
private final Button deleteBtn = new Button("Delete user");
HorizontalPanel namePanel = new HorizontalPanel();
HorizontalPanel usernamePanel = new HorizontalPanel();
HorizontalPanel loginPanel = new HorizontalPanel();
HorizontalPanel dobPanel = new HorizontalPanel();
HorizontalPanel emailPanel = new HorizontalPanel();
HorizontalPanel buttonPanel = new HorizontalPanel();
VerticalPanel vp = new VerticalPanel();
@Override
public void onModuleLoad() {
namePanel.add(nameLbl);
namePanel.add(nameTxt);
usernamePanel.add(usernameLbl);
usernamePanel.add(usernameTxt);
loginPanel.add(loginLbl);
loginPanel.add(loginTxt);
dobPanel.add(dobLbl);
dobPanel.add(dobTxt);
emailPanel.add(emailLbl);
emailPanel.add(emailTxt);
buttonPanel.add(addBtn);
buttonPanel.add(deleteBtn);
buttonPanel.add(updateBtn);
buttonPanel.add(getBtn);
vp.add(namePanel);
vp.add(usernamePanel);
vp.add(loginPanel);
vp.add(dobPanel);
vp.add(emailPanel);
vp.add(buttonPanel);
RootLayoutPanel.get().add(vp);
addBtn.addClickHandler(new ClickHandler() {
@Override
public void onClick(ClickEvent event) {
String name = nameTxt.getText();
String username = usernameTxt.getText();
String login = loginTxt.getText();
Date dob = new Date(dobTxt.getHighlightedDate().getTime());
String email = emailTxt.getText();
User resultUSer = new User(name,username,login,dob,email);
System.out.println(resultUSer.toString());
userServiceAsync.addUser(resultUSer, new AsyncCallback<Void>() {
@Override
public void onSuccess(Void result) {
nameTxt.setText("");
usernameTxt.setText("");
loginTxt.setText("");
emailTxt.setText("");
}
@Override
public void onFailure(Throwable caught) {
addBtn.setText("Failure");
}
});
}
});
}
}
Gdyby ktos mial chwile, to prosze o rzucenie okiem i jakies rady :)
Z gory dziekuje