在postman客户端上执行post请求时找不到405方法

时间:2015-11-30 09:36:40

标签: rest jax-rs postman

每当我执行POST请求时,都会出现错误“405 method not found”,尽管我已经在其中包含了一个必需的POST方法。我不知道为什么会出现错误。

我已经尝试了很多,我没有得到问题所在。请帮我解决一下。

我的资源文件是:

@Path("/customer")
public class CustomerResource {

    CustomerDao service = new CustomerDao();

    @GET
    @Path("/greet")
    public String greetCustomer() {
        return "Hello, Welcome to our site !!";
    }

    @GET
    @Path("/{name}")
    public String getCustomerByName(@PathParam("name") String name) {
        return service.getCustomerByName(name).toString();
    }

    @GET
    @Path("/{firstname}-{lastname}")
    public String getCustomerByName(@PathParam("firstname") String first,
        @PathParam("lastname") String last) {

        Collection<Customer> customerCollection = service.getAllCustomers();
        for (Customer customer : customerCollection) {
            if (customer.getFirstname().equalsIgnoreCase(first)
                && customer.getLastname().equalsIgnoreCase(last)) {
                    return customer.toString();
            }
        }
        return "Customer with " + first + " " + last + "not found";
    }

    @GET
    @Path("/range/{start}")
    @Produces("text/plain")
    public String getCustomersInRange(@QueryParam("start") int start) {
        List<Customer> allCustomerList = new ArrayList<Customer>(service.getAllCustomers());
        List<Customer> customerListInRange = new ArrayList<Customer>();
        if (start < allCustomerList.size()) {
            for (int i = start; i < allCustomerList.size(); i++) {
                customerListInRange.add(allCustomerList.get(i));
            }
            return customerListInRange.toString();
        } else {
            return "No customers in this range";
        }
    }

    @POST
    @Path("/add")
    @Consumes("application/x-www-form-urlencoded")
    public String createCustomer(@FormParam("firstname") String first,
        @FormParam("lastname") String last) {

        Customer newCustomer = new Customer();
        newCustomer.setFirstname(first);
        newCustomer.setLastname(last);
        service.addCustomer(newCustomer);
        return newCustomer.toString();
    }

    @PUT
    @Path("/{id}")
    @Produces("text/plain")
    @Consumes("application/x-www-form-urlencoded")
    public String updateCustomer(@PathParam("id") int id,
        @FormParam("firstname") String first,
        @FormParam("lastname") String last) {

        System.out.println(first + " " + last);
        Customer customer = service.updateCustomer(id, first, last);
        if (customer != null) {
            return "Updated Customer : " + customer.toString();
        } else {
            return "Customer with this id : " + id + "not found";
        }
    }

    @DELETE
    @Path("/{id}")
    @Produces("text/plain")
    public String deleteCustomer(@PathParam("id") int id) {
        Customer customer = service.deleteCustomer(id);
        if (customer != null) {
            return "Deleted Customer: " + customer.toString();
        } else {
            return "Customer with id : " + id + "was not found";
        }
    }
}

我的服务文件是:

public class CustomerDao {

    static private List<Customer> customerList = new ArrayList<Customer>();

    static {
        customerList.add(new Customer(101, "Roy", "Singh", "roy@gmail.com", "Pune"));
        customerList.add(new Customer(102, "Joy", "Jackson", "joy@gmail.com", "Nagpur"));
        customerList.add(new Customer(103, "Soy", "Nichol", "soy@gmail.com", "Mumbai"));
        customerList.add(new Customer(104, "Toy", "Sim", "toy@gmail.com", "Nasik"));
        customerList.add(new Customer(105, "Robin", "Singh", "john@gmail.com", "Nasik"));
    }

    public Customer getCustomerById(int id) {
        for (Customer cust : customerList) {
            if (cust.getId() == id) {
                return cust;
            }
        }

        return null;
    }

    public Customer getCustomerByName(String name) {
        for (Customer cust : customerList) {
            if (cust.getFirstname().equals(name)) {
                return cust;
            }
        }

        return null;
    }

    public List<Customer> getAllCustomers() {
        return customerList;
    }

    public List<Customer> getCustomersByCity(String city) {
        List<Customer> customersByCity = new ArrayList<Customer>();
        for (Customer cust : customerList) {
            if (cust.getCity().equals(city)) {
                customersByCity.add(cust);
            }
        }

        return customersByCity;
    }

    public void addCustomer(Customer cust) {
        customerList.add(cust);
    }

    public Customer updateCustomer(int id, String firstname, String lastname) {
        List<Customer> customers = getAllCustomers();
        for (Customer customer : customers) {
            if (customer.getId() == id) {
                customer.setFirstname(firstname);
                customer.setLastname(lastname);
                return customer;
            }
        }

        return null;
    }

    public Customer deleteCustomer(int id) {
        List<Customer> customers = getAllCustomers();
        for (Customer customer : customers) {
            if (customer.getId() == id) {
                customers.remove(customer);
                return customer;
            }
        }

        return null;
    }

}

使用的路径是:     http://localhost:8080/CustomerServiceApp/rest/customer/add

邮递员的错误是:

  

不允许使用405方法

发生的异常是:

  

org.apache.wink.server.internal.registry.ResourceRegistry - 系统找不到支持POST的resource.CustomerResource类中的任何方法。验证方法是否存在。

     

org.apache.wink.server.internal.RequestProcessor - 调度处理程序链时发生以下错误:WebApplicationException(405),消息'null',同时处理发送到http://localhost:8080/CustomerServiceApp/rest/customer/add的POST请求

0 个答案:

没有答案