我正在尝试获取不带path参数值的请求url。
假设我的完整url是
URl: http://localhost:8080/aaa/mock/abcd/1234/true
Path parameters: abcd, true
Output needed: /aaa/mock/abcd
我的web服务方法如下所示。
@Path(value = "/aaa/mock")
@Component
public class MockService
{
private static Log log = LogFactory.getLog(MockService.class);
//address
@GET
@Path(value = "/{mockrequest}/{status}")
@Produces(MediaType.JSON)
public String mockEngagement(@Context ContainerRequestContext request,@PathParam("mockrequest") String mockrequest,@PathParam("status") String status )
{
log.info("The mock url is"+request.getUriInfo().getRequestUri());
log.info("The mock url is"+request.getUriInfo().getAbsolutePath());
log.info("The mock url is"+request.getUriInfo().getBaseUri());
log.info("The mock url is"+request.getUriInfo().getMatchedURIs());
**//Out put needed /aaa/mock/abcd**
return "ajaja";
}
}
上面的调用都没有返回所需的信息。
我在想,是否有一个通用的过程来获得所需的输出,而不考虑路径参数的数量。
任何这样的方法。
发布于 2014-03-11 01:59:48
尝试使用UriInfo#getPath()
、UriInfo#getPath(boolean)
或UriInfo#getPathSegments()
。布尔参数是是否应该对路径进行编码。
https://jersey.java.net/apidocs/2.3.1/jersey/index.html
您还可以获取绝对路径和基本路径,然后使用URI#relativize(URI)
。
发布于 2019-01-30 22:24:10
试试这个:
request.getUriInfo().getPathSegments().get(0).getPath()
发布于 2017-11-11 18:48:45
public void filter(ContainerRequestContext context) throws IOException {
Message message = PhaseInterceptorChain.getCurrentMessage();
Set<Map.Entry<String, Object>> o = (Set<Map.Entry<String, Object>>)message.entrySet();
for (Map.Entry<String, Object> oo : o) {
String key = oo.getKey();
Object val = oo.getValue();
// Thises two properties gives the path of web service
//path_to_match_slash
//org.apache.cxf.request.uri
if(key.equals("path_to_match_slash"))
{ String v = (String)val;
System.out.println (key);
System.out.println (v);
}
if(key.equals("org.apache.cxf.request.uri"))
{ String v = (String)val;
System.out.println (key);
System.out.println (v);
}
}
}
此代码仅适用于apache cxf rest我们可以在ContainerRequestContext中找到path_to_match_slash、org.apache.cxf.request.uri属性
https://stackoverflow.com/questions/22307428
复制相似问题