restTemplate.exchange для метода GET выдает ошибку сервера 500 - PullRequest
0 голосов
/ 12 декабря 2018

Код моего отдыха

Код контроллера

@RestController
@RequestMapping("/webphone")
@Component("WebphoneController")
@Service
public class WebphoneController {

Logger LOG = LoggerFactory.getLogger(WebphoneController.class);

@Autowired
private WebphoneService webphoneService;

@RequestMapping(value = "/findUsers", 
        method = { RequestMethod.GET },  
        consumes = "application/json", 
        produces = "application/json")
@ResponseBody
public ResponseEntity<List<User>> findUsers(
        @RequestBody(required=true)  WebphoneFilter webphoneFilter,
        @RequestBody(required=false)  Paging paging
        ) {

Код модульного теста:

@Test
public void webophoneController_whenGivenWebphoneFilter_shouldReturnListOfUsers() throws URISyntaxException {

    final String baseUrl = "http://localhost:" + randomServerPort + "/webphone/findUsers";
    WebphoneFilter filter = new WebphoneFilter();
    filter.setOrgLeaderId("jn1488");

    URI uri = new URI(baseUrl);
    /*String requestJson = Util.Object2Json(filter);
    System.out.println(requestJson);
    */
    HttpHeaders headers = new HttpHeaders();
    headers.setContentType(MediaType.APPLICATION_JSON);

    HttpEntity entity = new HttpEntity<>(filter,headers);

    RestTemplate restTemplate = new RestTemplate();
    ResponseEntity answer = restTemplate.exchange(uri, HttpMethod.GET, entity, List.class);
    System.out.println("webophoneController_whenGivenWebphoneFilter_shouldReturnListOfUsers>>>>"+ answer);
    Assert.assertEquals("No service found for the URL", answer);
}

Я получаю org.springframework.web.client.HttpServerErrorException: 500нуль

1 Ответ

0 голосов
/ 14 декабря 2018

Код контроллера:

@RestController("WebphoneController")
@RequestMapping("/webphone")
public class WebphoneController {

Logger LOG = LoggerFactory.getLogger(WebphoneController.class);

@Autowired
private WebphoneService webphoneService;

@RequestMapping(value = "/findUsers", 
        method = { RequestMethod.GET },  
        consumes = "application/json", 
        produces = "application/json")
@ResponseBody
public ResponseEntity<List<User>> findUsers(
        @RequestBody(required=true)  WebphoneFilter webphoneFilter,
        @RequestParam(value = "pageSize", required = false, defaultValue = "10000")  int pageSize
        ) {
    }
}

Код модульного теста

@RunWith(SpringRunner.class)
@SpringBootTest(classes = { MicroUiApplication.class, CommonConfiguration.class, WebMvcConfig.class}, webEnvironment=WebEnvironment.RANDOM_PORT)    
@TestPropertySource(locations= "classpath:application.properties")
@ActiveProfiles("dev")
public class WebphoneControllerTests {

@LocalServerPort
int randomServerPort;

@Test
public void webophoneController_whenGivenWebphoneFilter_shouldReturnListOfUsers() throws URISyntaxException {

    final String baseUrl = "http://localhost:" + randomServerPort + "/webphone/findUsers";
    WebphoneFilter filter = new WebphoneFilter();
    filter.setOrgLeaderId("jn1488");

    URI uri = new URI(baseUrl);
    /*String requestJson = Util.Object2Json(filter);
    System.out.println(requestJson);
    */
    HttpHeaders headers = new HttpHeaders();
    headers.setContentType(MediaType.APPLICATION_JSON);

    HttpEntity entity = new HttpEntity<>(filter,headers);

    RestTemplate restTemplate = new RestTemplate();
    ResponseEntity answer = restTemplate.exchange(uri, HttpMethod.GET, entity, List.class);
    System.out.println("webophoneController_whenGivenWebphoneFilter_shouldReturnListOfUsers>>>>"+ answer);
    Assert.assertEquals(HttpStatus.SC_OK, answer.getStatusCode());
  }
}
...