webService-restful

查找服务

@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<NameValuePair> list = new ArrayList<NameValuePair>();
		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 + "]";
	}
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值