我正在尝试对球衣测试方法发布请求。但我总是得到一个错误,不知道,有什么不对。我正在使用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;
}
}
和js代码:
$(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/',
};
});
JS总是在没有描述的情况下返回错误(空的errorThrown),并且jersey在请求时记录错误:
PM com.sun.jersey.server.wadl.generators.WadlGeneratorJAXBGrammarGenerator attachTypes
INFO: Couldn't find JAX-B element for class java.lang.String
答案 0 :(得分:7)
我遇到同样的问题,球衣和灰熊总是得到同样的错误。经过2周的研究,似乎这个问题与CORS(跨域)困难有关。
对我有用的解决方案是一个过滤器,它为响应头提供了一些额外的信息:
import com.sun.jersey.spi.container.ContainerRequest;
import com.sun.jersey.spi.container.ContainerResponse;
import com.sun.jersey.spi.container.ContainerResponseFilter;
class CORSFilter implements ContainerResponseFilter {
@Override
public ContainerResponse filter(ContainerRequest request, ContainerResponse response) {
// instead of "*" you should provide a specific domain name for security reasons
response.getHttpHeaders().add("Access-Control-Allow-Origin", "*");
response.getHttpHeaders().add("Access-Control-Allow-Methods", "PUT, GET, POST, DELETE, OPTIONS");
response.getHttpHeaders().add("Access-Control-Allow-Headers", "Content-Type");
return response;
}
}
将过滤器介绍给灰熊:
...
ResourceConfig rc = new ClasspathResourceConfig("my.resource.package");
rc.getContainerResponseFilters().add(new CORSFilter());
return GrizzlyServerFactory.createHttpServer("http://base_uri", rc);
...
也许你可以把这个用于tomcat。
答案 1 :(得分:2)
如果您使用的是Glassfish服务器,则需要将com.sun.enterprise.overrideablejavaxpackages
设置为JVM。此外还有一个主题:
希望这会对你有所帮助。