我是泽西岛的新手,我正在尝试为搜索结果开发 GET。为此,我需要发送一个带有搜索条件和数据的对象。我想知道我做错了什么。我的 Junit 测试用例出现以下异常
com.sun.jersey.api.client.UniformInterfaceException: GET http://localhost:8081/mCruiseOnCarPool4All/carpool4all/Search/Request/com.mcruiseon.carpool.concrete.SearchConcrete@676e3f returned a response status of 404 Not Found
at com.sun.jersey.api.client.WebResource.handle(WebResource.java:686)
at com.sun.jersey.api.client.WebResource.access$200(WebResource.java:74)
at com.sun.jersey.api.client.WebResource$Builder.get(WebResource.java:507)
at test.carpool4all.SingleSearchTest.testPost(SingleSearchTest.java:89)
我的服务器端 GET
@GET
@Path ("Request/{search}")
@Consumes({ MediaType.APPLICATION_JSON })
@Produces({ MediaType.APPLICATION_JSON })
public Response search(@PathParam("search") SearchConcrete searchConcrete) {
SearchJourneyRequest request = new SearchJourneyRequest(searchConcrete) ;
SearchJourneyResponse response ;
clientSession = sessionManager.getClientSession(searchConcrete.getIdentityHash()) ;
clientSession.getSendQueue().sendRequest(request) ;
try {
response = (SearchJourneyResponse)clientSession.waitAndGetResponse(request) ;
} catch (WaitedLongEnoughException e) {
return Response.serverError().build() ;
} catch (UnableToResolveResponseException e) {
return Response.serverError().build() ;
}
return Response.ok(response.getSearchResults()).build();
}
客户端 Junit 测试
SearchConcrete searchProvider = new SearchConcrete(Globals.SearchCriteria.FlexiTime,
identityHash,
// more parameters
);
service = client.resource(UriBuilder.fromUri("http://localhost:8081/mCruiseOnCarPool4All/carpool4all/Search/Request/"+searchProvider).build());
Object[] searchResults = service.type(MediaType.APPLICATION_JSON).get(Object[].class);
编辑:感谢@eugen,为了解决这个问题,我添加了一个具体的类,我的 Object[] 作为私有成员。我使用 POST 而不是 GET,这里是固定代码。现在我的拼车搜索结果来了:)。
服务 = client.resource(UriBuilder.fromUri(
"http://localhost:8081/mCruiseOnCarPool4All/carpool4all/Search/Request").build());
SearchResultsConcrete searchResults = service.type(MediaType.APPLICATION_JSON).post(SearchResultsConcrete.class, searchProvider);
assertNotNull(searchResults);
assertNotNull(searchResults.getSearchResults()) ;
assertTrue(searchResults.getSearchResults().length == 3) ;
assertTrue(searchResults.getSearchResults()[SearchJourneyResponse.FLEXI_POSITION].length > 0) ;
assertTrue(searchResults.getSearchResults()[SearchJourneyResponse.FLEXIENDTIME_POSITION].length > 0) ;
@POST
@Path ("Request")
@Consumes({ MediaType.APPLICATION_JSON })
@Produces({ MediaType.APPLICATION_JSON })
public Response search(JAXBElement<SearchConcrete> element) {
SearchJourneyRequest request = new SearchJourneyRequest((SearchConcrete)element.getValue()) ;
SearchJourneyResponse response ;
clientSession = sessionManager.getClientSession(((SearchConcrete)element.getValue()).getIdentityHash()) ;
clientSession.getSendQueue().sendRequest(request) ;
try {
response = (SearchJourneyResponse)clientSession.waitAndGetResponse(request) ;
} catch (WaitedLongEnoughException e) {
return Response.serverError().build() ;
} catch (UnableToResolveResponseException e) {
return Response.serverError().build() ;
}
return Response.ok(response.getSearchResults()).build();
}