Java RestEasy:@PathParam在字符串有空格时返回第一个单词
我的Java RestEasy:@PathParam在字符串有空格时返回第一个单词,java,jboss,jax-rs,resteasy,wildfly,Java,Jboss,Jax Rs,Resteasy,Wildfly,我的API端点看起来像 @GET @Consumes(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON) @Path("groups/{groupName}") public Response getCategoriesByGroupName(@PathParam("groupName") @Nonnull final String groupName) { return Response.ok(getCat
API
端点看起来像
@GET
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
@Path("groups/{groupName}")
public Response getCategoriesByGroupName(@PathParam("groupName") @Nonnull final String groupName) {
return Response.ok(getCategoryTO().getCategoriesByGroupName(categoryManager, groupName)).build();
}
当我试图到达终点时
curl -H"Content-Type: application/json" -H"BEARER:792345452:78f7f8a4-a8c9-454a-93a8-6633a1076781:169000000" "https://myapp.com/rest/categories/groups/Utilities"
我得到了正确的JSON
返回,这意味着groupName
被Utilities
但当我这么做的时候
curl -H"Content-Type: application/json" -H"BEARER:792345452:78f7f8a4-a8c9-454a-93a8-6633a1076781:169000000" "https://myapp.com/rest/categories/groups/Food & Drink"
我将服务器错误视为
Caused by: javax.ejb.EJBException: javax.persistence.NoResultException: no categories exists with groupName: Food
问题是,groupName
被替换为Food
,而不是Food&Drink
我尝试使用其他groupName
s,发现每当我的groupName
有空格时,只有第一个单词被替换为groupName
如何解决此问题,使传入URL
的整个字符串变为groupName
我的服务器部署在与我的端点无关的
Wildfly 8
上
正如@Sotirios所提到的,在将URL发送到服务器之前,我需要对其进行编码
当我这么做的时候,事情开始起作用了
curl -H"Content-Type: application/json" -H"BEARER: 792345452:78f7f8a4-a8c9-454a-93a8-6633a1076781:169000000" "https://myapp.com/rest/categories/groups/Food%20%26%20Drink"
[{"id":"9b1e97f2-ac7d-4caf-85fc-476cd97dd6cb","name":"Alcohol & Bars","groupName":"Food & Drink"}]
&
是一个特殊字符。你需要对它进行编码。你能指导我怎么做吗?使用urlcoder.encode(stringToEncode,“UTF-8”)代码>URL编码是一种常见做法。您不仅需要用%20