반응형

1. Maven Archetype

Archetype이란 Maven(Spring, Spring boot)에서 나만의 template/boilerplate를 제작하는 방법이다.

 

2. 기본세팅

2.1 Setting.xml

- Archetype 사용하기 위해선 setting.xml 파일이 필요하다.

- setting.xml 파일이 없다면, 내문서의 .m2 폴더에 setting.xml 파일을 생성한다.

<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0
                      http://maven.apache.org/xsd/settings-1.0.0.xsd">


  <localRepository>${user.home}/.m2/repository</localRepository>

</settings>

 

2.2 Maven

https://maven.apache.org/download.cgi

 

Maven – Download Apache Maven

Downloading Apache Maven 3.6.3 Apache Maven 3.6.3 is the latest release and recommended version for all users. The currently selected download mirror is http://us.mirrors.quenda.co/apache/. If you encounter a problem with this mirror, please select another

maven.apache.org

- Archetype은 기본적으로 Maven명령어를 사용하기 때문에 Maven이 설치되어 있어야 한다.

- 위 사이트에 접속하여 .zip 파일을 다운받아 적절한 위치에 압축을 해제한 뒤 고급시스템 설정의 path에

압축 해제한 경로에 맞게(Ex : C:\apache-maven-3.6.3\bin) path를 추가한다.

 

※ Spring/Spring Boot는 Maven 설치안하는데 Maven이 되는데, 그 이유는 Spring/Spring Boot의 경우 프로젝트를 생성하게 되면, 자동으로 mvnw.cmd 라는 파일이 생기기 때문이다.

 

3. Archetype 사용방법

3.1 미리 생성한 프로젝트에서 생성 후 로컬배포

- Spring/Sprin Boot에서 나만의 template/boilerplate를 제작하였다면, IDE Terminal에서 프로젝트 Root 경로에 아래의 명령어를 입력한다

mvn archetype:create-from-project

 

- 명령어를 입력하게되면 target/generated-sources/archetype 폴더가 생성된다. 생성한 폴더에 접근한다.

cd target/generated-soruces/archetype

 

- IDE Terminal에서 mvn install 또는 mvn deploy를 실행한다.

mvn install

  - mvn install은 local 저장소에 배포를 하며, .m2 폴더에 저장된다.

  - mvn deploy는 maven 저장소에 배포하는 것이며, 사설 nexus 저장소에 배포가능하다.

 

 

- mvn archetype:generate -DarchetypeCatalog=local 명령어를 통해 정상적으로 등록되었는지 확인한다.

mvn archetype:generate -DarchetypeCatalog=local

 

- mvn install 했으나 로컬에 배포되지 않은 경우 mvn archetype:update-locate-catalog 를 입력한다.

mvn archetype:update-locate-catalog

 

3.2 사설 저장소에 배포

- mvn deploy로 사설 nexus에 등록할 경우 target/generated-soruces/archetype 에 있는 pom.xml에 아래와 같이 등록한다. 

	<distributionManagement>
        <repository>
            <id>test-releases</id> 
            <name>Releases Repository</name>
            <url>http://127.0.0.1/repository/maven-releases/</url>
        </repository>
        <snapshotRepository>
            <id>test-snapshots</id>
            <name>Snapshots Repository</name>
            <url>http://127.0.0.1/repository/maven-snapshots/</url> 
        </snapshotRepository>
    </distributionManagement>

 

- 만약 사설 저장소에 권한이 필요해 계정 및 비밀번호가 필요하면, .m2 폴더의 settings.xml에 계정과 비밀번호를 등록한다. server에 들어가는 id 와 위 xml의 id가 같아야한다.

<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0
                      http://maven.apache.org/xsd/settings-1.0.0.xsd">


  <localRepository>${user.home}/.m2/repository</localRepository>

  <servers>
    <server>
      <id>test-releases</id>
      <username>admin</username>
      <password>testpw</password>
    </server>
     <server>
      <id>test-snapshots</id>
      <username>admin</username>
      <password>testpw</password>
    </server>

  </servers>
 
</settings>

 

- 등록 후 target/generated-soruces/archetype 경로에서 mvn deploy를 하면 사설저장소에 저장된다.

- 사설 저장소에 있는 것을 사용 및 확인하고 싶다면 setting.xml에 mirror를 작성해줘야 한다.

<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0
                      http://maven.apache.org/xsd/settings-1.0.0.xsd">


  <localRepository>${user.home}/.m2/repository</localRepository>

 <mirrors>
	<mirror>
	<id>test-snapshots-repo</id>
	<mirrorOf>*</mirrorOf>
	<name>Mirror.</name>
	<url>http://127.0.0.1/repository/maven-snapshots/</url>
	</mirror>
</mirrors>

  <servers>
    <server>
      <id>test-releases</id>
      <username>admin</username>
      <password>testpw</password>
    </server>
     <server>
      <id>test-snapshots</id>
      <username>admin</username>
      <password>testpw</password>
    </server>

  </servers>
 
</settings>

 

- 등록 후 mvn archetype:generate 하면 등록한 archetype 항목이 나올것이다.

 

4. 프로젝트 생성

- mvn archetype:generate 명령어 후 플젝 선택시 입력하는 것들은

target/generated-soruces/archetype 폴더의 pom.xml에 있는

- groupid

- artifactId

- version

이며, 똑같이 입력해주면 프로젝트가 생성된다.

 

