RESTEasy 상호 작용 Post 및 Get

6456 단어 resteasy
RESTEasy 상호 작용 Post 및 Get
 
1, Webservice 서버측 코드:
@GET
    @POST
    @Path("books888/mapped8")
    @Consumes({MediaType.MEDIA_TYPE_WILDCARD})
    @Produces({MediaType.APPLICATION_JSON,MediaType.APPLICATION_XML})
    public Customers getBooksMapped8(@FormParam("author") String author,@QueryParam("id") String idStr, MultivaluedMap<String,String> form) {
        Customers customers = new Customers();
        List<Customer> list = new ArrayList<Customer>();
        Customer customer;
        customer = new Customer();
        customer.setId(1);
        customer.setFirstName("Bill");
        customer.setLastName("Burke");
        customer.setStreet("263 Clarendon Street");
        customer.setCity("Boston");
        customer.setState("MA");
        customer.setZip("02115");
        customer.setCountry("USA");
        list.add(customer);
        customers.setCustomers(list);
        return customers;
    }

 
2, Webservice 클라이언트 코드:
private static void testGet() throws Exception {
        String url = URL + "library/books888/mapped8";
        ClientRequest request = new ClientRequest(url);
        request.queryParameter("author", "Jerval");
        request.queryParameter("id", 232123);
        request.queryParameter("test", "test");
        request.accept(MediaType.APPLICATION_XML);
        System.out.println(request.getTarget(String.class));
        Customers customers = request.getTarget(Customers.class);
        System.out.println(customers);
    }
    
    private static void testPost() throws Exception {
        String url = URL + "library/books888/mapped8";
        ClientRequest request = new ClientRequest(url);
        request.formParameter("author", "Jerval2");
        request.formParameter("id", 2321232);
        request.formParameter("test", "test2");
        System.out.println(request.postTarget(String.class));
        Customers customers = request.postTarget(Customers.class);
        System.out.println(customers);
    }

 
3, 관련 객체 코드:
@XmlRootElement(name = "customers")
public class Customers {
    private List<Customer> customers;

    @XmlElementRef
    public List<Customer> getCustomers() {
        return customers;
    }

    public void setCustomers(List<Customer> customers) {
        this.customers = customers;
    }

    @Override
    public String toString() {
        return "Customers [customers=" + customers + "]";
    }
}

 
 
@XmlRootElement(name = "customer")
public class Customer {
    private int id;
    private String firstName;
    private String lastName;
    private String street;
    private String city;
    private String state;
    private String zip;
    private String country;

    @XmlAttribute
    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    @XmlElement(name = "first-name")
    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    @XmlElement(name = "last-name")
    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    @XmlElement
    public String getStreet() {
        return street;
    }

    public void setStreet(String street) {
        this.street = street;
    }

    @XmlElement
    public String getCity() {
        return city;
    }

    public void setCity(String city) {
        this.city = city;
    }

    @XmlElement
    public String getState() {
        return state;
    }

    public void setState(String state) {
        this.state = state;
    }

    @XmlElement
    public String getZip() {
        return zip;
    }

    public void setZip(String zip) {
        this.zip = zip;
    }

    @XmlElement
    public String getCountry() {
        return country;
    }

    public void setCountry(String country) {
        this.country = country;
    }

    @Override
    public String toString() {
        return "Customer [id=" + id + ", firstName=" + firstName + ", lastName=" + lastName + ", street=" + street + ", city=" + city
                + ", state=" + state + ", zip=" + zip + ", country=" + country + "]";
    }
}

4, 테스트 결과:
{"customers":{"customer":{"@id":"1","city":"Boston","country":"USA","first-name":"Bill","last-name":"Burke","state":"MA","street":"263 Clarendon Street","zip":"02115"}}}
Customers [customers=[Customer [id=1, firstName=Bill, lastName=Burke, street=263 Clarendon Street, city=Boston, state=MA, zip=02115, country=USA]]]
{"customers":{"customer":{"@id":"1","city":"Boston","country":"USA","first-name":"Bill","last-name":"Burke","state":"MA","street":"263 Clarendon Street","zip":"02115"}}}
Customers [customers=[Customer [id=1, firstName=Bill, lastName=Burke, street=263 Clarendon Street, city=Boston, state=MA, zip=02115, country=USA]]]

좋은 웹페이지 즐겨찾기