inflearn logo
강의

강의

N
챌린지

챌린지

멘토링

멘토링

N
클립

클립

로드맵

로드맵

지식공유

스프링 웹 MVC

WebMvcConfigurer 1부 Formatter - localhost:8080/hello?name=keesun

292

meon take

작성한 질문수 5

0

package me.whiteship.web_mvc_onfigurer;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class SampleController {

@GetMapping("/hello")
public String hello(@RequestParam("name") Person person) {
return "hello " + person.getName();
}
}

package me.whiteship.web_mvc_onfigurer;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;

import static org.junit.Assert.*;
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.content;

@RunWith(SpringRunner.class)
@WebMvcTest
public class SampleControllerTest {

@Autowired
MockMvc mockMvc;

@Test
public void hello() throws Exception {
this.mockMvc.perform(get("/hello")
.param("name", "keesun"))
.andDo(print())
.andExpect(content().string("hello keesun"));
}

}

package me.whiteship.web_mvc_onfigurer;

import org.springframework.context.annotation.Configuration;
import org.springframework.format.FormatterRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class WebConfig implements WebMvcConfigurer {
}

package me.whiteship.web_mvc_onfigurer;

public class Person {
private String name;

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}
}

package me.whiteship.web_mvc_onfigurer;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;

import static org.junit.Assert.*;
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.content;

@RunWith(SpringRunner.class)
@WebMvcTest
public class SampleControllerTest {

@Autowired
MockMvc mockMvc;

@Test
public void hello() throws Exception {
this.mockMvc.perform(get("/hello")
.param("name", "keesun"))
.andDo(print())
.andExpect(content().string("hello keesun"));
}

}

junit 4로 테스트 결과 아래와 같이 에러가 발생합니다.

Resolved Exception:

             Type = org.springframework.web.method.annotation.MethodArgumentConversionNotSupportedException

MockHttpServletRequest:

      HTTP Method = GET

      Request URI = /hello

       Parameters = {name=[keesun]}

          Headers = []

             Body = <no character encoding set>

    Session Attrs = {}

Handler:

             Type = me.whiteship.web_mvc_onfigurer.SampleController

           Method = me.whiteship.web_mvc_onfigurer.SampleController#hello(Person)

Async:

    Async started = false

     Async result = null

Resolved Exception:

             Type = org.springframework.web.method.annotation.MethodArgumentConversionNotSupportedException

ModelAndView:

        View name = null

             View = null

            Model = null

FlashMap:

       Attributes = null

MockHttpServletResponse:

           Status = 500

    Error message = null

          Headers = []

     Content type = null

             Body = 

    Forwarded URL = null

   Redirected URL = null

          Cookies = []

MockHttpServletRequest:

      HTTP Method = GET

      Request URI = /hello

       Parameters = {name=[keesun]}

          Headers = []

             Body = <no character encoding set>

    Session Attrs = {}

Handler:

             Type = me.whiteship.web_mvc_onfigurer.SampleController

           Method = me.whiteship.web_mvc_onfigurer.SampleController#hello(Person)

Async:

    Async started = false

     Async result = null

Resolved Exception:

             Type = org.springframework.web.method.annotation.MethodArgumentConversionNotSupportedException

ModelAndView:

        View name = null

             View = null

            Model = null

FlashMap:

       Attributes = null

MockHttpServletResponse:

           Status = 500

    Error message = null

          Headers = []

     Content type = null

             Body = 

    Forwarded URL = null

   Redirected URL = null

          Cookies = []

java.lang.AssertionError: Response content 

Expected :hello keesun

Actual   :

<Click to see difference>

소스 확인 좀 부탁드려도 될까요?

MVC java spring

답변 1

0

백기선

name이라는 문자열( String)을 Person으로 변환할 수 있는 코드가 안보이네요. 그래서 MethodArgumentConversionNotSupportedException 이런 에러가 발생한거 같네요.

7:58 예제 코드 찾는 법

0

256

2

PATCH의 Idempotent에 대한 질문

0

257

1

2:51초 질문입니다.

0

324

1

만약 어플리케이션컨텍스트에 필터를 설정하지 않으면 어떻게되나요?

0

361

0

web.xml에 위치한 애플리케이션 web.xml 내에서 파싱 오류 발생

0

515

0

Spring Boot 2.6 이상 버전에서의 DispatcherServlet

0

366

1

Event`Xxx`Controller 분리할때의 기준에 관한 질문입니다.

0

634

1

11분 57초 쯤, `returnValueHandlers` 들의 네이밍컨벤션 관련 질문입니다.

0

379

1

@EnableWebmvc 설정을 하면

0

415

1

springframework와의 차이점

0

283

1

@RequestBody의 바인딩에러를 잡는 방법

1

1655

2

안녕하세요 기선님 강의를 보다 서블릿에 궁금증이 생겨 질문 드립니다

1

237

1

HelloService의 값을 전달을 못 받습니다.

0

296

1

ServletContext와 WebApplicationContext의 관계 질문

1

740

1

하나의 서비스에, 두개의 리포지토리도 연결이 되나요?

0

364

1

view name 리턴에 대해

0

290

2

Formatter print 질문 드립니다.

0

232

1

에리 메시지가 한글입니다.

0

293

1

java 클래스파일로 설정되어 있던 것을 web.xml로 바꾸는 경우

0

338

1

@RequestParam

0

255

1

@Retention 질문

0

229

1

Event.builder() 관련 mac에서 lombok 문제 있으신 분들

0

198

1

스프링 부트 사용시 WebMvcConfigurerSupport 클래스

0

252

1

http://localhost:8080/hello 입력시 오류

0

492

4