English 中文(简体)
这一请求有什么错误?
原标题:What is wrong with this request?

我正试图提出职位要求,以测试女西岛的方法。 但是,我总是犯错误,没有想法,这是错的。 我正在使用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
最佳回答

如果你使用玻璃鱼服务器,那么你需要将<条码>com.sun. . Enterprises.overrideablejavax Packages 安装到证书。 此外,这里还有一线:

https://stackoverflow.com/questions/7840620/multi Valuedmap- Tenion>Multi ValuedMap exception

希望这将有助于你们。

问题回答

我同小岛一样遭遇同样的问题,并 all忙地照搬同样的错误。 在经过两周的研究之后,这一问题似乎与CORS(交叉领域)的困难有关。

The solution that works for me was a filter that gives some additional information to the response header:

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

2. filter:

    ...
    ResourceConfig rc = new ClasspathResourceConfig("my.resource.package");
    rc.getContainerResponseFilters().add(new CORSFilter());
    return GrizzlyServerFactory.createHttpServer("http://base_uri", rc);
    ...

也许你可以采取这一步骤。





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签