gpt4 book ai didi

json - Jersey 调用带有对象作为参数的 get 不起作用

转载 作者:行者123 更新时间:2023-12-02 05:26:02 24 4
gpt4 key购买 nike

我是 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[] 作为私有(private)成员。我在这里使用 POST 而不是 GET,这是固定代码。现在我的拼车搜索结果来了:)。

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

最佳答案

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

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 附加在您请求的 url 末尾(由您的测试用例“http://localhost:8081/mCruiseOnCarPool4All/carpool4all/Search/Request/”+searchProvider 确认)。这没有意义。

我假设您想通过 GET 请求发送一个 json?你不能那样做! GET 没有正文,只有 url(和 header ),并且将 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 支持并处理所有数据绑定(bind)。然后更改您的服务器端方法:

@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[] 作为响应,使用具体的 java 类,如 MyResponseItem[]

关于json - Jersey 调用带有对象作为参数的 get 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13083005/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com