31

我使用 Spring restTemplate。我在单独的应用程序中制作了一个 REST 服务和客户端作为单元测试。我有返回用户列表和用户创建方法的方法:

@GET
@Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON,
        MediaType.TEXT_XML })
@Path("/all")
public Response getAllUsers() {
    List<User> list = dao.getAll();
    GenericEntity<List<User>> result = new GenericEntity<List<User>>(list) {
    };
    return Response.status(Status.OK).entity(result).build();
}

如果我请求在浏览器中显示所有用户,它会向我显示 xml。没关系。但是,当我尝试使用它时:

@Test
public void testGetAll() {
    List list = new RestTemplate().getForObject(URL + "all", List.class);
    System.out.println(list);
}

我有

WARNING: GET request for "http://localhost:8080/REST/all" resulted in 500 (Internal Server Error); invoking error handler

我试图调试这个。方法工作期间没有异常。浏览器向我显示用户的 xml。有什么问题?

另外,我想知道,如何从模板对象(用于测试)获取状态代码或消息?

感谢您的回答。

编辑:

我修改了我的测试方法:

@Test
public void testGetAll() {
    RestTemplate template = new RestTemplate();

    List<HttpMessageConverter<?>> messageConverters = new    ArrayList<HttpMessageConverter<?>>();
    Jaxb2RootElementHttpMessageConverter jaxbMessageConverter = new Jaxb2RootElementHttpMessageConverter();
    List<MediaType> mediaTypes = new ArrayList<MediaType>();
    mediaTypes.add(MediaType.APPLICATION_XML);
    jaxbMessageConverter.setSupportedMediaTypes(mediaTypes);
    messageConverters.add(jaxbMessageConverter);

    template.setMessageConverters(messageConverters);
    List list = template.getForObject(URL + "all",
            ArrayList.class);
    System.out.println(list);
}

我得到了例外:

org.springframework.web.client.RestClientException: Could not extract response: no suitable HttpMessageConverter found for response type [class java.util.ArrayList] and content type [application/xml]
at org.springframework.web.client.HttpMessageConverterExtractor.extractData(HttpMessageConverterExtractor.java:107)
at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:496)
at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:452)
at org.springframework.web.client.RestTemplate.getForObject(RestTemplate.java:222)
at com.nixsolutions.web.service.rest.UserRESTServiceTest.testGetAll(UserRESTServiceTest.java:61)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
4

4 回答 4

46

也许这样......

RestTemplate template = new RestTemplate(true);


ResponseEntity<TblGps[]> responseEntity = restTemplate.getForEntity(urlGETList, TblGps[].class);

TblGps[]=responseEntity.getBody();
于 2014-05-15T10:23:09.393 回答
7

您需要使用 的具体实现List,例如您可以使用ArrayList,请参见以下示例:

ResponseEntity<? extends ArrayList<User>> responseEntity = restTemplate.getForEntity(restEndPointUrl, (Class<? extends ArrayList<User>>)ArrayList.class, userId);

它甚至适用于完全通用的设置:

ResponseEntity<? extends ArrayList<HashMap<String,Object>>> responseEntity = restTemplate.getForEntity(restEndPointUrl, (Class<? extends ArrayList<HashMap<String,Object>>>)ArrayList.class, parameterId);
于 2015-06-24T14:54:07.940 回答
5

当您设置 时,Jaxb2RootElementHttpMessageConverter您会覆盖RestTemplate. 默认转换器之一(我认为是字符串转换器)可以处理text/xml类型。删除整个Jaxb2RootElementHttpMessageConverter,但在您期望的时候留下那部分,ArrayList.class而不是List.class这样,这将起作用:

@Test
public void testGetAll() {
    RestTemplate template = new RestTemplate();
    List list = template.getForObject(URL + "all",
        ArrayList.class);
    System.out.println(list);
}

您可能还需要添加一个accept标题来选择使用text/xml而不是其他生成的类型之一:

HttpHeaders requestHeaders = new HttpHeaders();
requestHeaders.setAccept(Arrays.asList(new MediaType[] {MediaType.TEXT_XML}));

exchangeRestTemplate(而不是getForObject)一起使用:

List list = template.exchange(URL + "all", new HttpEntity<String>(requestHeaders()), ArrayList.class);
于 2013-10-18T13:06:12.970 回答
0

你可以使用restTemplate.getForEntity(). 它将向您返回ResponseEntity所有响应信息(包括状态)。

于 2013-10-18T12:31:37.400 回答