REST ガンマン


JAX-RS (Jersey) でRESTインタフェースを実現しました。

type=html/text/json(2種類)/jsonp のパターンを試しました。


【type=html】

http://35.202.98.32:8080/GunmanREST/rest/gunclock/html/26

https://35.202.98.32:8443/GunmanREST/rest/gunclock/html/26

https://35.225.1.11:8443/GunmanREST/rest/gunclock/html/20


【type=text】

http://35.202.98.32:8080/GunmanREST/rest/gunclock/text/15

https://35.202.98.32:8443/GunmanREST/rest/gunclock/text/13

https://35.225.1.11:8443/GunmanREST/rest/gunclock/text/20


【type=json】

http://35.202.98.32:8080/GunmanREST/rest/gunclock/json/20

https://35.202.98.32:8443/GunmanREST/rest/gunclock/json/13

https://35.225.1.11:8443/GunmanREST/rest/gunclock/json/25


【type=json(2)】

http://35.202.98.32:8080/GunmanREST/rest/gunclock/json2/20

https://35.202.98.32:8443/GunmanREST/rest/gunclock/json2/13

https://35.225.1.11:8443/GunmanREST/rest/gunclock/json2/25


【type=jsonp】

http://35.202.98.32:8080/GunmanREST/rest/gunclock/jsonp/20

https://35.202.98.32:8443/GunmanREST/rest/gunclock/jsonp/13

https://35.225.1.11:8443/GunmanREST/rest/gunclock/jsonp/25


JSONPのクライアント(javascript(jQuery+$ajax))


サーバ記述 (JAX-RS (jersey) )
package gunman;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.DefaultValue;
import javax.ws.rs.core.MediaType;

import org.glassfish.jersey.server.JSONP;

@Path("/gunclock/")
public class REST_GunClock {
	
	static String getGunClock(String newline, int clockSize){
		GunClockBean gcb = new GunClockBean();
		gcb.setStrNewline(newline);
		gcb.setClockSize(clockSize);
		String gunclock = gcb.getGunClock();
		return gunclock;
	}

@Path("text/{clocksize}")
	// This method is called if TEXT_PLAIN is request
	@GET
	@Produces(MediaType.TEXT_PLAIN)
	public String getGunClockText(@PathParam("clocksize") final int pClocksize) {
		String gunclock = getGunClock("\n", pClocksize);
		return gunclock;
	}
	

	// This method is called if HTML is request   ★ブラウザアクセスは、こちら(この記述がなければ、TEXT_PLAIN
@Path("html/{clocksize}")
	@GET
	@Produces(MediaType.TEXT_HTML)
	public String getGunClockHtmlWithClocksize(@PathParam("clocksize") final int pClocksize) {
		String gunclock = getGunClock("\n", pClocksize);
		return "<html> " + "<title>" + "GunClock-REST(html)" + "</title>"
				+ "<body><pre>" + gunclock + "</body></pre>" + "</html> ";
	}  

@Path("json/{clocksize}")
	@GET
	@Produces(MediaType.APPLICATION_JSON)
	public Map<String, String> getGunClockJSON(@PathParam("clocksize") final int pClocksize) {
		String gunclock = getGunClock("\n", pClocksize);
		String line[] = gunclock.split("\n");

		Map<String, String> map = new HashMap<>();
		String s = "";
		for ( int i=0; i<line.length; i++){
			if ( i != 0 ) {
				s += ",";
			}
			s += line[i];
		}
		map.put("message", s);
		return map;
	}

@Path("json2/{clocksize}")
	@GET
	@Produces(MediaType.APPLICATION_JSON)
	public List<String> getGunClockJSON2(@PathParam("clocksize") final int pClocksize) {
		String gunclock = getGunClock("\n", pClocksize);
		String line[] = gunclock.split("\n");
		
		List<String> list = new ArrayList<String>();
		for ( int i=0; i<line.length; i++){
			list.add(line[i]);
		}
		return list;
	}


	public class JaxbBean {
		private String message;
		public JaxbBean() {}
		public JaxbBean(final String message) {
			this.message = message;
		}
		public String getMessage() {
			return message;
		}
		public void setMessage(final String message) {
			this.message = message;
		}
	}

@Path("jsonp/{clocksize}")
	@GET
	@JSONP(callback = "callback", queryParam = "callback")
	@Produces({"application/javascript"})
	//public JaxbBean getGunClockJSON(@PathParam("clocksize") final int pClocksize, @QueryParam("callback") @DefaultValue("callback") String callback) {
	public JaxbBean getGunClockJSONP(@PathParam("clocksize") final int pClocksize) {
		String gunclock = getGunClock("\n", pClocksize);
		String line[] = gunclock.split("\n");

		String s = "";
		for ( int i=0; i<line.length; i++){
			if ( i != 0 ) {
				s += ",";
			}
			s += line[i];
		}

		return new JaxbBean(s);
	}

}


クライアント記述 (jQuery ($.ajax) )
//    const endpoint = "http://localhost:8080/GunmanREST/rest/gunclock/jsonp/30";  //OK
//    const endpoint = "http://35.202.98.32:8080/GunmanREST/rest/gunclock/jsonp/26";  //OK
    const endpoint = "https://35.202.98.32:8443/GunmanREST/rest/gunclock/jsonp/26";  //OK
//    const endpoint = "https://35.225.1.11:8443/GunmanREST/rest/gunclock/jsonp/26";   //OK

    $.ajax({
        type: 'GET',
        url: endpoint,
        dataType: 'jsonp',
        data: {
        },
        success: out => {
//            alert(out.message);
            showJson(out.message);
            gunClockAsync(out.message);
        }
    });


function showJson(mes) {
  document.getElementById('jsontext').value = mes;
}


function gunClockAsync(mes) {

  var lines = null;
  var gunClockImage = "";

  lines = mes.split(",");
  for ( i=0; i<lines.length; i++) {
    gunClockImage += lines[i] + "\n";
  }
  document.getElementById('gunclock').value = gunClockImage;
}


WARファイル(GunmanREST.war)


[ ガンマンのページへ戻る ]