Rest call java sample code [original]

1. Server-side sample code (how to build a rest service, please see the previous article)
@Service
@Path("/msMqMessage")
public class MsMqServiceImpl implements MsMqService{	
	@Resource
	private MsMqUtil msMqUtil;
	
	@POST  
         @Consumes(MediaType.APPLICATION_FORM_URLENCODED)
	@Produces(MediaType.APPLICATION_JSON)  
	public void send(@FormParam("label") String label, @FormParam("body") String body){
		msMqUtil.send(label, body);
	}
	
	@GET
	@Produces(MediaType.APPLICATION_JSON)
	public List<Map<String, String>> receive(@QueryParam("count") int count)
	        throws java.io.UnsupportedEncodingException{
		return msMqUtil.receive(count);
	}
	
	public MsMqUtil getMsMqUtil() {
		return msMqUtil;
	}

	public void setMsMqUtil(MsMqUtil msMqUtil) {
		this.msMqUtil = msMqUtil;
	}
}


2. Client calling code
public class RestClient {
	private static void post() {
		Client client = ClientBuilder.newClient();
		WebTarget target = client.target("http://localhost:8080/cbec-msmq/msMqMessage");
		Form form=new Form();
		form.param("label", "123123");
		form.param("body", "hello");
		Response response = target.request().post(Entity.form(form));
		response.close();
	}
	
	private static void get(){
		Client client = ClientBuilder.newClient();
		WebTarget target = client.target("http://localhost:8080/cbec-msmq/msMqMessage")
				.queryParam("count", 1);
		Response response = target.request().get();
		System.out.println(response.readEntity(String.class));
	}
	
	public static void main(String[] args){
		post();
		get();
	}
}

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326880950&siteId=291194637