Spring Json 라이브러리
MVN 라이브러리 사용(JSON)
- MVN 레포지터리에서 Jackson Databind 2.13.3에 소스 복사해서 pom.xml에 테스트 위쪽에 붙여넣기
- 자동으로 다운 받아진다
<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.13.3</version>
</dependency>
Json 데이터 포맷
- {키 : 값} / {“키” : 값} 키는 문자열로 표현 값은 데이터타입이 존재한다
- JSON.parse(‘JSON문자열’) -> javaScript Object구문으로 변환
- const obj = JSON.parse(‘ { “name” : “John”, “age” : 30, “city” : “New York” } ‘ );
- postman에 주소를 넣어서 변환되는거 확인
package com.demo.controller;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.demo.domain.ProductVO;
@Controller
public class SampleController5 {
private static final Logger Logger = LoggerFactory.getLogger(SampleController5.class);
@RequestMapping("/doJSON")
public @ResponseBody ProductVO doJSON() {
ProductVO vo = new ProductVO("상품", 30000);
// 기본은 jsp가 실행되어 그 결과가 클라이언트의 브라우저에 전송된다
// 리턴값이 객체. 객체가 참조하는 값을 클라이언트의 브라우저에게 jsp가 진행되지 않고, json포맷으로 보내고자 할때
// vo->JSON 데이터포맷으로 변환작업 : jackson-databind 라이브러리가 백그라운드 작업
return vo;
}
}
- 라이브러리 pom.xml 추가
<!-- https://mvnrepository.com/artifact/com.oracle.database.jdbc/ojdbc8 -->
<!-- 오라클 데이터베이스 드라이버 -->
<dependency>
<groupId>com.oracle.database.jdbc</groupId>
<artifactId>ojdbc8</artifactId>
<version>21.5.0.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.zaxxer/HikariCP -->
<!-- Connection Pool기능지원 -->
<dependency>
<groupId>com.zaxxer</groupId>
<artifactId>HikariCP</artifactId>
<version>2.7.4</version>
</dependency>
<!-- mybatis 사용하기위한 아래 5가지 라이브러리(With spring-test). -->
<!-- 1)mybatis 2)mybatis-spring 3)spring-jdbc 4)spring-tx 5)spring-test -->
<!-- 1)https://mvnrepository.com/artifact/org.mybatis/mybatis -->
<!-- xml문서에 sql구문을 관리하는 형태의 특징 -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.6</version>
</dependency>
<!-- 2)https://mvnrepository.com/artifact/org.mybatis/mybatis-spring -->
<!-- 스프링과 마이바티를 중계(연결) -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.3.2</version>
</dependency>
<!-- 3)https://mvnrepository.com/artifact/org.springframework/spring-jdbc -->
<!-- 자바의 jdbc를 스프링버전으로 만든 의미 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${org.springframework-version}</version>
</dependency>
<!-- 4)https://mvnrepository.com/artifact/org.springframework/spring-tx -->
<!-- 트랜잭션 기능제공 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>${org.springframework-version}</version>
</dependency>
<!-- 5)JUnit와 함께 테스트를 위한 관련 라이브러리 -->
<!-- 테스트 기능제공 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${org.springframework-version}</version>
</dependency>
- pom.xml에서 Test부분 4.7에서 4.12로 변경
- 메서드를 선택해서 JUnit 실행
// src/test/java 파일에 JUnit Test Case 생성
package com.demo.controller;
import static org.junit.Assert.fail;
import java.sql.Connection;
import java.sql.DriverManager;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class JDBCTests {
private static final Logger Logger = LoggerFactory.getLogger(JDBCTests.class);
private String url = "jdbc:oracle:thin:@localhost:1521:xe";
private String uid = "ora_user";
private String passwd = "1234";
static {
try {
Class.forName("oracle.jdbc.OracleDriver");
} catch(Exception ex) {
ex.printStackTrace();
}
}
@Test
public void testConnection() {
try(Connection conn = DriverManager.getConnection(url, uid, passwd)) {
Logger.info("연결성공");
} catch(Exception ex) {
fail(ex.getMessage());
}
}
}
- root-context.xml 에 beans 지우고 삽입
<?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:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:mybatis-spring="http://mybatis.org/schema/mybatis-spring"
xsi:schemaLocation="http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.1.xsd
http://mybatis.org/schema/mybatis-spring http://mybatis.org/schema/mybatis-spring-1.2.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd">
<!-- Root Context: defines shared resources visible to all other web components -->
<bean id="hikariConfig" class="com.zaxxer.hikari.HikariConfig">
<property name="driverClassName" value="oracle.jdbc.OracleDriver" />
<property name="jdbcUrl" value="jdbc:oracle:thin:@localhost:1521:xe" />
<property name="username" value="ora_user" />
<property name="password" value="1234" />
</bean>
<bean id="dataSource" class="com.zaxxer.hikari.HikariDataSource" destroy-method="close">
<constructor-arg ref="hikariConfig" />
</bean>
<!-- 데이터베이스 연결과 SQL 구문 실행에 대한 모든기능을 가진 객체 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
</bean>
</beans>
- root-context.xml 파일의 datasource bean설정 테스트
- 메서드를 선택해서 JUnit 실행
package com.demo.controller;
import static org.junit.Assert.*;
import java.sql.Connection;
import javax.sql.DataSource;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
// root-context.xml 파일의 datasource bean설정 테스트
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"file:src/main/webapp/WEB-INF/spring/**/root-context.xml"})
public class DataSourceTests {
private static final Logger Logger = LoggerFactory.getLogger(DataSourceTests.class);
@Autowired // 메모리상의 "dataSource" bean이 대입(주입)
private DataSource ds;
@Test
public void testConnection() throws Exception{
try (Connection conn = ds.getConnection()) {
Logger.info("연결성공 : " + conn);
} catch (Exception ex) {
ex.printStackTrace();
}
}
@Test
public void test() {
fail("Not yet implemented");
}
}
댓글남기기