restの呼び出しjavaサンプルコード【オリジナル】
詳細
1.サービス側サンプルコード(restサービスの構築方法は、前編を参照)
2.クライアント呼び出しコード
1.サービス側サンプルコード(restサービスの構築方法は、前編を参照)
@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
2.クライアント呼び出しコード
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();
}
}