【问题标题】:Rest Client: Javax.ws.rs休息客户端:Javax.ws.rs
【发布时间】:2015-11-14 11:29:00
【问题描述】:

我从 Rest 开始,不知道如何正确实现它。我做了一个练习:我必须使用 javax.ws.rs 标准库中的 RestClient-API 实现一个 Rest-Client,我尝试使用下面的代码,但我得到了一个空指针异常。但是资源在那里,当我直接从浏览器尝试时(http://localhost:8080/sep/rest/customers/112)。现在我的问题是我该如何正确地做到这一点。一些限制,我必须使用 XML(不是 JSON)来支持数据。

我的客户代码:

public Response createCustomer(Customer customer){
        log.info("Starting: Rest Create a Customer with Name: " + Customer.class.getName());
        this.customerWebTarget = this.client.target(URL);

        Response response = this.customerWebTarget.request().
                buildPost(Entity.entity(customer, MediaType.APPLICATION_XML)).invoke();

        log.info("Ending: Rest Create a Customer with Name: " + response.getEntity().getClass().getName());
        return response;
    }

客户资源代码:

@Path("customers")
public class CustomerResource implements IAllowedMethods<Customer> {

    private static final long serialVersionUID = -6367055402693237329L;

    private Logger logger = Logger.getLogger(CustomerResource.class.getName());

    @Inject
    private CustomerService service;

    public CustomerResource() {
        logger.info("create of instance " + this.getClass().getName());
    }

    @Override
    @GET
    @Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
    public Response get() {
        List<Customer> list = service.loadAll(Customer.FINDALL, Customer.class);
        if (list != null && !list.isEmpty()) {
            ResponseCustomerList responseList = new ResponseCustomerList();
            responseList.setList(list);
            return Response.ok(responseList).build();
        }
        return Response.status(Status.NOT_FOUND).build();
    }
.
.
.

客户代码:

import de.ostfalia.sep.adapter.XMLIntegerAdapter;

@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class Customer implements Serializable {

    private static final long serialVersionUID = 80668466040239995L;

    @XmlID
    @XmlJavaTypeAdapter(XMLIntegerAdapter.class)
    private Integer customerNumber;

    private String customerName;

    private String contactLastName;

    private String contactFirstName;

    private String phone;

    private String addressLine1;

    private String addressLine2;

    private String city;

    private String state;

    private String postalCode;

    private String country;

    @XmlIDREF
    private Employee salesRepEmployee;

    private BigDecimal creditLimit;

    private Set<Payment> payments;

    private Set<Order> orders;

    public Customer() {
    }

    public Customer(Integer customernumber) {
        this.customerNumber = customernumber;
    }

    public Customer(Integer customerNumber, String customerName, String contactLastName, String contactFirstName,
            String phone, String addressLine1, String city, String country) {
        this.customerNumber = customerNumber;
        this.customerName = customerName;
        this.contactLastName = contactLastName;
        this.contactFirstName = contactFirstName;
        this.phone = phone;
        this.addressLine1 = addressLine1;
        this.city = city;
        this.country = country;
    }

    public Integer getCustomerNumber() {
        return customerNumber;
    }

    public void setCustomerNumber(Integer customerNumber) {
        this.customerNumber = customerNumber;
    }

    public String getCustomerName() {
        return customerName;
    }

    public void setCustomerName(String customerName) {
        this.customerName = customerName;
    }

    public String getContactLastName() {
        return contactLastName;
    }

    public void setContactLastName(String contactLastName) {
        this.contactLastName = contactLastName;
    }

    public String getContactFirstName() {
        return contactFirstName;
    }

    public void setContactFirstName(String contactFirstName) {
        this.contactFirstName = contactFirstName;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public String getAddressLine1() {
        return addressLine1;
    }

    public void setAddressLine1(String addressLine1) {
        this.addressLine1 = addressLine1;
    }

    public String getAddressLine2() {
        return addressLine2;
    }

    public void setAddressLine2(String addressLine2) {
        this.addressLine2 = addressLine2;
    }

    public String getCity() {
        return city;
    }

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

    public String getState() {
        return state;
    }

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

    public String getPostalCode() {
        return postalCode;
    }

    public void setPostalCode(String postalCode) {
        this.postalCode = postalCode;
    }

    public String getCountry() {
        return country;
    }

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

    public Employee getSalesRepEmployee() {
        return salesRepEmployee;
    }

    public void setSalesRepEmployee(Employee salesRepEmployee) {
        this.salesRepEmployee = salesRepEmployee;
    }

    public BigDecimal getCreditLimit() {
        return creditLimit;
    }

    public void setCreditLimit(BigDecimal creditLimit) {
        this.creditLimit = creditLimit;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (customerNumber != null ? customerNumber.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are
        // not set
        if (!(object instanceof Customer)) {
            return false;
        }
        Customer other = (Customer) object;
        if ((this.customerNumber == null && other.customerNumber != null)
                || (this.customerNumber != null && !this.customerNumber.equals(other.customerNumber))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return customerNumber.toString();
    }

    public Set<Payment> getPayments() {
        return payments;
    }

    public void setPayments(Set<Payment> payments) {
        this.payments = payments;
    }

    public Set<Order> getOrders() {
        return orders;
    }

    public void setOrders(Set<Order> orders) {
        this.orders = orders;
    }
}

【问题讨论】:

    标签: rest wildfly-8 java-ee-7 rest-client


    【解决方案1】:

    代替response.getEntity(),使用response.readEntity(String.class) 以String 的形式获取数据。如果您想将其反序列化为 POJO,只需将该类传递给 readEntity。

    您还应该确保检查状态代码 (response.getStatus()) 以确保它是成功状态。

    【讨论】:

    • 它出来了,它与URL有关......当我在请求中给出URL(localhost:8080/sep/rest/customers/112)时,它可以工作,但是当我像URL +“112一样添加它时“它似乎没有做同样的事情。
    • 如果您想将112 添加到../customers 目标,请使用target(URL).path("112")
    • 我尝试从客户的响应中提取信息,但出现异常,例如缺少一些注释...这是服务器代码。 “找不到内容类型应用程序/xml 的 MessageBodyReader 和类的类型 ....Customer”。我在我的服务器中消化了这个注释。
    • 您的客户类在哪里?用它编辑你的帖子。
    • 我不能通过 response.readEntity(Customer.class) 来读取实体;
    猜你喜欢
    • 1970-01-01
    • 2014-11-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-30
    相关资源
    最近更新 更多