我尝试使用此示例并学习如何调用休息服务,当我输入此代码时出现错误,因为 eclipse 不知道 org.example.Customer; .定义客户客户的错误。如何将其导入我的项目?我尝试使用 eclipse 建议但没有成功。(修复项目设置等)
import java.util.List;
import javax.ws.rs.core.MediaType;
import org.example.Customer;
import com.sun.jersey.api.client.*;
public class JerseyClient {
public static void main(String[] args) {
Client client = Client.create();
WebResource resource =
client.resource("http://localhost:8080/CustomerService/rest/customers");
// Get response as String
String string = resource.path("1")
.accept(MediaType.APPLICATION_XML)
.get(String.class);
System.out.println(string);
// Get response as Customer
Customer customer = resource.path("1") ------"*Here is the error in customer*-------
.accept(MediaType.APPLICATION_XML)
.get(Customer.class);
System.out.println(customer.getLastName() + ", "+ customer.getFirstName());
// Get response as List<Customer>
List<Customer> customers = resource.path("findCustomersByCity/Any%20Town")
.accept(MediaType.APPLICATION_XML)
.get(new GenericType<List<Customer>>(){});
System.out.println(customers.size());
}
}
我已经使用了这个博客的代码
http://blog.bdoughan.com/2010/08/creating-restful-web-service-part-55.html