본문 바로가기
728x90

Spring Boot37

Spring 정적 리소스 맵핑 - 기본 리소스 위치 classpath:/static classpath:/public classpath:/resources/ classpath:/META-INF/resource/ > 위 경로에 요청에 해당하는 리소스가 있다면 자동으로 매핑하여 리턴 ex) https://localhost:8080/hello.html 이라는 요청시 /static/hello.html이 있는 경우 해당 리소스를 리턴 * 맵핑설정을 변경하고 싶은 경우 spring.mvc.static-path-pattern=/sangguk/** 이제는 https://localhost:8080/hello.html > https://localhost:8080/sangguk/hello.html 으로 요청해야함 2022. 2. 27.
ViewResolver Accept header값에 따라 응답이 달라짐 > 해당하는 view에 맞게 Return xml로 응답을 받고 싶은 경우 > xpath사용 예시 @RunWith(SpringRunner.class) @WebMvcTest(UserController.class) public class UserControllerTest { @Autowired MockMvc mockMvc; @Test public void createUser_XML() throws Exception { String userJson = "{\"username\":\"sangguk\", \"password\":\"1234\"}"; mockMvc.perform(post("/users/create") .contentType(MediaType.APPLIC.. 2022. 2. 27.
HttpMediaTypeNotAcceptableException MockHttpServletRequest: HTTP Method = POST Request URI = /users/create Parameters = {} Headers = [Content-Type:"application/json;charset=UTF-8", Accept:"application/xml", Content-Length:"41"] Body = {"username":"sangguk", "password":"1234"} Session Attrs = {} Handler: Type = com.example.springbootwebmvc.user.UserController Method = com.example.springbootwebmvc.user.UserController#create(User) As.. 2022. 2. 27.
HttpMessageConverters : http요청 본문을 객체로 변환하거나, 객체를 http응답 본문으로 변경할 때 사용 @ResponseBody : 해당 객체의 converter를 사용해서 응답 return @RequestBody : 해당 객체의 converter를 사용해서 객체로 변환 @RestController public class UserController { @PostMapping("/userinfo") public @ResponseBody User postUserInformation(@RequestBody User user){ return user; } } * @ResponseBody은 @RestController를 사용하는 경우 생략 가능 Test 코드 @RunWith(SpringRunner.class) @WebMvcTes.. 2022. 2. 20.
728x90