用于GET方法的restTemplate.exchange提供服务器错误500

时间:2018-12-12 19:40:36

标签: java spring-boot junit4 spring-restcontroller

我的休息控制器代码

控制器代码

@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 :(得分:0)

控制器代码:

@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());
  }
}