diff --git a/src/main/java/com/example/cosmos_serversb/entities/Location.java b/src/main/java/com/example/cosmos_serversb/entities/Location.java new file mode 100644 index 0000000..b4a0667 --- /dev/null +++ b/src/main/java/com/example/cosmos_serversb/entities/Location.java @@ -0,0 +1,31 @@ +package com.example.cosmos_serversb.entities; + +public class Location { + public double latitude; + public double longitude; + + public Location(){ + + } + + public Location(double latitude, double longitude){ + setLatitude(latitude); + setLongitude(longitude); + } + + public void setLatitude(double latitude) { + this.latitude = latitude; + } + + public double getLatitude() { + return latitude; + } + + public void setLongitude(double longitude) { + this.longitude = longitude; + } + + public double getLongitude() { + return longitude; + } +} diff --git a/src/main/java/com/example/cosmos_serversb/entities/Shop.java b/src/main/java/com/example/cosmos_serversb/entities/Shop.java index 11b6c42..f019eb2 100644 --- a/src/main/java/com/example/cosmos_serversb/entities/Shop.java +++ b/src/main/java/com/example/cosmos_serversb/entities/Shop.java @@ -13,53 +13,51 @@ /** * SHOP用のクラス * - * @param longitude string - * @param latitude String - * @param longitudeRange String - * @param latitudeRange String + * @param genre string + * @param code int + * @param location Location * @param shop String * */ @JsonInclude(JsonInclude.Include.NON_NULL) - public String longitude; @JsonView(ShopsView.getShopsView.class) - public String latitude; + public String genre; @JsonView(ShopsView.getShopsView.class) - public String longitudeRange; - public String latitudeRange; - public String shop; - - public Set shops = new HashSet<>(); + public int code; + @JsonView(ShopsView.getShopsView.class) + public Location location; public Shop() { } - public Shop(String longitude, String latitude, String longitudeRange, String latitudeRange, String shop) { - setlongitude(longitude); - setlatitude(latitude); - setlongitudeRange(longitudeRange); - setlatitudeRange(latitudeRange); - setshop(shop); + public Shop(String genre, int code, double latitude, double longitude) { + setGenre(genre); + setCode(code); + setLocation(latitude, longitude); } - public void setlongitude(String longitude) { - this.longitude = longitude; + public void setGenre(String genre) { this.genre = genre; } - public void setlatitude(String latitude) { - this.latitude = latitude; + public String getGenre(){ + return genre; } - public void setlongitudeRange(String longitudeRange) { - this.longitudeRange = longitudeRange; + public void setCode(int code) { + this.code = code; } - public void setlatitudeRange(String latitudeRange) { - this.latitudeRange = latitudeRange; + public int getCode(){ + return code; } - public void setshop(String shop) { - this.shop = shop; + public void setLocation(double latitude, double longitude) { + location.setLatitude(latitude); + location.setLongitude(longitude); + } + + public Location getLocation() { + return location; } } \ No newline at end of file diff --git a/src/main/java/com/example/cosmos_serversb/models/Shops.java b/src/main/java/com/example/cosmos_serversb/models/Shops.java index cc26d93..e6410b7 100644 --- a/src/main/java/com/example/cosmos_serversb/models/Shops.java +++ b/src/main/java/com/example/cosmos_serversb/models/Shops.java @@ -2,10 +2,17 @@ import com.example.common.LogUtils; import com.example.cosmos_serversb.entities.*; +import com.fasterxml.jackson.core.JsonFactory; +import com.fasterxml.jackson.core.JsonParser; +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.ObjectMapper; import org.hibernate.Session; import org.hibernate.SessionFactory; import javax.inject.Singleton; +import java.io.*; +import java.net.HttpURLConnection; +import java.net.URL; @Singleton public class Shops { @@ -26,12 +33,41 @@ return theInstance; } - public static Shop getShop(String longitude, String latitude, String longitudeRange, String latitudeRange, String shop) { - String uri = baseURI + AppName +shop; + public static Shop getShop(String leftLatitude, String leftLongitude, String rightLatitude, String rightLongitude) { + String result = ""; + String genre; + String code; + String location; + String[] split; + try { + URL url = new URL("https://map.yahooapis.jp/search/local/V1/localSearch?bbox=" + leftLatitude + "," + leftLongitude + "," + rightLatitude + "," + rightLongitude + "&gc=02&output=json&appid=dj00aiZpPTVzYzloUDJjS0VMSyZzPWNvbnN1bWVyc2VjcmV0Jng9MGE-"); + HttpURLConnection con = (HttpURLConnection) url.openConnection(); + con.connect(); + String tmp = ""; + BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream())); + while ((tmp = in.readLine()) != null) { + result += tmp; + } + in.close(); + con.disconnect(); + }catch(Exception e){ + e.printStackTrace(); + } + JsonNode jnode = null; + try{ + JsonFactory jfactory = new JsonFactory(); + JsonParser parser = jfactory.createJsonParser(result); + ObjectMapper mapper = new ObjectMapper(); + jnode = mapper.readTree(parser); + }catch(Exception e){ + e.printStackTrace(); + } + genre = jnode.get("Feature").get(0).get("Property").get("Genre").get(0).get("Name").asText(); + code = jnode.get("Feature").get(0).get("Property").get("Genre").get(0).get("Code").asText(); + location = jnode.get("Feature").get(0).get("Geometry").get("Coordinates").asText(); + split = location.split(","); + Shop shop = new Shop(genre, Integer.parseInt(code), Double.parseDouble(split[0]), Double.parseDouble(split[1])); - Session session = SessionManager.getInstance().getSession(); - Shop testShop = new Shop(longitude,latitude,longitudeRange,latitudeRange,shop); - session.save(testShop); - return testShop; + return shop; } } \ No newline at end of file diff --git a/src/main/java/com/example/cosmos_serversb/resources/ShopsRest.java b/src/main/java/com/example/cosmos_serversb/resources/ShopsRest.java index 98fb8a0..73599c5 100644 --- a/src/main/java/com/example/cosmos_serversb/resources/ShopsRest.java +++ b/src/main/java/com/example/cosmos_serversb/resources/ShopsRest.java @@ -17,14 +17,14 @@ public class ShopsRest { @GET - public String getShopsInfo(@QueryParam("longitude") String longitude, - @QueryParam("latitude") String latitude, - @QueryParam("longitudeRange") String longitudeRange, - @QueryParam("latitudeRange") String latitudeRange, - @QueryParam("shop") String shop)throws JsonProcessingException { + public String getShopsInfo(@QueryParam("leftLatitude") String leftLatitude, + @QueryParam("leftLongitude") String leftLongitude, + @QueryParam("rightLatitude") String rightLatitude, + @QueryParam("rightLongitude") String rightLongitude + )throws JsonProcessingException { SessionManager.getInstance().getSession(); ObjectMapper mapper = new ObjectMapper(); - String json = mapper.writerWithView(ShopsView.getShopsView.class).writeValueAsString(Shops.getInstance().getShop(longitude,latitude,longitudeRange,latitudeRange,shop)); + String json = mapper.writerWithView(ShopsView.getShopsView.class).writeValueAsString(Shops.getInstance().getShop(leftLatitude,leftLongitude,rightLatitude,rightLongitude)); SessionManager.getInstance().closeSession(); LogUtils.info("位置情報を取得"); return json; diff --git a/src/test/java/HttpUrlConnectionTest/SampleTest.java b/src/test/java/HttpUrlConnectionTest/SampleTest.java new file mode 100644 index 0000000..d185525 --- /dev/null +++ b/src/test/java/HttpUrlConnectionTest/SampleTest.java @@ -0,0 +1,66 @@ +package HttpUrlConnectionTest; + +import com.example.cosmos_serversb.models.Groups; +import com.fasterxml.jackson.core.JsonFactory; +import com.fasterxml.jackson.core.JsonParser; +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.SerializationFeature; +import com.fasterxml.jackson.databind.node.JsonNodeType; +import jdk.nashorn.internal.ir.ObjectNode; + +import java.io.*; +import java.net.*; + +public class SampleTest { + public static String getResult(String urlString) { + String result = ""; + try { + URL url = new URL(urlString); + HttpURLConnection con = (HttpURLConnection) url.openConnection(); + con.connect(); + String tmp = ""; + BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream())); + while ((tmp = in.readLine()) != null) { + result += tmp; + } + in.close(); + con.disconnect(); + }catch(Exception e){ + e.printStackTrace(); + } + return result; + } + + public static JsonNode getJsonNode(String jsonString){ + JsonNode head = null; + try{ + JsonFactory jfactory = new JsonFactory(); + JsonParser parser = jfactory.createJsonParser(jsonString); + ObjectMapper mapper = new ObjectMapper(); + head = mapper.readTree(parser); + }catch(Exception e){ + e.printStackTrace(); + } + return head; + } + + public static void main(String[] args) { + String Result; + JsonNode json; + String URL = "https://map.yahooapis.jp/search/local/V1/localSearch?bbox=139.727685,35.663782,139.734251,35.669378&gc=02&results=20&output=json&appid=dj00aiZpPTVzYzloUDJjS0VMSyZzPWNvbnN1bWVyc2VjcmV0Jng9MGE-"; + Result = getResult(URL); + json = getJsonNode(Result); + + String genre = json.get("Feature").get(0).get("Property").get("Genre").get(0).get("Name").asText(); + String code = json.get("Feature").get(0).get("Property").get("Genre").get(0).get("Code").asText(); + String location = json.get("Feature").get(0).get("Geometry").get("Coordinates").asText(); + + String[] split = location.split(","); + System.out.println(genre); + System.out.println(Integer.parseInt(code)); + System.out.println(split[0]); + System.out.println(split[1]); + System.out.println(json); + } +}