From f6a8a4ce804bdb791295abf20a9f1d3d4a9dc2df Mon Sep 17 00:00:00 2001 From: anyjava Date: Tue, 16 Feb 2016 23:41:46 +0900 Subject: [PATCH 1/4] =?UTF-8?q?=EC=BA=90=EB=A6=AD=ED=84=B0=EC=85=8B=20UTF-?= =?UTF-8?q?8=EC=9C=BC=EB=A1=9C=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../net/anyjava/SpringDemoApplication.java | 28 +++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/src/main/java/net/anyjava/SpringDemoApplication.java b/src/main/java/net/anyjava/SpringDemoApplication.java index 799d571..8549ebb 100644 --- a/src/main/java/net/anyjava/SpringDemoApplication.java +++ b/src/main/java/net/anyjava/SpringDemoApplication.java @@ -1,14 +1,42 @@ package net.anyjava; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.boot.autoconfigure.web.HttpEncodingProperties; +import org.springframework.boot.context.web.OrderedCharacterEncodingFilter; +import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; +import org.springframework.core.Ordered; +import org.springframework.http.converter.HttpMessageConverter; +import org.springframework.http.converter.StringHttpMessageConverter; + +import java.nio.charset.Charset; @SpringBootApplication @ComponentScan public class SpringDemoApplication { + @Autowired + private HttpEncodingProperties httpEncodingProperties; + public static void main(String[] args) { SpringApplication.run(SpringDemoApplication.class, args); } + + + @Bean + public HttpMessageConverter responseBodyConverter() { + return new StringHttpMessageConverter(Charset.forName("UTF-8")); + } + + @Bean + public OrderedCharacterEncodingFilter characterEncodingFilter() { + OrderedCharacterEncodingFilter filter = new OrderedCharacterEncodingFilter(); + filter.setEncoding(this.httpEncodingProperties.getCharset().name()); + filter.setForceEncoding(this.httpEncodingProperties.isForce()); + filter.setOrder(Ordered.HIGHEST_PRECEDENCE); + return filter; + } + } From b55df9941f082c37a06e8cfeec48b0a22a1a104c Mon Sep 17 00:00:00 2001 From: anyjava Date: Tue, 16 Feb 2016 23:48:57 +0900 Subject: [PATCH 2/4] =?UTF-8?q?CustomerController=20=EC=A1=B0=ED=9A=8CAPI?= =?UTF-8?q?=20=EC=99=84=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- build.gradle | 1 + .../demo/controller/CustomerController.java | 13 ++++++---- .../controller/CustomerControllerTest.java | 25 ++++++++++++++++--- 3 files changed, 31 insertions(+), 8 deletions(-) diff --git a/build.gradle b/build.gradle index c967842..7323819 100644 --- a/build.gradle +++ b/build.gradle @@ -34,6 +34,7 @@ dependencies { compile('org.springframework.boot:spring-boot-starter-data-jpa') compile('org.springframework.boot:spring-boot-starter-freemarker') compile('org.projectlombok:lombok:1.16.6') + compile('com.google.code.gson:gson:2.5') //compile('org.springframework.boot:spring-boot-starter-security') //compile('org.bgee.log4jdbc-log4j2:log4jdbc-log4j2:1.11') runtime('com.h2database:h2') diff --git a/src/main/java/net/anyjava/demo/controller/CustomerController.java b/src/main/java/net/anyjava/demo/controller/CustomerController.java index fee6153..77be719 100644 --- a/src/main/java/net/anyjava/demo/controller/CustomerController.java +++ b/src/main/java/net/anyjava/demo/controller/CustomerController.java @@ -1,10 +1,9 @@ package net.anyjava.demo.controller; import net.anyjava.demo.domain.Customer; -import org.springframework.web.bind.annotation.PathVariable; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RequestMethod; -import org.springframework.web.bind.annotation.RestController; +import net.anyjava.demo.service.CustomerService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.*; /** * Created by anyjava on 2016. 2. 11.. @@ -13,8 +12,12 @@ @RequestMapping("/api/Customers") public class CustomerController { + @Autowired + private CustomerService customerService; + @RequestMapping(value = "{id}", method = RequestMethod.GET) + @ResponseBody public Customer getCustomer(@PathVariable Long id) { - return null; + return customerService.select(id); } } diff --git a/src/test/java/net/anyjava/demo/controller/CustomerControllerTest.java b/src/test/java/net/anyjava/demo/controller/CustomerControllerTest.java index a265655..36b1d7f 100644 --- a/src/test/java/net/anyjava/demo/controller/CustomerControllerTest.java +++ b/src/test/java/net/anyjava/demo/controller/CustomerControllerTest.java @@ -1,11 +1,15 @@ package net.anyjava.demo.controller; +import com.google.gson.Gson; import net.anyjava.SpringDemoApplication; +import net.anyjava.demo.domain.Customer; +import net.anyjava.demo.service.CustomerService; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.SpringApplicationConfiguration; +import org.springframework.http.MediaType; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.test.context.web.WebAppConfiguration; import org.springframework.test.web.servlet.MockMvc; @@ -13,6 +17,7 @@ import org.springframework.web.context.WebApplicationContext; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; @RunWith(SpringJUnit4ClassRunner.class) @@ -25,11 +30,17 @@ public class CustomerControllerTest { @Autowired private WebApplicationContext webApplicationContext; + @Autowired + private CustomerService customerService; + + /** * MockMvc */ @Before public void setup() { + + mockMvc = MockMvcBuilders .webAppContextSetup(webApplicationContext) .build(); @@ -37,9 +48,17 @@ public void setup() { @Test public void testGetCustomer() throws Exception { - mockMvc.perform(get("/api/Customers/1")) - .andExpect(status().isOk()); -// .andExpect(content().string(containsString("Hello"))); + + Customer customer = this.customerService.create(new Customer(null, "아", "이유")); + Gson gson = new Gson(); + String json = gson.toJson(customer); + + mockMvc.perform( + get("/api/Customers/1").accept(MediaType.APPLICATION_JSON)) + .andExpect(status().isOk()) + .andExpect( + content().contentType(MediaType.APPLICATION_JSON_UTF8_VALUE)) + .andExpect(content().string(json)); } From c0deb6d55df027474c30e5437597dbad15e74568 Mon Sep 17 00:00:00 2001 From: anyjava Date: Wed, 17 Feb 2016 00:30:31 +0900 Subject: [PATCH 3/4] =?UTF-8?q?404=20NOT=20FOUND=20Case=20=EC=B6=94?= =?UTF-8?q?=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../demo/controller/CustomerController.java | 21 ++++++++++++++++--- .../controller/CustomerControllerTest.java | 3 +++ 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/src/main/java/net/anyjava/demo/controller/CustomerController.java b/src/main/java/net/anyjava/demo/controller/CustomerController.java index 77be719..cba4850 100644 --- a/src/main/java/net/anyjava/demo/controller/CustomerController.java +++ b/src/main/java/net/anyjava/demo/controller/CustomerController.java @@ -3,6 +3,7 @@ import net.anyjava.demo.domain.Customer; import net.anyjava.demo.service.CustomerService; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.HttpStatus; import org.springframework.web.bind.annotation.*; /** @@ -15,9 +16,23 @@ public class CustomerController { @Autowired private CustomerService customerService; - @RequestMapping(value = "{id}", method = RequestMethod.GET) - @ResponseBody + @RequestMapping(value = "/{id}", method = RequestMethod.GET) public Customer getCustomer(@PathVariable Long id) { - return customerService.select(id); + + Customer c = customerService.select(id); + + // 조회결과가 없을경우 404 리턴 + if ( c == null ) + throw new UserNotFoundException(id); + + return c; + } + + @ResponseStatus(HttpStatus.NOT_FOUND) + class UserNotFoundException extends RuntimeException { + + public UserNotFoundException(Long userId) { + super("could not find user '" + userId + "'."); + } } } diff --git a/src/test/java/net/anyjava/demo/controller/CustomerControllerTest.java b/src/test/java/net/anyjava/demo/controller/CustomerControllerTest.java index 36b1d7f..8065733 100644 --- a/src/test/java/net/anyjava/demo/controller/CustomerControllerTest.java +++ b/src/test/java/net/anyjava/demo/controller/CustomerControllerTest.java @@ -60,6 +60,9 @@ public void testGetCustomer() throws Exception { content().contentType(MediaType.APPLICATION_JSON_UTF8_VALUE)) .andExpect(content().string(json)); + mockMvc.perform( + get("/api/Customers/0").accept(MediaType.APPLICATION_JSON)) + .andExpect(status().isNotFound()); } From f573ee67c1b0347a1a5af3b77443ad699b5a1461 Mon Sep 17 00:00:00 2001 From: anyjava Date: Sun, 28 Feb 2016 16:39:07 +0900 Subject: [PATCH 4/4] =?UTF-8?q?=EC=BB=A8=ED=8A=B8=EB=A1=A4=EB=9F=AC=20?= =?UTF-8?q?=ED=85=8C=EC=8A=A4=ED=8A=B8=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- build.gradle | 1 + gradle/wrapper/gradle-wrapper.properties | 4 ++-- .../net/anyjava/SpringDemoApplication.java | 9 ++++++++- .../demo/controller/CustomerController.java | 16 +++++++++++---- .../anyjava/demo/service/CustomerService.java | 7 +++++++ .../anyjava/demo/WelcomeControllerTest.java | 1 - .../controller/CustomerControllerTest.java | 20 ++++++++++++++----- .../demo/service/CustomerServiceTest.java | 5 +++-- 8 files changed, 48 insertions(+), 15 deletions(-) diff --git a/build.gradle b/build.gradle index 7323819..3a2bfa3 100644 --- a/build.gradle +++ b/build.gradle @@ -40,6 +40,7 @@ dependencies { runtime('com.h2database:h2') //runtime('mysql:mysql-connector-java') testCompile('org.springframework.boot:spring-boot-starter-test') + testCompile('com.jayway.jsonpath:json-path:2.1.0') //testCompile('org.springframework.security:spring-security-test:4.0.3.RELEASE') } diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties index a32a80a..0043b77 100644 --- a/gradle/wrapper/gradle-wrapper.properties +++ b/gradle/wrapper/gradle-wrapper.properties @@ -1,6 +1,6 @@ -#Tue Jan 12 17:13:48 KST 2016 +#Wed Feb 17 17:48:19 KST 2016 distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-2.10-bin.zip +distributionUrl=https\://services.gradle.org/distributions/gradle-2.10-all.zip diff --git a/src/main/java/net/anyjava/SpringDemoApplication.java b/src/main/java/net/anyjava/SpringDemoApplication.java index 8549ebb..60c43f0 100644 --- a/src/main/java/net/anyjava/SpringDemoApplication.java +++ b/src/main/java/net/anyjava/SpringDemoApplication.java @@ -25,14 +25,21 @@ public static void main(String[] args) { } + /** + * UTF-8 설정 + */ @Bean public HttpMessageConverter responseBodyConverter() { return new StringHttpMessageConverter(Charset.forName("UTF-8")); } + /** + * UTF-8 설정 + */ @Bean public OrderedCharacterEncodingFilter characterEncodingFilter() { - OrderedCharacterEncodingFilter filter = new OrderedCharacterEncodingFilter(); + OrderedCharacterEncodingFilter filter = + new OrderedCharacterEncodingFilter(); filter.setEncoding(this.httpEncodingProperties.getCharset().name()); filter.setForceEncoding(this.httpEncodingProperties.isForce()); filter.setOrder(Ordered.HIGHEST_PRECEDENCE); diff --git a/src/main/java/net/anyjava/demo/controller/CustomerController.java b/src/main/java/net/anyjava/demo/controller/CustomerController.java index cba4850..a9e52b7 100644 --- a/src/main/java/net/anyjava/demo/controller/CustomerController.java +++ b/src/main/java/net/anyjava/demo/controller/CustomerController.java @@ -4,7 +4,11 @@ import net.anyjava.demo.service.CustomerService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpStatus; -import org.springframework.web.bind.annotation.*; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.bind.annotation.ResponseStatus; +import org.springframework.web.bind.annotation.RestController; /** * Created by anyjava on 2016. 2. 11.. @@ -16,16 +20,20 @@ public class CustomerController { @Autowired private CustomerService customerService; + /** + * 고객조회 API + */ @RequestMapping(value = "/{id}", method = RequestMethod.GET) public Customer getCustomer(@PathVariable Long id) { - Customer c = customerService.select(id); + Customer customer = customerService.select(id); // 조회결과가 없을경우 404 리턴 - if ( c == null ) + if ( customer == null ) { throw new UserNotFoundException(id); + } - return c; + return customer; } @ResponseStatus(HttpStatus.NOT_FOUND) diff --git a/src/main/java/net/anyjava/demo/service/CustomerService.java b/src/main/java/net/anyjava/demo/service/CustomerService.java index 3b01fe4..bc996da 100644 --- a/src/main/java/net/anyjava/demo/service/CustomerService.java +++ b/src/main/java/net/anyjava/demo/service/CustomerService.java @@ -26,10 +26,17 @@ public Customer create(Customer customer) { } + /** + * 조회 + */ public Customer select(Long id) { return customerRepository.findOne(id); } + /** + * 삭제 + */ + @Transactional public boolean delete(Long id) { Customer customer = customerRepository.findOne(id); boolean returnV = false; diff --git a/src/test/java/net/anyjava/demo/WelcomeControllerTest.java b/src/test/java/net/anyjava/demo/WelcomeControllerTest.java index 54b0135..1a72d5c 100644 --- a/src/test/java/net/anyjava/demo/WelcomeControllerTest.java +++ b/src/test/java/net/anyjava/demo/WelcomeControllerTest.java @@ -35,7 +35,6 @@ public class WelcomeControllerTest { public void setup() { mockMvc = MockMvcBuilders .webAppContextSetup(webApplicationContext) -// .apply(springSecurity()) .build(); } diff --git a/src/test/java/net/anyjava/demo/controller/CustomerControllerTest.java b/src/test/java/net/anyjava/demo/controller/CustomerControllerTest.java index 8065733..75c62bf 100644 --- a/src/test/java/net/anyjava/demo/controller/CustomerControllerTest.java +++ b/src/test/java/net/anyjava/demo/controller/CustomerControllerTest.java @@ -16,9 +16,10 @@ import org.springframework.test.web.servlet.setup.MockMvcBuilders; import org.springframework.web.context.WebApplicationContext; +import static org.hamcrest.Matchers.is; +import static org.junit.Assert.assertTrue; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; -import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; -import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*; @RunWith(SpringJUnit4ClassRunner.class) @SpringApplicationConfiguration(classes = SpringDemoApplication.class) @@ -49,7 +50,8 @@ public void setup() { @Test public void testGetCustomer() throws Exception { - Customer customer = this.customerService.create(new Customer(null, "아", "이유")); + Customer customer + = this.customerService.create(new Customer(null, "아", "이유")); Gson gson = new Gson(); String json = gson.toJson(customer); @@ -57,13 +59,21 @@ public void testGetCustomer() throws Exception { get("/api/Customers/1").accept(MediaType.APPLICATION_JSON)) .andExpect(status().isOk()) .andExpect( - content().contentType(MediaType.APPLICATION_JSON_UTF8_VALUE)) - .andExpect(content().string(json)); + content().contentType( + MediaType.APPLICATION_JSON_UTF8_VALUE)) + .andExpect(content().string(json)) + .andExpect(jsonPath("$.id", is(customer.getLastName()))) + .andExpect(jsonPath("$.firstName", is(customer.getFirstName()))) + .andExpect(jsonPath("$.lastName", is(customer.getLastName()))); mockMvc.perform( get("/api/Customers/0").accept(MediaType.APPLICATION_JSON)) .andExpect(status().isNotFound()); } + @Test + public void 한글테스트() { + assertTrue(true); + } } \ No newline at end of file diff --git a/src/test/java/net/anyjava/demo/service/CustomerServiceTest.java b/src/test/java/net/anyjava/demo/service/CustomerServiceTest.java index 545c3a9..8d89ac7 100644 --- a/src/test/java/net/anyjava/demo/service/CustomerServiceTest.java +++ b/src/test/java/net/anyjava/demo/service/CustomerServiceTest.java @@ -54,7 +54,7 @@ public void tearDown() throws Exception { public void testCreate() { Customer customer = getCustomer(); Customer target = getCustomer(); - target.setId(this.maxId+1L); + target.setId(this.maxId + 1L); assertEquals(target, customerService.create(customer)); } @@ -73,7 +73,8 @@ public void testUpdate() { customer.setLastName("이유"); assertEquals(new Customer(this.maxId, "아", "이유"), customer); -// assertEquals(new Customer(this.maxId, "아", "이유"), customerService.update(customer)); +// assertEquals(new Customer(this.maxId, "아", "이유") +// , customerService.update(customer)); } @Test