查找服务
@Path("/crm")
public class CustomerService {
@Path("/customer/{id}")
@GET
@Produces("application/json")
public Customer getCustomerById(@PathParam("id") String id){
System.out.println("getId"+id);
Customer custom = new Customer(id,"小红果",19);
return custom;
}
@Path("/addCustomer")
@POST
@Consumes("application/x-www-form-urlencoded") //参数为键值对
public String addCustomer(@BeanParam Customer customer){ //@beanParam ,在springmvc自
//在本这个服务端,不许用这个声明,并且在类的变量里边指明 key
System.err.println(customer);
return "success"+customer.toString();
}
}
查找客户端
public static void main(String[] args) throws ClientProtocolException, IOException {
CloseableHttpClient build = HttpClientBuilder.create().build();
HttpGet httpGet = new HttpGet("http://192.168.0.107:9996/getConsumer/crm/customer/3");
CloseableHttpResponse response = build.execute(httpGet);
HttpEntity entity = response.getEntity();
if(response.getStatusLine().getStatusCode()==HttpStatus.SC_OK){
String string = EntityUtils.toString(entity,"utf-8");
System.out.println("成功"+string);
}else{
String string = EntityUtils.toString(entity,"utf-8");
System.err.println(string);
}
EntityUtils.consume(entity);
build.close();
}
启动服务
public class MainServer {
public static void main(String[] args) {
JAXRSServerFactoryBean jaxWsServerFactoryBean = new JAXRSServerFactoryBean();
jaxWsServerFactoryBean.setAddress("http://192.168.0.107:9996/getConsumer");
jaxWsServerFactoryBean.setResourceClasses(CustomerService.class);
Server server = jaxWsServerFactoryBean.create();
server.start();
System.out.println("开张了");
}
}
添加对象类 客户端
public class AddCustomerClient {
public static void main(String[] args) throws ClientProtocolException, IOException {
CloseableHttpClient build = HttpClientBuilder.create().build();
HttpPost httpPost = new HttpPost("http://192.168.0.107:9996/getConsumer/crm/addCustomer");
List list = new ArrayList();
list.add(new BasicNameValuePair("id","001"));
list.add(new BasicNameValuePair("name","芳芳"));
list.add(new BasicNameValuePair("age","18"));
HttpEntity httpEntity = new UrlEncodedFormEntity(list,"utf-8");
httpPost.setEntity(httpEntity);
CloseableHttpResponse response = build.execute(httpPost);
HttpEntity entity = response.getEntity();
if(response.getStatusLine().getStatusCode()==HttpStatus.SC_OK){
String string = EntityUtils.toString(entity,"utf-8");
System.out.println("成功"+string);
}else{
String string = EntityUtils.toString(entity,"utf-8");
System.err.println(string);
}
EntityUtils.consume(entity);
build.close();
}
添加对象类
@XmlRootElement
public class Customer {
public Customer() {
}
@FormParam("id") //添加的时候向服务器传参数必须使用这个
private String id;
@FormParam("name")
private String name;
@FormParam("age")
private Integer age;
public Customer(String id, String name, Integer age) {
super();
this.id = id;
this.name = name;
this.age = age;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
@Override
public String toString() {
return "Customer [id=" + id + ", name=" + name + ", age=" + age + "]";
}
}