Rest客户端:Javax.ws.rs

Rest客户端:Javax.ws.rs,rest,wildfly-8,java-ee-7,rest-client,Rest,Wildfly 8,Java Ee 7,Rest Client,我从Rest开始,不知道如何正确地实现它。我得到了一个练习:我必须使用javax.ws.rs标准库中的RestClient API实现一个Rest客户机,我尝试使用下面的代码,但是我得到了一个空指针异常。但是,当我直接从浏览器()尝试时,资源就在那里。现在我的问题是我如何才能正确地做到这一点。在一些限制条件下,我必须使用XML(而不是JSON)作为数据支持 Hier我的客户代码: public Response createCustomer(Customer customer){

我从Rest开始,不知道如何正确地实现它。我得到了一个练习:我必须使用javax.ws.rs标准库中的RestClient API实现一个Rest客户机,我尝试使用下面的代码,但是我得到了一个空指针异常。但是,当我直接从浏览器()尝试时,资源就在那里。现在我的问题是我如何才能正确地做到这一点。在一些限制条件下,我必须使用XML(而不是JSON)作为数据支持

Hier我的客户代码:

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();
    }
.
.
.
@Path(“客户”)
公共类CustomerResource实现了IAllowedMethods{
私有静态最终长serialVersionUID=-6367055402693237329L;
private Logger=Logger.getLogger(CustomerResource.class.getName());
@注入
私人客户服务;
公共客户资源(){
logger.info(“创建实例”+this.getClass().getName());
}
@凌驾
@得到
@产生({MediaType.APPLICATION_XML,MediaType.APPLICATION_JSON})
公众反应{
List=service.loadAll(Customer.FINDALL,Customer.class);
if(list!=null&&!list.isEmpty()){
ResponseCustomerList responseList=新的ResponseCustomerList();
响应列表设置列表(列表);
返回Response.ok(responseList.build();
}
返回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;
    }
}
import de.ostfalia.sep.adapter.XMLIntegerAdapter;
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
公共类Customer实现了可序列化{
私有静态最终长serialVersionUID=80668466040239995L;
@XmlID
@XmlJavaTypeAdapter(XMLIntegerAdapter.class)
私有整数customerNumber;
私有字符串客户名称;
私有字符串contactLastName;
私有字符串contactFirstName;
私人电话;
私有字符串地址行1;
私有字符串地址行2;
私人城市;
私有字符串状态;
专用字符串后代码;
私人国家;
@XmlIDREF
私人雇员销售雇员;
私人信用额度;
私人设定支付;
私人定单;
公众客户(){
}
公共客户(整数客户编号){
this.customerNumber=customerNumber;
}
公共客户(整数customerNumber、字符串customerName、字符串contactLastName、字符串contactFirstName、,
字符串电话,字符串地址Line1,字符串城市,字符串国家/地区){
this.customerNumber=customerNumber;
this.customerName=客户名称;
this.contactLastName=contactLastName;
this.contactFirstName=contactFirstName;
this.phone=电话;
this.addressLine1=addressLine1;
this.city=城市;
这个国家=国家;
}
公共整数getCustomerNumber(){
返回客户编号;
}
public void setCustomerNumber(整数customerNumber){
this.customerNumber=customerNumber;
}
公共字符串getCustomerName(){
返回客户名称;
}
public void setCustomerName(字符串customerName){
this.customerName=客户名称;
}
公共字符串getContactLastName(){
返回contactLastName;
}
public void setContactLastName(字符串contactLastName){
this.contactLastName=contactLastName;
}
公共字符串getContactFirstName(){
返回contactFirstName;
}
public void setContactFirstName(字符串contactFirstName){
this.contactFirstName=contactFirstName;
}
公共字符串getPhone(){
回电话;
}
公用无效设置电话(字符串电话){
this.phone=电话;
}
公共字符串getAddressLine1(){
返回地址行1;
}
公共无效setAddressLine1(字符串addressLine1){
this.addressLine1=addressLine1;
}
公共字符串getAddressLine2(){
返回地址行2;
}
公共无效setAddressLine2(字符串addressLine2){
this.addressLine2=addressLine2;
}
公共字符串getCity(){
回归城市;
}
公共城市(字符串城市){
this.city=城市;
}
公共字符串getState(){
返回状态;
}
公共无效设置状态(字符串状态){
this.state=状态;
}
公共字符串getPostalCode(){
返回后代码;
}
公共无效setPostalCode(字符串postalCode){
this.postalCode=postalCode;
}
公共字符串getCountry(){
返回国;
}
公共国家/地区(字符串国家/地区){
这个国家=国家;
}
公共雇员getSalesRepEmployee(){
返回员工;
}
public void setSalesRepEmployee(员工salesRepEmployee){
this.salesRepEmployee=salesRepEmployee;
}
公共BigDecimal getCreditLimit(){
退货限额;
}
公共无效setCreditLimit(BigDecimal creditLimit){
this.creditLimit=creditLimit;
}
@凌驾
公共int hashCode(){
int hash=0;
hash+=(customerNumber!=null?customerNumber.hashCode():0);
返回散列;
}
@凌驾
公共布尔等于(对象){
//TODO:警告-如果id字段为
//未设定
if(!(客户的对象实例)){
返回false;
}
客户其他=(客户)对象;
if((this.customerNumber==null&&other.customerNumber!=null)
||(this.customerNumber!=null&!this.customerNumber.equals(other.customerNumber))){
返回false;
}
返回true;
}
@凌驾
公共字符串toString(){
返回customerNumber.toString();
}
公共设置getPayments(){
归还款项;
}
公共作废设置付款(设置付款){
这是付款=付款;
}
公共图书馆