Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
3.1 요청 매핑하기 (handler method) 정리 전 코드 정리
- Loading branch information
Showing
7 changed files
with
4 additions
and
252 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,8 @@ | ||
package me.nimkoes; | ||
|
||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.RequestBody; | ||
import org.springframework.web.bind.annotation.RequestParam; | ||
import org.springframework.web.bind.annotation.RestController; | ||
import org.springframework.stereotype.Controller; | ||
|
||
@RestController | ||
@Controller | ||
public class MyHelloController { | ||
|
||
@GetMapping("/hello") | ||
public String hello(@RequestParam("name") Person person) { | ||
System.out.println("handler method execute !!"); | ||
return "hello " + person.getName(); | ||
} | ||
|
||
@GetMapping("/message") | ||
public String message(@RequestBody String body) { | ||
return "Hello, " + body; | ||
} | ||
|
||
@GetMapping("/jsonMessage") | ||
public Person jsonMessage(@RequestBody Person person) { | ||
System.out.println(person.getName()); | ||
person.setName("My name is " + person.getName()); | ||
return person; | ||
} | ||
|
||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,38 +1,9 @@ | ||
package me.nimkoes; | ||
|
||
import java.util.concurrent.TimeUnit; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.http.CacheControl; | ||
import org.springframework.oxm.jaxb.Jaxb2Marshaller; | ||
import org.springframework.web.servlet.config.annotation.InterceptorRegistry; | ||
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; | ||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; | ||
|
||
@Configuration | ||
public class WebConfig implements WebMvcConfigurer { | ||
|
||
@Bean | ||
public Jaxb2Marshaller jaxb2Marshaller() { | ||
Jaxb2Marshaller jaxb2Marshaller = new Jaxb2Marshaller(); | ||
|
||
// jaxb 에서 사용하는 @XmlRootElement annotation 의 위치를 알려줘야 변환이 가능하다. | ||
jaxb2Marshaller.setPackagesToScan(Person.class.getPackage().getName()); | ||
return jaxb2Marshaller; | ||
} | ||
|
||
@Override | ||
public void addInterceptors(InterceptorRegistry registry) { | ||
registry.addInterceptor(new GreetingInterceptor()) | ||
.addPathPatterns("/hi") | ||
.order(0); | ||
registry.addInterceptor(new AnotherInterceptor()).order(-1); | ||
} | ||
|
||
@Override | ||
public void addResourceHandlers(ResourceHandlerRegistry registry) { | ||
registry.addResourceHandler("/m/**") // 어떤 패턴의 요청을 처리할지 정의 | ||
.addResourceLocations("classpath:/m/", "file:/Users/nimkoes/files/", "/myStatic/") // 리소스를 찾을 위치 | ||
.setCacheControl(CacheControl.maxAge(10, TimeUnit.MINUTES)); // 리소스가 변경되지 않은 동안 캐싱을 유지할 시간 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,112 +1,11 @@ | ||
package me.nimkoes; | ||
|
||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; | ||
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print; | ||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import java.io.StringWriter; | ||
import javax.xml.transform.Result; | ||
import javax.xml.transform.stream.StreamResult; | ||
import org.hamcrest.Matchers; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
import org.springframework.http.HttpHeaders; | ||
import org.springframework.http.MediaType; | ||
import org.springframework.oxm.Marshaller; | ||
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; | ||
import org.springframework.test.context.junit4.SpringRunner; | ||
import org.springframework.test.web.servlet.MockMvc; | ||
|
||
@RunWith(SpringRunner.class) | ||
@SpringBootTest | ||
@AutoConfigureMockMvc | ||
@WebMvcTest | ||
public class MyHelloControllerTest { | ||
|
||
// @WebMvcTest annotation 을 정의하면 MockMvc 를 주입받을 수 있다. | ||
@Autowired | ||
MockMvc mockMvc; | ||
|
||
// Spring Boot 를 사용하여 기본적으로 등록 된 bean 중 Jackson 이 제공하는 ObjectMapper 를 사용 | ||
@Autowired | ||
ObjectMapper objectMapper; | ||
|
||
@Autowired | ||
Marshaller marshaller; | ||
|
||
@Test | ||
public void hello() throws Exception { | ||
this.mockMvc.perform(get("/hello") | ||
.param("name", "nimkoes")) | ||
.andDo(print()) | ||
.andExpect(content().string("hello nimkoes")); | ||
} | ||
|
||
@Test | ||
public void staticTest() throws Exception { | ||
this.mockMvc.perform(get("/index.html")) | ||
.andDo(print()) | ||
.andExpect(status().isOk()) | ||
.andExpect(content().string(Matchers.containsString("Hello Nimkoes"))); | ||
} | ||
|
||
@Test | ||
public void staticTest_M() throws Exception { | ||
this.mockMvc.perform(get("/m/index.html")) | ||
.andDo(print()) | ||
.andExpect(status().isOk()) | ||
.andExpect(content().string(Matchers.containsString("Hello Mobile Nimkoes"))) | ||
.andExpect(header().exists(HttpHeaders.CACHE_CONTROL)); | ||
} | ||
|
||
@Test | ||
public void stringMessage() throws Exception { | ||
this.mockMvc.perform(get("/message").content("nimkoes")) | ||
.andDo(print()) | ||
.andExpect(status().isOk()) | ||
.andExpect(content().string("Hello, nimkoes")); | ||
} | ||
|
||
@Test | ||
public void jsonMessage() throws Exception { | ||
|
||
Person person = new Person(); | ||
person.setName("nimkoes"); | ||
|
||
// 객체를 json 문자열로 변환 | ||
String jsonString = objectMapper.writeValueAsString(person); | ||
|
||
this.mockMvc.perform(get("/jsonMessage") | ||
.contentType(MediaType.APPLICATION_JSON_UTF8) // context type 을 사용하여 request 데이터가 json 형식임을 알려줌 | ||
.accept(MediaType.APPLICATION_JSON_UTF8) // accept 를 사용하여 response 데이터가 json 형식이기를 바란다고 알려줌 | ||
.content(jsonString)) // request body 에 jsonString 입력 | ||
.andDo(print()) | ||
.andExpect(status().isOk()) | ||
.andExpect(jsonPath("$.name").value("My name is nimkoes")); | ||
} | ||
|
||
|
||
@Test | ||
public void xmlMessage() throws Exception { | ||
|
||
Person person = new Person(); | ||
person.setName("nimkoes"); | ||
|
||
// 객체를 xml 문자열로 변환 | ||
StringWriter stringWriter = new StringWriter(); | ||
Result result = new StreamResult(stringWriter); | ||
marshaller.marshal(person, result); | ||
|
||
String xmlString = stringWriter.toString(); | ||
|
||
|
||
this.mockMvc.perform(get("/jsonMessage") | ||
.contentType(MediaType.APPLICATION_XML) // context type 을 사용하여 request 데이터가 xml 형식임을 알려줌 | ||
.accept(MediaType.APPLICATION_XML) // accept 를 사용하여 response 데이터가 xml 형식이기를 바란다고 알려줌 | ||
.content(xmlString)) // request body 에 xmlString 입력 | ||
.andDo(print()) | ||
.andExpect(status().isOk()) | ||
.andExpect(xpath("person/name").string("My name is nimkoes")); | ||
} | ||
} |