Is it possible to get the absolute request url without path parameters

java get url parameters
httpservletrequest get path
how to build url with parameters in java
containerrequestcontext
get url from string java
java url query parameters
java parse url path
java get base url

I am trying to get the request url without values of path parameters into it.

Consider my complete url is

URl: http://localhost:8080/aaa/mock/abcd/1234/true
Path parameters: abcd, true
Output needed: /aaa/mock/abcd

My web service method looks like this.

@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";
    }


}

None of the above calls return the required info.

I am thinking if there is a generic process to get the desired output irrespective of number of path parameters.

Any such methods.

Try UriInfo#getPath(), UriInfo#getPath(boolean), or UriInfo#getPathSegments(). The boolean argument is whether the path should be encoded or not.

https://jersey.java.net/apidocs/2.3.1/jersey/index.html

You could also get the absolute path and the base path and then use URI#relativize(URI).

Getting page URL without querystring in C#, JavaScript can access the current URL in parts. For this URL: So to get the full URL path in JavaScript: var newURL  Вы можете использовать splitфункцию с ?как разделитель , чтобы сделать это.. url = request.url.split("?")[0] Я не уверен, если это является наиболее эффективным / правильным методом, хотя.

Try this:

request.getUriInfo().getPathSegments().get(0).getPath()

Get URL and URL Parts in JavaScript, It supports the following URL schemes: file , ftp , gopher , hdl , http , https to the general structure of a URL: scheme://netloc/path;parameters?query#fragment . If the URL is decomposed before parsing, or is not a Unicode string, no error will This Request For Comments includes the rules for joining an absolute and a  This will return the absolute URL. www.google.com, for example, or it will return localhost URL if you are in the local environment. It should be kept in mind that Url.AbsoluteUri does return anything after the “#” symbol. When working on an app, you might also want to get different results for different purposes.

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

    }
}

this code could work only for apache cxf rest we can found path_to_match_slash , org.apache.cxf.request.uri properties in the ContainerRequestContext

20.16. urlparse — Parse URLs into components, PathEscape escapes the string so it can be safely placed inside a URL path The rawurl may be relative (a path, without a host) or absolute (starting with a scheme). was received in an HTTP request, so the rawurl is interpreted only as an absolute Path. In general there are multiple possible escaped forms of any path. This article will examine the properties of the Request object that will provide path and url information related to the application and the current request. First, here are a couple of tables of useful properties on the Request object and an example of the text they return for a given input URL.

url, Absolute URLs allow you to create links to other servers. of path variables similarly to normal parameters but specifying a placeholder inside your URL's path: The AbsolutePath property contains the path information that the server uses to resolve requests for information. Typically this is the path to the desired information on the server's file system, although it also can indicate the application or script the server must run to provide the information.

Standard URL Syntax, No CR or LF is allowed except in the final CRLF sequence. In this case the absolute path of the URI MUST be transmitted (see section 3.2.1, with semantics equivalent to the parameters on a programming language method invocation. We can differentiate between an absolute URL and a relative URL by looking only at the path part of the URL. If the path part of the URL starts with the "/" character, the browser will fetch that resource from the top root of the server, without reference to the context given by the current document. Let's look at some examples to make this

HTTP/1.1: Request, Use URL.human_repr() for getting human readable representation: > Passing encoded=True parameter prevents URL auto-encoding, user is responsible about Encoded path part of URL and query string, '/' for absolute URLs without path part. Changing host for relative URLs is not allowed, use URL.join() instead. >  If no path is provided to the url helper, a Illuminate\Routing\UrlGenerator instance is returned, allowing you to access information about the current URL: // Get the current URL without the query string echo url()->current(); // Get the current URL including the query string echo url()->full(); // Get the full URL for the previous

Comments
  • This is not applicable for HttpServletRequest
  • Please consider adding an explanation in plain English for your answer to improve its quality and describe how your code solves the OP's problem.
  • Welcome to SO. As much effort obviously has gone into this answer, it might be difficult to get a grasp at if it's just code. It's usual to comment the solution with a few sentences. Please edit your answer and add some explanation.
  • On a second glance, it seems obvious to me that this piece of code does not constitute any answer to the question. Especially "path_to_match_slash" and "org.apache.cxf.request.uri" seem to be complete out of scope here, even if given as example. You've been asked to expand on your answer, but you didn't revise it. In the current state it is not acceptable.