使用对象作为参数调用get的Jersey不起作用

时间:2012-10-26 07:57:16

标签: json java-ee jersey

我是泽西新手,我正在尝试为搜索结果开发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 []作为私有成员的具体类。而不是GET,我在这里使用POST是固定代码。现在我的拼车搜索结果即将来临:)。

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

2 个答案:

答案 0 :(得分:3)

你做错了。 首先让我们分析你的错误:

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.

我们可以看到com.mcruiseon.carpool.concrete.SearchConcrete@676e3f附加在您请求的网址末尾(由您的测试用例“http://localhost:8081/mCruiseOnCarPool4All/carpool4all/Search/Request/”+ searchProvider确认)。这没有意义。

我假设你想用你的GET请求发送一个json?你做不到! GET没有正文但只有url(和标题),并且将json附加到你的url没有意义。如果你想发送json,你必须做一个POST。

您正在尝试执行与此帖What is the maximum length of JSON object received by JAX-RS web service using GET?类似的内容,请查看我的回答。

以下是如何使用Genson库http://code.google.com/p/genson/执行您想要的操作。 使用maven下载genson,它将在类路径中自动启用json支持并处理所有数据绑定。 然后更改服务器端方法:

@POST
@Consumes({ MediaType.APPLICATION_JSON })
@Produces({ MediaType.APPLICATION_JSON })
public Response search(SearchConcrete searchConcrete) {
    ...
}

将测试更改为:

// configure your client to use genson
ClientConfig config = new DefaultClientConfig();
config.getClasses().add(GensonJsonConverter.class);
cli = Client.create(config);

// then the test code
TypeOfTheResponse response = cli.resource("http://localhost:8081/mCruiseOnCarPool4All/carpool4all/Search/Request")
   .accept(MediaType.APPLICATION_JSON)
   .type(MediaType.APPLICATION_JSON)
   .post(TypeOfTheResponse.class, searchProvider);

备注:不要使用Object []作为响应,使用像MyResponseItem []

这样的具体java类

答案 1 :(得分:2)

您在这里错过了/

"...Request"+searchProvider

这一定是

"...Request/" + searchProvider

修改

您不能只是将一个Object添加到URL。此

"http://localhost:8081/mCruiseOnCarPool4All/carpool4all/Search/Request/"+searchProvider

将导致调用toString() SearchConcrete方法。结果是

com.mcruiseon.carpool.concrete.SearchConcrete@676e3f 

服务器无法从此重建SearchConcrete