반응형
반응형

이클립스에서 Kotlin + Spring Boot 2.0 + Webflux 사용방법이다.

DB연동은 다음에 하며 간단한 프로젝트 생성 후 Get, Post, Put, Delete 부터시작한다.

 

R2DBC MySQL 연결 -

2020/06/04 - [Develop/Spring Boot] - Kotiln Spring Boot Webflux r2dbc(MySQL)

 

Kotiln Spring Boot Webflux r2dbc(MySQL)

이전 글에 이어서 Kotlin + Spring Boot 2.0 + Webflux + R2DBC(MySQL)사용방법이다. 이전글 - Kotlin Spring Boot Webflux (Eclipse) 2020/04/26 - [Develop/Spring Boot] - Kotlin Spring Boot Webflux (Eclipse..

wky.kr

 

1.

https://start.spring.io/

접속하여 간단히 프로젝트 부터 생성한다. Dependencies는 Webflux를 위해 Reactive Web만 추가한다.

 

GENERATE를 눌러 프로젝트를 다운받은 후 이클립스에 추가한다.

 

2. 실행하기 전에 Intellij 에서는 실행을 안해봐서 잘 모르겠지만, 이클립스에서 실행할 경우 final 에러가 뜨는데 그 경우

Spring Boot를 실행하는 메인 클래스에 open이라는 키워드를 붙여준다.

 

3.다음 간단히 프로젝트 구조를 잡아준다.

Model에는 Spring이나 Webflux가 아닌 SpringBoot 에서의 VO를 작성해준다.

 

4. Handler를 작성해준다. Handler는 Spring이나 Webflux가 아닌 SpringBoot 에서의 Controller라고 생각하면된다.

 

5. router를 작성해준다. router에서 URL을 설정해 Handler로 연결 시켜준다.

Configuration Annotation을 사용하는 경우 class 앞에 꼭 open 을 붙여주어야 하며,

open이 붙은 클래스들의 함수들 또한 앞에 open을 꼭 붙여주어야 한다.

 

 

6. Postman으로 간단히 실행해본다.

GET : Handler에서 작성한대로 query parameter를 넣어 그 결과를 반환한다.

 

POST : Handler에서 작성한대로 id, name을 전송하게 되면 id, name을 출력한다.

 

PUT : Handler에서 작성한대로 id, name을 전송하게 되면 id, name을 출력한다.

 

DELETE : Handler에서 작성한대로 id, name을 전송하게 되면 id, name을 출력한다.

 

다음에 시간나면 데이터베이스 연결을 위해 ReactiveCrudRepository사용법과 Join하는 방법을 올려야겠다.

 

Kotlin Webflux R2DBC MySQL - 

2020/06/04 - [Develop/Spring Boot] - Kotiln Spring Boot Webflux r2dbc(MySQL)

 

Kotiln Spring Boot Webflux r2dbc(MySQL)

이전 글에 이어서 Kotlin + Spring Boot 2.0 + Webflux + R2DBC(MySQL)사용방법이다. 이전글 - Kotlin Spring Boot Webflux (Eclipse) 2020/04/26 - [Develop/Spring Boot] - Kotlin Spring Boot Webflux (Eclipse..

wky.kr

 

반응형
반응형


Spring boot ElasticBeanstalk 환경변수설정시 주의사항이다.

이것 때매 조금 고생을 한 적이 있다.


Spring boot에서 application.properties 작성시 대표적으로 server.port 등 이렇게 . (마침표)을 써서 사용한다

하지만 ElasticBeanstalk 에서는 아래의 사진처럼 이를 _ (언더스코어)로 구분하는데

Spring boot의  application.properties에는 - (하이픈)이 사용되는 것이 있다. 대충 이런것들 spring.aop.proxy-target-class이 있다.

이럴 경우 - (하이픈)은 . (마침표)가 아니라 그냥 두는 경향이 있는데 

이 또한 무조건 - (하이픈)을 _ (언더바)로 바꾼뒤 ElasticBeanstalk 의 환경변수설정에 입력해야 한다.

ex) SPRING_AOP_PROXY_TARGET_CLASS





반응형
반응형



LocalDateTime 의 형식 때문에 Gson으로 JSON으로 변경시 아래의 사진 처럼 Data가 Deserialize 하게 나온다.



처음엔 Gson 때문인지 몰랐고 jackson-datatype-jsr310 이거 때문인 줄 알아, 정말 쌩고생을 했음에도 불구하고 안되다가

혹시나 해서 LocalDateTime 형식 때문에 Json으로 변경시 이상하게 되는가 싶어 확인했다가 정말로 Gson때문인 걸 알게되었다.

이유는 아마도 LocalDateTime 형식에서 2019-01-06T00:24:38 가운데 T 때문에 Gson에서 자동으로 위의 사진처럼 date와 time으로

나누어 주는 것 같다.



1. JPA를 사용할 것이고, DB의 날짜 형식은 TimeStamp를 사용한다. 일단 Converter를 만들어야 한다. Spring boot에서의 JPA는 2.1을 사용하기 때문에 JPA 2.1은 Java8에서의 LocalDateTime을 지원하지 않기 때문에 Converter를 만든다. 


Converter를 만드는 법은 아래와 같이 어노테이션을 추가하거나


@SpringBootApplication
@EntityScan(basePackageClasses = {Application.class, Jsr310JpaConverters.class} )
public class Application {

  public static void main(String[] args) {
    SpringApplication.run(Application.class, args);
  }
}


아래와 같이 직접 만든다.


import java.sql.Timestamp;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

import javax.persistence.AttributeConverter;
import javax.persistence.Converter;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateSerializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer;

@Converter(autoApply = true)
public class DateConverter implements AttributeConverter<LocalDateTime, Timestamp> {
	@Override
	public Timestamp convertToDatabaseColumn(LocalDateTime locDateTime) {
		return (locDateTime == null ? null : Timestamp.valueOf(locDateTime));
	}

	@Override
	public LocalDateTime convertToEntityAttribute(Timestamp sqlTimestamp) {
		
		return (sqlTimestamp == null ? null : sqlTimestamp.toLocalDateTime());
	}
}


2. 만들었으면 데이터베이스를 통해 Date를 가져오는 것은 문제 없이 작동된다.

하지만 값을 찍어보면 Deserialize 하게 나온다. Gson을 사용하지 않고 순수하게 값만 가져올 경우는 이제

pom.xml에 아래의 maven을 추가한 뒤


       

			com.fasterxml.jackson.datatype
			jackson-datatype-jsr310
 

application.properties에 아래처럼 추가하면 Deserialize 하게 나오던 값이 serialize 나온다.

spring.jackson.serialization.write-dates-as-timestamps=false

하지만 우리가 할 것은 Gson을 통해 변경하는 것이기 때문에 위와 같이 하여도 아무런 변화를 느끼지 못할 것이다.


3. 이제 Gson을 통해 변경하기 위해 클래스를 생성한다.


import java.lang.reflect.Type;
import java.time.LocalDateTime;

import com.google.gson.JsonElement;
import com.google.gson.JsonPrimitive;
import com.google.gson.JsonSerializationContext;
import com.google.gson.JsonSerializer;

public class GsonConfig implements JsonSerializer<localdatetime>{

	@Override
	public JsonElement serialize(LocalDateTime date, Type type, JsonSerializationContext json) {
		// TODO Auto-generated method stub
		return new JsonPrimitive(date.toString());
	}

}


그리고 이제 Gson을 사용시 아래처럼 사용한다.


Gson gson = new GsonBuilder().registerTypeAdapter(LocalDateTime.class, new GsonConfig()).create();
String jsonString = gson.toJson("변경할 값");

4. 이렇게 하면 2019-01-06T00:24:38 가운데 T 때문에 맨위의 사진처럼  Deserialize 나온 값이

 아래의 사진처럼  Serialize하게 나온다.


반응형
반응형


Spring boot 에서  Invisible reCAPTCHA 설정하는 방법이다.



reCAPTHA 는 쉽게 말해 봇이 접근을 시도하는 것으로부터 보호하여 스팸글 등록 및 자동 글 작성 자동 회원가입을 막는 것이다.


현재는 v3까지 나왔으며 여러버전이 있는 데, Invisible reCAPTCHA는 말그대로 보이지 않는다. 버튼 체크나, 그림 맞추기 등을 하지 않아도 됨.

원리는 구글측에서 공개하지는 않았지만 구글측에서 사용자의 패턴을 분석해서 방지한다는 정도로 알고있다.



1.  https://www.google.com/recaptcha/intro/v3.html 사이트에 접속한다.


우측 상단의 My reCAPTCHA에 접속한다.



2.  접속한 뒤 새로 만든다.


Label 에는 제목을 입력한다. 이 제목은 말그대로 그냥 제목이다 파일 이름 짓듯이, 프로젝트 생성하듯이 지으면 된다.

그리고 아래와 같이 기입한 후 생성한다. Send alerts to owner는 체크해도 되고 안해도된다. 그리고 만들고나서도 수정 가능하니 신경을 크게 안써도 된다.



3.  생성된 것을 확인

아래와 같이 Site key와 Secret key를 받게 된다.

Step1에는 사용방법이 간단하게 나와 있으며 

Step2에는 Invisible reCAPTCHA API에 사용에 필요한 요청과 응답이 나와 있다.


4.   https://developers.google.com/recaptcha/docs/invisible 옆의 주소에 들어간다.

구글에서 아래와 같이 기본적인 예제를 올려 놓았다. 

이것을 복사를 한다.



5. 복사를 했으면 Spring boot의 static 폴더 아래에 아래와 같이 만든다

굳이 여기가 아니더라도 프론트엔드를 따로 사용하시는 분들이라면 그 쪽에 만들어서 사용하면 된다.


data-siteKey는 3번에서 받은 Site-key를 입력한다. 그리고 action에는 reCAPTCHA에 사용할 API주소를 입력한다. 


6. action에 사용할  reCAPTCHA를 사용할 Controller를 만든다.

하지만 그 전에 reCAPTCHAP에 응답받을 DTO를 생성한다. 이 작업은 굳이 안해도 되고 String으로 바로 받아도 되지만

깔끔함을 위해 작성했다.

구글 API에 아래와 같이 나와있다. 

위를 바탕으로 DTO를 작성한다.

public class reCAPTCHA_DTO {
	private boolean success;
	private Integer number;
	private String action;
	private String challenge_ts;
	private String hostname;
	private String error_codes;
	
	
	public reCAPTCHA_DTO() {
		super();
	}


	public reCAPTCHA_DTO(boolean success, Integer number, String action, String challenge_ts, String hostname,
			String error_codes) {
		super();
		this.success = success;
		this.number = number;
		this.action = action;
		this.challenge_ts = challenge_ts;
		this.hostname = hostname;
		this.error_codes = error_codes;
	}


	public boolean isSuccess() {
		return success;
	}


	public void setSuccess(boolean success) {
		this.success = success;
	}


	public Integer getNumber() {
		return number;
	}


	public void setNumber(Integer number) {
		this.number = number;
	}


	public String getAction() {
		return action;
	}


	public void setAction(String action) {
		this.action = action;
	}


	public String getChallenge_ts() {
		return challenge_ts;
	}


	public void setChallenge_ts(String challenge_ts) {
		this.challenge_ts = challenge_ts;
	}


	public String getHostname() {
		return hostname;
	}


	public void setHostname(String hostname) {
		this.hostname = hostname;
	}


	public String getError_codes() {
		return error_codes;
	}


	public void setError_codes(String error_codes) {
		this.error_codes = error_codes;
	}
}

7. 이제 controller를 작성한다.


params 의 seceret에서는 3번의 seceret_key를 적는다.

3번에의 요청에서 봤듯이 ip가 필요하기 때문에 ip를 받아온다. (ip받아오는 방법은 서버마다 다르기 때문에 굳이 이렇게 안해도 됨.)

g-recaptha-response역시 3번에 나와 있듯이 구글측에서 주는 것이기 때문에 RequsetParam으로 받아 그대로 사용하면 된다.

그리고 아래와 같이 작성해서 post를 날리면 된다.


@PostMapping() public reCAPTCHA_DTO reCAPTCHA_TEST(@RequestParam(name="g-recaptcha-response") String recaptchaResponse , HttpServletRequest request) { String ip = request.getRemoteAddr(); String url = "https://www.google.com/recaptcha/api/siteverify"; String params="?secret=6LcwQYIUAAA******************&response="+recaptchaResponse; RestTemplate restTemplate = new RestTemplate(); reCAPTCHA_DTO re = restTemplate.exchange(url+params, HttpMethod.POST, null, reCAPTCHA_DTO.class).getBody(); if(re.isSuccess()) { System.out.println("성공"); } else { System.out.println("실패"); } return re; }

8. 결과


아래 사진과 같이 맨 오른쪽 하단에 reCAPTCHA 표시가 나타나며 submit을 누르면 결과가나온다.

결과



Invisible reCAPTCHA말고도 다른 reCAPTCHA사용하는 방법 역시 위와 크게 다르지 않기 때문에 아래의 레퍼런스에서 확인하여 때에 맞게 사용하면 된다.



참고 -  https://developers.google.com/recaptcha/intro

반응형
반응형


마지막으로 hashmap 을 json으로 만드는 방법과, JsonObject를 생성하는 방법이다.



Spring boot Json 1편, 2편, 3편, 4편

1. 2018/11/06 - [Spring Boot] - Spring Boot Json, JsonObject로 만들기 - JSON 1편

2. 2018/11/07 - [Develop/Spring Boot] - Spring Boot Json, Gson을 이용한 JsonObject to String, String to JsonObject- JSON 2편

3. 2018/11/09 - [Develop/Spring Boot] - Spring Boot Json, Jackson을 이용한 JsonParsing - Json 3편

4. 2018/11/12 - [Develop/Spring Boot] - Spring Boot Deserialization Json, Deserialization JsonString to JsonObject - Json 4편



번외 Database의 값을 Json으로 select하는 방법.

1. 2018/10/24 - [Spring Boot] - Spring boot jpa map, hashmap, JSON형식

2. 2018/10/28 - [Spring Boot] - Spring boot JPA EntityManager를 이용한 Map형식으로 mapping하기


Spring boot에서 MySQL JSON 타입 SELECT하는 방법

1. 2018/11/30 - [Develop/Spring Boot] - Spring boot MySQL JSON - MySQL JSON DATA TYPE 값 가져오기



HashMap을 Json으로 만드는 방법을 배우기에 앞서 JsonObject를 생성하는 방법을 먼저 본다.


1. google-json-simple을 이용한 JsonObject 만드는 방법.


xml에 아래와 같은 dependency를 추가한다.


		com.googlecode.json-simple
		json-simple
		1.1


우리가 만들어볼 Json예제는 4편에서 사용한 Json이다.


{
    "id": 1,
    "password": "1234",
    "details": [
        {
            "name": "test",
            "age": 20
        },{
            "name": "test2",
            "age": 21
        }
    ] 
}

그리고 아래와 같이 JSONObject라는 것과 JSONArray라는 것을 사용하여 JSON을 생성할 수 있다.


public void test() {
		JSONObject obj = new JSONObject();
		JSONArray arr = new JSONArray();
		
		obj.put("id", 1);
		obj.put("password", "1234");
		
		JSONObject obj2 =new JSONObject();
		obj2.put("name", "test");
		obj2.put("age", 20);
		
		arr.add(obj2);
		
		obj2 = new JSONObject();
		obj2.put("name", "test2");
		obj2.put("age", 21);
		
		arr.add(obj2);
		
		obj.put("details", arr);
		
		System.out.println(obj.toString());
		
}




현재는 Json이 Array를 포함하여 JSONArray를 사용하였지만 일반 적인 key : value 형태는 JSONObject 하나만으로도 생성할 수 있다.


2. Gson을 이용한JsonObject 생성


이 방법은 Json  2편에서 배웠으나 이번엔 Array를 사용하기 때문에 다시 한번 해본다. 사실 위의 json-simple과 별반 다를게 없다.



	public void test() {
		Gson gson = new Gson();
		JsonObject obj = new JsonObject();
		JsonArray arr = new JsonArray();
		
		obj.addProperty("id", 1);
		obj.addProperty("password", "1234");
		
		JsonObject obj2 = new JsonObject();
		obj2.addProperty("name", "test");
		obj2.addProperty("age", 20);
		arr.add(obj2);
		
		obj2= new JsonObject();
		obj2.addProperty("name", "test2");
		obj2.addProperty("age", 21);
		
		arr.add(obj2);
		
		obj.add("details", arr);
		
		String jsonString = obj.toString();
		
		System.out.println("gson을 이용한 객체생성");
		System.out.println(jsonString);
		
	}




3. HashMap을 Json으로 만드는 방법.


HashMap<String, Object>를 선언해서 이번 편에서 했던 모든 것들(json-simple의 obj와 gson의 obj)을 HashMap에 put해보고

일반 데이터를 HashMap에 put해서 json으로 변환해 보는 법을 해본다. 그리고 gson의 prettyprinting을 이용하여 예쁘게 출력해본다.



public void test() {
		JSONObject obj = new JSONObject();
		JSONArray arr = new JSONArray();

		obj.put("id", 1);
		obj.put("password", "1234");

		JSONObject obj2 = new JSONObject();
		obj2.put("name", "test");
		obj2.put("age", 20);

		arr.add(obj2);

		obj2 = new JSONObject();
		obj2.put("name", "test2");
		obj2.put("age", 21);

		arr.add(obj2);

		obj.put("details", arr);

		
		JsonObject g_obj = new JsonObject();
		JsonArray g_arr = new JsonArray();

		g_obj.addProperty("id", 1);
		g_obj.addProperty("password", "1234");

		JsonObject g_obj2 = new JsonObject();
		g_obj2.addProperty("name", "test");
		g_obj2.addProperty("age", 20);
		g_arr.add(g_obj2);

		g_obj2 = new JsonObject();
		g_obj2.addProperty("name", "test2");
		g_obj2.addProperty("age", 21);

		g_arr.add(g_obj2);

		g_obj.add("details", g_arr);
		
		HashMap map = new HashMap();
		map.put("json-simple", obj);
		map.put("gson", g_obj);
		map.put("HashMap", "맵테스트");
		map.put("숫자테스트", 1234567890);
		
		Gson gson = new GsonBuilder().setPrettyPrinting().create();
		String jsonString = gson.toJson(map, new TypeToken>() {
		}.getType());
		System.out.println("HashMap to JSON");
		System.out.println(jsonString);

	}



이로써 Spring boot 에서의 Json에 대한 학습을 마무리한다.

1편부터 마지막편까지의 내용과 번외편 내용 2개만 알아도 Spring boot에서 모든 데이터들을 Json으로 만들 수 있다.


반응형
반응형

어찌보면 가장 중요한 Deserialization한 JsonString 형태를 파싱하는 방법이다.

Spring Boot에서 Deserialization 파싱을 위해 Jackson과 gson을 사용하면서 동시에 클래스를 생성하여 사용해야한다.

 

 

Spring boot Json 1편, 2편, 3편, 마지막

1. 2018/11/06 - [Spring Boot] - Spring Boot Json, JsonObject로 만들기 - JSON 1편

2. 2018/11/07 - [Develop/Spring Boot] - Spring Boot Json, Gson을 이용한 JsonObject to String, String to JsonObject- JSON 2편

3. 2018/11/09 - [Develop/Spring Boot] - Spring Boot Json, Jackson을 이용한 JsonParsing - Json 3편

4. 2018/11/13 - [Develop/Spring Boot] - Spring Boot Json, hashmap to json , JsonObject 만들기- JSON 마지막

 

 

번외 Database의 값을 Json으로 select하는 방법.

1. 2018/10/24 - [Spring Boot] - Spring boot jpa map, hashmap, JSON형식

2. 2018/10/28 - [Spring Boot] - Spring boot JPA EntityManager를 이용한 Map형식으로 mapping하기

 

Spring boot에서 MySQL JSON 타입 SELECT하는 방법

1. 2018/11/30 - [Develop/Spring Boot] - Spring boot MySQL JSON - MySQL JSON DATA TYPE 값 가져오기

 

 

1. Class를 생성하여 Gson을 이용한 Deserializaion한 JsonString to Object(JsonObject)

 

앞의 1편, 2편, 3편에 사용했던 TestDTO를 수정하자.

 

수정하기 전 먼저 Test_2DTO를 새로 생성한다

public class Test_2DTO { 	private String name; 	private int age; 	 	public Test_2DTO() { 		super(); 	}  	public Test_2DTO(String name, int age) { 		super(); 		this.name = name; 		this.age = age; 	}  	public String getName() { 		return name; 	}  	public void setName(String name) { 		this.name = name; 	}  	public int getAge() { 		return age; 	}  	public void setAge(int age) { 		this.age = age; 	} } 
public class TestDTO {  	private Integer id; 	private String password; 	private List details; 	 	 	public TestDTO() { 		super(); 	}   	public TestDTO(Integer id, String password, List details) { 		super(); 		this.id = id; 		this.password = password; 		this.details = details; 	}   	public Integer getId() { 		return id; 	}   	public void setId(Integer id) { 		this.id = id; 	}   	public String getPassword() { 		return password; 	}   	public void setPassword(String password) { 		this.password = password; 	}   	public List getDetails() { 		return details; 	}   	public void setDetails(List details) { 		this.details = details; 	}   	@Override 	public String toString() { 		return new Gson().toJson(this); 	} } 

 

생성했다면, 

 

public void test() { 		 		String jsonString = "{\r\n" +  				"    \"id\": 1,\r\n" +  				"    \"password\": \"1234\",\r\n" +  				"    \"details\": [\r\n" +  				"        {\r\n" +  				"            \"name\": \"test\",\r\n" +  				"            \"age\": 20\r\n" +  				"        },{\r\n" +  				"            \"name\": \"test2\",\r\n" +  				"            \"age\": 21\r\n" +  				"        }\r\n" +  				"    ] \r\n" +  				"}"; 		Gson gson = new Gson(); 		TestDTO t = gson.fromJson(jsonString, TestDTO.class); 		System.out.println("id : " +t.getId()); 		System.out.println("password : "+t.getPassword()); 		System.out.println("name1 : "+t.getDetails().get(0).getName()); 		System.out.println("age1: "+t.getDetails().get(0).getAge()); 		System.out.println("name2 : "+t.getDetails().get(1).getName()); 		System.out.println("age2: "+t.getDetails().get(1).getAge()); 		 	} 

위에 사용한 Json내용이다., 

 

{     "id": 1,     "password": "1234",     "details": [         {             "name": "test",             "age": 20         },{             "name": "test2",             "age": 21         }     ]  }

 

위 test() 메소드를 실행하면 아래와 같이 Deserializaion한 JsonString이 파싱되어 JsonObject에 알맞게 들어간다.

 

 

2. Jackson의 ObjectMapper를 이용한 Json String to JsonObject 이다.

 

Json내용은 위와 동일한 상태에서 진행하였다.

 

public void test() { 		ObjectMapper objectMapper = new ObjectMapper(); 		  		String jsonString = "{\r\n" +  				"    \"id\": 1,\r\n" +  				"    \"password\": \"1234\",\r\n" +  				"    \"details\": [\r\n" +  				"        {\r\n" +  				"            \"name\": \"test\",\r\n" +  				"            \"age\": 20\r\n" +  				"        },{\r\n" +  				"            \"name\": \"test2\",\r\n" +  				"            \"age\": 21\r\n" +  				"        }\r\n" +  				"    ] \r\n" +  				"}"; 		  try { 	            TestDTO t = objectMapper.readValue(jsonString, TestDTO.class);    // String to Object로 변환 	            System.out.println("ObjectMapper테스트"); 	    		System.out.println("id : " +t.getId()); 	    		System.out.println("password : "+t.getPassword()); 	    		System.out.println("name1 : "+t.getDetails().get(0).getName()); 	    		System.out.println("age1: "+t.getDetails().get(0).getAge()); 	    		System.out.println("name2 : "+t.getDetails().get(1).getName()); 	    		System.out.println("age2: "+t.getDetails().get(1).getAge());	      	 	        } catch (IOException e) { 	            e.printStackTrace(); 	        }		 } 

gson과 마찬가지로 잘 작동된다.

반응형

3. Jackson JsonNode 를 이용한 Deserializaion String to JsonObject

 

어떻게 보면 가장 핵심인 부분일 수도 있다. 클래스 생성없이 할 수 있다는 큰 장점이있다.

 

Deserializaion Json Data는 위와 동일한 데이터를 사용한다.

 

 

public void test() { 		ObjectMapper objectMapper = new ObjectMapper();  		String jsonString = "{\r\n" + "    \"id\": 1,\r\n" + "    \"password\": \"1234\",\r\n" 				+ "    \"details\": [\r\n" + "        {\r\n" + "            \"name\": \"test\",\r\n" 				+ "            \"age\": 20\r\n" + "        },{\r\n" + "            \"name\": \"test2\",\r\n" 				+ "            \"age\": 21\r\n" + "        }\r\n" + "    ] \r\n" + "}"; 		try { 			System.out.println("Jackson JsonNode 테스트"); 			JsonNode t = objectMapper.readValue(jsonString, JsonNode.class); // String to Object로 변환 			 			JsonNode id = t.get("id"); 			System.out.println("id : " + id.asInt()); 			 			JsonNode password = t.get("password"); 			System.out.println("password : " + password.asText()); 			 			JsonNode details = t.get("details"); 			JsonNode details_1 = details.get(0);		 			System.out.println("name1 : " + details_1.get("name").asText()); 			System.out.println("age1: " + details_1.get("age").asInt()); 			 			JsonNode details_2 = details.get(1);	 			System.out.println("name2 : " + details_2.get("name").asText()); 			System.out.println("age2: " + details_2.get("name").asText());  		} catch (IOException e) { 			e.printStackTrace(); 		} } 

class 생성없이 파싱이 잘 되었다

 

이 처럼 웬만한 것들은 class를 생성하여 모든 Deserializaion을 파싱할 수 있으며,

Jackson JsonNode 를 이용하여 처리할 수 있다. 

반응형
반응형

 

Spring Boot에서 Json 파싱을 위해 Jackson을 기본적으로 제공해준다. Spring에서는 maven 또는 gradle을 xml에 추가해야한다.

사실 기본적인 기능은 앞선 gson과 다를게 없다.

 

Spring boot Json 1편, 2편, 4편, 마지막

1. 2018/11/06 - [Spring Boot] - Spring Boot Json, JsonObject로 만들기 - JSON 1편

2. 2018/11/07 - [Develop/Spring Boot] - Spring Boot Json, Gson을 이용한 JsonObject to String, String to JsonObject- JSON 2편

3. 2018/11/12 - [Develop/Spring Boot] - Spring Boot Deserialization Json, Deserialization JsonString to JsonObject - Json 4편

4. 2018/11/13 - [Develop/Spring Boot] - Spring Boot Json, hashmap to json , JsonObject 만들기- JSON 마지막

 

 

 

번외 Database의 값을 Json으로 select하는 방법.

1. 2018/10/24 - [Spring Boot] - Spring boot jpa map, hashmap, JSON형식

2. 2018/10/28 - [Spring Boot] - Spring boot JPA EntityManager를 이용한 Map형식으로 mapping하기

 

Spring boot에서 MySQL JSON 타입 SELECT하는 방법

1. 2018/11/30 - [Develop/Spring Boot] - Spring boot MySQL JSON - MySQL JSON DATA TYPE 값 가져오기

 

 

1. Jackson ObjectMapper (JsonString to Object)

 

public void test() { 		ObjectMapper objectMapper = new ObjectMapper();  		String Json = 		    "{ \"id\" : 1, \"password\" : \"1234\" }";  		try { 		    TestDTO t = objectMapper.readValue(Json, TestDTO.class);	// String to Object로 변환  		    System.out.println("id = " + t.getId()); 		    System.out.println("password = " + t.getPassword()); 		     		    String jsonString = objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(t); //다시 jsonString으로 변환 		    System.out.println(jsonString); 		} catch (IOException e) { 		    e.printStackTrace(); 		} }

 

(위의 TestDTO은 1편에 나와있다.)

 

objectMapper를 통해 JsonString을 Object로 변환할 수 있었고 writeValueAsString을 통해 다시 JsonString으로도 변환이 가능하다

반응형

2. Object From JsonFile

 

 

ObjectMapper objectMapper = new ObjectMapper();  URL url = new URL("file:data/test.json");  TestDTO t = objectMapper.readValue(url, TestDTO.class); 

 

test.json 파일엔

{

"id" : 1,

"password" : 1234

}

들어 가있다.

 

 

기본적인 사용방법은 여기까지이다 다음편에 Jackson JsonNodeClass를 설명한다.

Jackson 가이드 - https://www.baeldung.com/jackson

반응형
반응형

 

1편에서 Spring Boot에서 데이터를 JSON형식으로 표현하는 가장 기본적인 방법인 클래스를 이용한 방법을 배웠다.

이번에는 Gson을 이용한 JsonOjbect to String, String to JsonObject를 알아아보자.

 

개인적인 경험으론 Gson이 가장 Json파싱에서 핵심이 아닐까 생각한다.

 

 

Spring boot Json 1편, 3편, 4편, 마지막

1. 2018/11/06 - [Spring Boot] - Spring Boot Json, JsonObject로 만들기 - JSON 1편

2 .2018/11/09 - [Develop/Spring Boot] - Spring Boot Json, Jackson을 이용한 JsonParsing - Json 3편

3. 2018/11/12 - [Develop/Spring Boot] - Spring Boot Deserialization Json, Deserialization JsonString to JsonObject - Json 4편

4. 2018/11/13 - [Develop/Spring Boot] - Spring Boot Json, hashmap to json , JsonObject 만들기- JSON 마지막

 

 

번외 Database의 값을 Json으로 select하는 방법.

1. 2018/10/24 - [Spring Boot] - Spring boot jpa map, hashmap, JSON형식

2. 2018/10/28 - [Spring Boot] - Spring boot JPA EntityManager를 이용한 Map형식으로 mapping하기

 

Spring boot에서 MySQL JSON 타입 SELECT하는 방법

1. 2018/11/30 - [Develop/Spring Boot] - Spring boot MySQL JSON - MySQL JSON DATA TYPE 값 가져오기

 

 

 

1. 먼저 pom.xml에 의존성을 추가한다.

 

 		com.google.code.gson 		gson 		2.8.5  

 

2. Gson을 이용한 Json 생성

 

public String test3() { 		 		Gson gson = new Gson(); 		JsonObject obj = new JsonObject(); 		String id=null; 		obj.addProperty("id", id); 		obj.addProperty("pass", 1234); 		 		String i = obj.toString(); 		String j = gson.toJson(obj); 		System.out.println(i); 		System.out.println(j); 		return i; } 

 

Gson의 JsonObject를 Import받고 JsonObject를 이용하여 Json 형식으로 만들었다.

위의 toString()으로 호출한 결과와 gson.toJson()을 이용한 결과가 다르다는 걸 볼 수 있다.

아래의 gson.toJson()을 이용하면 null값이 들어올 경우 키값이 사라져 버린다.

 

3. String to JsonObject

 

public void test3() { 		 		Gson gson = new Gson(); 		String json = "{\"id\": 1, \"password\": \"1234\"}";  		TestDTO test = gson.fromJson(json, TestDTO.class); 		System.out.println(test.getId()); 		System.out.println(test.getPassword()); 		 } 

 

1편에서 사용한 TestDTO를 통해 Json객체를 생성할 수 있다.

반응형

4.  객체를 Json으로 만들기

 

	public void test3() { 		 		Gson gson = new Gson(); 		TestDTO test2 = new TestDTO(); 		test2.setId(2); 		test2.setPassword("2222"); 		String i=gson.toJson(test2); 		System.out.println(i); 	} 

 

5.  Gson의 JsonParser와 JsonElement를 통한 JsonParsing

 

	public void test3() { 		 		String json = "{\"id\": 1, \"password\": \"1234\"}"; 		JsonParser jp = new JsonParser(); 		JsonElement je = jp.parse(json); 		int id = je.getAsJsonObject().get("id").getAsInt(); 		String pass = je.getAsJsonObject().get("password").getAsString(); 		System.out.println(id + " / " + pass); 	} 

 

6. Gson을 이용하여 Class의 toString() 을 이용한 Json 생성

 

1편의 TestDTO클래스에 Gson을 이용한 toString() 으로 보다 빠르고 편리하게 Json을 생성할 수 있다.

 

import com.google.gson.Gson;  public class TestDTO {  	private Integer id; 	private String password; 	 	 	public TestDTO() { 		super(); 	} 	public TestDTO(Integer id, String password) { 		super(); 		this.id = id; 		this.password = password; 	} 	public Integer getId() { 		return id; 	} 	public void setId(Integer id) { 		this.id = id; 	} 	public String getPassword() { 		return password; 	} 	public void setPassword(String password) { 		this.password = password; 	} 	 	@Override 	public String toString() { 		return new Gson().toJson(this); 	} }  	public void test3() {  		Gson gson = new Gson(); 		TestDTO test2 = new TestDTO(); 		test2.setId(2); 		test2.setPassword("2222"); 		String json = test2.toString(); 		System.out.println(json); 	} 

 

7. 기타

 

이외에도 Json 예쁘게 출력하는 방법 등 여러가지 기능이 존재한다.

 

 

Gson가이드 - https://github.com/google/gson/blob/master/UserGuide.md

 

반응형
반응형

 

Spring Boot에서 데이터를 JSON형식으로 표현하는 가장 기본적인 방법으로는 클래스를 이용하는 방법이다.

이 방법을 알아야 다음에 Deserialization한 JSON을 만들 수 있다.

 

Spring boot Json 2편, 3편, 4편, 마지막

1. 2018/11/07 - [Develop/Spring Boot] - Spring Boot Json, Gson을 이용한 JsonObject to String, String to JsonObject- JSON 2편

2. 2018/11/09 - [Develop/Spring Boot] - Spring Boot Json, Jackson을 이용한 JsonParsing - Json 3편

3. 2018/11/12 - [Develop/Spring Boot] - Spring Boot Deserialization Json, Deserialization JsonString to JsonObject - Json 4편

4. 2018/11/13 - [Develop/Spring Boot] - Spring Boot Json, hashmap to json , JsonObject 만들기- JSON 마지막

 

번외 Database의 값을 Json으로 select하는 방법.

1. 2018/10/24 - [Spring Boot] - Spring boot jpa map, hashmap, JSON형식

2. 2018/10/28 - [Spring Boot] - Spring boot JPA EntityManager를 이용한 Map형식으로 mapping하기

 

Spring boot에서 MySQL JSON 타입 SELECT하는 방법

1. 2018/11/30 - [Develop/Spring Boot] - Spring boot MySQL JSON - MySQL JSON DATA TYPE 값 가져오기

 

 

 

먼저 DTO를 생성한다.

public class TestDTO {  	private Integer id; 	private String password; 	 	 	public TestDTO() { 		super(); 	} 	public TestDTO(Integer id, String password) { 		super(); 		this.id = id; 		this.password = password; 	} 	public Integer getId() { 		return id; 	} 	public void setId(Integer id) { 		this.id = id; 	} 	public String getPassword() { 		return password; 	} 	public void setPassword(String password) { 		this.password = password; 	} 	 } 

다음 컨트롤러를 하나 생성해준다.

@RestController @RequestMapping(value = "/jsontest") public class TestClass { 	 	@GetMapping() 	public TestDTO test() { 		TestDTO test = new TestDTO(1, "1111"); 		return test; 	} 	 	@GetMapping("/2") 	public List test2() { 		List test = new ArrayList(); 		test.add(0, new TestDTO(1, "1111")); 		test.add(1, new TestDTO(2, "2222")); 		test.add(2, new TestDTO(3, "3333")); 		return test; 	} }
반응형

그리고 실행하게 되면

 

1. test() 실행화면

 

2. test2() 실행화면

 

이렇게 Json 형태로 출력된다

참고로 Map은 Json형태로 출력되지 않고 {"id" = 1, "password"="1111"} 형식으로 출력된다. List만 Json형식으로 출력된다.

또한, 이렇게 클래스로 만든 JsonObject는 Return할 때만 Json으로 출력되지, log또는 print할 경우엔 Json형식으로 출력되지 않는다.

Json형식으로 출력되게 하려면 다음에 할 2편을 참고.

 

기초는 마무리.

 

반응형

+ Recent posts