我正试图提出职位要求,以测试女西岛的方法。 但是,我总是犯错误,没有想法,这是错的。 我正在使用Tomcat。
Java
@Path("/users")
public class UsersResource {
@GET
@Produces(MediaType.TEXT_PLAIN)
public String sayHello() {
return "Hello Jersey Today";
}
@POST
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public String createUser(User us)
{
//System.out.println("userdata " + us);
User user = new User("testUserName", "testPassword");
return user.toJson();
}
}
用户类别:
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
public class User
{
private String username;
private String password;
public User()
{
super();
}
public User(String name, String pass)
{
super();
this.username = name;
this.password = pass;
}
public User(String username)
{
this.username = username;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String toJson()
{
Gson gson = new GsonBuilder().setPrettyPrinting().create();
String json = gson.toJson(this);
return json;
}
}
And js code:
$(function () {
main = {
// Start updates
start: function(){
var user = {username: "test", password: "pass"};
var encoded = $.toJSON( user );
ajaxPost.ajaxRequest("TodoListing/rest/users", user,
function(data)
{
alert("success with data " + data);
},
function(jqXHR, textStatus, errorThrown) {
alert("error " + textStatus + "/" + errorThrown);
});
}
},
// Ajax request.
ajaxPost = {
ajaxRequest: function(service, data, successSelector, errorSelector) {
$.ajax({
type: POST ,
url: ajaxPost.serverURL + service,
data: data,
contentType: "application/json",
dataType: "application/json",
success: successSelector,
error: errorSelector
});
},
serverURL: http://localhost/ ,
};
});
共同提交文件总是在无描述(错误错误)的情况下回归错误,而妇女委员会则在请求时出现错误:
PM com.sun.jersey.server.wadl.generators.WadlGeneratorJAXBGrammarGenerator attachTypes
INFO: Couldn t find JAX-B element for class java.lang.String