不带'root'的JSon对象发送

问题描述 投票:0回答:1

使用Java RestEasy JSon API,在下一个WebService客户端代码中,我想问您是否可以通过这种方式发送de JSon对象:

{
"title" : "Some title book", 
"author" : "Some author", 
"price" : 99.9
}

...代替此:

{
"book" :
{
"title" : "Some title book", 
"author" : "Some author", 
"price" : 99.9
}
}

我的意思是我不想发送'root'元素“ book”。

import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;

@XmlRootElement(name = "book")
@XmlType(propOrder = {"title", "author", "price"})
public class Book {

    private String title; 
    private String author; 
    private Double price;

    public Book() {
        super();
    }

    public String getTitle() {
        return title;
    }
    public void setTitle(String title) {
        this.title = title;
    }

    public String getAuthor() {
        return author;
    }
    public void setAuthor(String author) {
        this.author = author;
    }

    public Double getPrice() {
        return price;
    }
    public void setPrice(Double price) {
        this.price = price;
    }

}

import javax.ws.rs.client.Entity;
import javax.ws.rs.core.Response;

import org.jboss.resteasy.client.jaxrs.ResteasyClient;
import org.jboss.resteasy.client.jaxrs.ResteasyClientBuilder;
import org.jboss.resteasy.client.jaxrs.ResteasyWebTarget;

import com.fasterxml.jackson.databind.ObjectMapper;

public class BookClient {

    public static void main(String[] args) {

        Response httpResponse = null;
        try {   
            String url = "http://someserver/somepath/etc";

            ResteasyClient client = new ResteasyClientBuilder().build();
            ResteasyWebTarget target = client.target(url);

            Book libro1 = new Book();
                libro1.setAuthor("Some author");
                libro1.setPrice(99.9);
                libro1.setTitle("Some title book");

            String jsonEnviado = new ObjectMapper().writerWithDefaultPrettyPrinter().writeValueAsString(libro1);

            Entity<Book> entidad = Entity.entity(libro1, "application/json");
            httpResponse = target.request().post(entidad);

            Respuesta respuesta = httpResponse.readEntity(Respuesta.class); 
            if (respuesta != null) {
                String jsonRecibido = new ObjectMapper().writerWithDefaultPrettyPrinter().writeValueAsString(respuesta);
            }

            int status = httpResponse.getStatus();
            if (status == 200) {
                logTexto = "OK. Status: " + status;
                System.out.println(logTexto);
            } else { 
                logTexto = "KO. Status: " + status;
                throw new Exception(logTexto);
            }

        } catch(Exception e) {
            System.out.println("EXCEPTION! " + e.getMessage());
            e.printStackTrace();
        } finally {         
            if (httpResponse != null) {
                try {
                    httpResponse.close();
                } catch (Exception e) {
                    //IGNORE IT
                }                   
            }   
        }

    }

}

谢谢!

java json rest resteasy
1个回答
0
投票

最后,我用它来避免发送根元素'book':

//Entity<Book> entidad = Entity.entity(libro1, "application/json");
//httpResponse = target.request().post(entidad);

Entity<String> entidad = Entity.entity(jsonEnviado, "application/json");
httpResponse = target.request().post(entidad);
© www.soinside.com 2019 - 2024. All rights reserved.