티스토리 뷰
1 . google api 검색 후 Google API 콘솔이라는 사이트로 입장
2 . 많은 API를 제공하지만 Translate API ( 번역 )를 사용해볼 것임
3 . API 키를 생성 해야됨
4 . 브라우저 키로 생성
5 . API키를 발급 받음
6. 이제 코드작성..!! 메이븐으로 필요한 라이브러리 세팅
( JSON 으로 받아오기 때문에 json-simple 라이브러리 및 spring 관련 라이브러리 추가 ... )
TranslateController.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | package controller; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.servlet.ModelAndView; import service.TranslateService; @Controller public class TranslateController { @Autowired private TranslateService tService; @RequestMapping("trans.do") public ModelAndView trans(@RequestParam(required=false) String text, String source, String target){ ModelAndView mav = new ModelAndView(); if(text!= null){ mav.addObject("result", tService.TranslateService(text, source, target)); } mav.setViewName("trans"); return mav; } } | cs |
TranslateService.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 | package service; import java.io.IOException; import java.net.URI; import java.net.URISyntaxException; import org.apache.http.HttpResponse; import org.apache.http.ParseException; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpGet; import org.apache.http.client.utils.URIBuilder; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.util.EntityUtils; import org.json.simple.JSONArray; import org.json.simple.JSONObject; import org.json.simple.parser.JSONParser; import org.springframework.stereotype.Service; @Service public class TranslateService { public String TranslateService(String text, String source, String target){ String key = "AIzaSyCAbRDD_kZDP8WYeXwv19SCFUOpb8Lciso"; // API 키 HttpClient client = new DefaultHttpClient(); URI uri; String result = null; JSONArray jsonArray = null; JSONObject jsonObject = null; JSONObject jsonObject2 = null; JSONObject jsonObject3 = null; try { uri = new URIBuilder("https://www.googleapis.com/language/translate/v2") .addParameter("key", key) .addParameter("q", text) .addParameter("source", source) .addParameter("target", target) .build(); HttpGet httpGet = new HttpGet(uri); HttpResponse resp = client.execute(httpGet); result = EntityUtils.toString(resp.getEntity(), "UTF-8"); JSONParser parser = new JSONParser(); jsonObject = (JSONObject)parser.parse(result); jsonObject2 = (JSONObject)jsonObject.get("data"); jsonArray = (JSONArray) jsonObject2.get("translations"); jsonObject3 = (JSONObject) jsonArray.get(0); } catch (URISyntaxException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (ParseException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (org.json.simple.parser.ParseException e) { // TODO Auto-generated catch block e.printStackTrace(); } String r = (String) jsonObject3.get("translatedText"); return r; } } | cs |
Trans.jsp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <center> <form action="trans.do"> <tr> <td><input type="text" name="text"> <select name="source" size="1"> <option value="ko" >한국어</option> <option value="en" seleted>영어</option> </select> <br><br> <input type="submit" value="번역"></td> </tr> <br> <br> <br> <br> <br> 번역결과 <select name="target" size="1"> <option value="ko" seleted>한국어</option> <option value="en" >영어</option> </select> <br> <textarea rows="2" cols="20">${result }</textarea> </form> </center> </body> </html> | cs |
web.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1"> <display-name>Spring-Google-Translate</display-name> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> <welcome-file>default.html</welcome-file> <welcome-file>default.htm</welcome-file> <welcome-file>default.jsp</welcome-file> </welcome-file-list> <servlet> <servlet-name>dispatcher</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>dispatcher</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> </web-app> | cs |
applicationContext.xml
1 2 3 4 5 6 7 8 9 10 | <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd"> <context:component-scan base-package="service"/> </beans> | cs |
dispatcher-servlet.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 | <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd"> <context:component-scan base-package="controller"/> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="suffix" value=".jsp"/> <property name="prefix" value="jsp/"/> </bean> </beans> | cs |
'Framework > Spring' 카테고리의 다른 글
[Spring] Spring AOP (415) | 2016.06.10 |
---|---|
[Spring] 스프링의 모듈 (0) | 2016.05.26 |
댓글