简体   繁体   English

如何使用jersey和java在客户端和Web服务休息之间传输对象

[英]How to transfer object between client and webservice rest using jersey and java

i'm new in JAX-WS and i've created a test sample like: 我是JAX-WS的新手,我创建了一个测试样本,如:

SERVER: 服务器:

@Path("/login")
public class Login {
   public Login(){
      initDB();
   }
    @GET
    @Path("{username}/{password}")  
    @Produces(MediaType.TEXT_PLAIN)
    public String login(@PathParam("username") String username,@PathParam("password") String password) {
     int id = db.login(username, password);
     return ""+id;
    }
} 

CLIENT: 客户:

public class WSConnection {

    private ClientConfig config;
    private Client client;
    private WebResource service;
    public WSConnection(){
        config = new DefaultClientConfig();
        client = Client.create(config);
        service = client.resource(getBaseURI());
    }
    public int login(String username,String password){
        return Integer.parseInt(service.path("rest").path("login").path(username).path(password).accept(
                MediaType.TEXT_PLAIN).get(String.class));
    }
    private URI getBaseURI() {
        return UriBuilder.fromUri(
                "http://localhost:8080/Project2").build();
    }
}

In server, on method login, i return the id of the user selected from username and password. 在服务器中,在方法登录时,我返回从用户名和密码中选择的用户的id。 If i want to return the object: 如果我想返回对象:

public class Utente {

    private int id;
    private String username;
    private String nome;
    private String cognome;

    public Utente(int id, String username, String nome, String cognome) {
        this.id = id;
        this.username = username;
        this.nome = nome;
        this.cognome = cognome;
    }
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getUsername() {
        return username;
    }
    public void setUsername(String username) {
        this.username = username;
    }
    public String getNome() {
        return nome;
    }
    public void setNome(String nome) {
        this.nome = nome;
    }
    public String getCognome() {
        return cognome;
    }
    public void setCognome(String cognome) {
        this.cognome = cognome;
    }   
}

What i have to do? 我该怎么办? can someone explain me ? 有人可以解释一下吗? thank you!!! 谢谢!!! :) :)

Annotate your object with @XmlRootElement and change @Produces(MediaType.TEXT_PLAIN) on your resource to @Produces(MediaType.APPLICATION_XML). 使用@XmlRootElement注释对象并将资源上的@Produces(MediaType.TEXT_PLAIN)更改为@Produces(MediaType.APPLICATION_XML)。 Return that object from the resource method - it will get automatically sent as XML to the client. 从资源方法返回该对象 - 它将自动作为XML发送到客户端。 On the client side you can do: 在客户端,您可以:

Utente u = service.path("rest").path("login").path(username).path(password)
           .accept(MediaType.APPLICATION_XML).get(Utente.class);

Btw, it is a bad idea to map login operation to HTTP GET - you should use POST instead. 顺便说一句,将登录操作映射到HTTP GET是一个坏主意 - 你应该使用POST。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM