Store @PathParam values from REST call in a list or array

前端 未结 2 575
情深已故
情深已故 2020-12-15 01:59

My function looks like this:

    @PUT
    @Path(\"property/{uuid}/{key}/{value}\")
    @Produces(\"application/xml\")    
    public Map

        
2条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-15 02:25

    A workaround:

    @Path("/foo/bar/{other: .*}
    public Response foo(@PathParam("other") VariableStrings vstrings) {
    
       String[] splitPath = vstrings.getSplitPath();
    
    
    }
    

    VariableStrings class:

    public class VariableStrings {
    
       private String[] splitPath;
    
       public VariableStrings(String unparsedPath) {
         splitPath = unparsedPath.split("/");
       }
    }
    

    Path segment sequence to vararg array in JAX-RS / Jersey?

    Another example where you map the optional parameter to a Map:

    @GET
    @ Produces({"application/xml", "application/json", "plain/text"})
    @ Path("/location/{locationId}{path:.*}")
    public Response getLocation(@PathParam("locationId") int locationId, @PathParam("path") String path) {
        Map < String, String > params = parsePath(path);
        String format = params.get("format");
        if ("xml".equals(format)) {
            String xml = " parsePath(String path) {
        if (path.startsWith("/")) {
            path = path.substring(1);
        }
        String[] pathParts = path.split("/");
        Map < String, String > pathMap = new HashMap < String, String > ();
        for (int i = 0; i < pathParts.length / 2; i++) {
            String key = pathParts[2 * i];
            String value = pathParts[2 * i + 1];
            pathMap.put(key, value);
        }
        return pathMap;
    }
    

提交回复
热议问题