Rest Spring Server identifying a period as a regular expression

0

I have a Rest service with the following method:

@RequestMapping(value = "/usuario/{login}", method = RequestMethod.GET)
    @ResponseBody
    public ResponseEntity<InputStreamResource> usuario(@PathVariable("login") String login, HttpServletRequest request) {
        User user = userService.buscarPorNickname(login);
        InputStream is = null;
        if (user != null) {
            try {
                is = new FileInputStream(user.getPicture().getCaminho());
            } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        if (is == null) {
            return ResponseEntity.status(HttpStatus.NOT_FOUND).body(null);
        } else {
            return ResponseEntity.ok().contentType(MediaType.IMAGE_PNG).body(new InputStreamResource(is));
        }
    }

When the user name has a period, the server understands it as a regular expression and returns me an empty file. How to get around this?

    
asked by anonymous 20.07.2017 / 04:25

1 answer

0

If you want to get the full format, you should use the regular expression:

/ somepath / {variable:. +} in your case "/usuario/{login:.+}"

Results you'll get:

/ somepath / param will give rise to a variable with value "param"
/somepath/param.json will give a variable with value "param.json"
/somepath/param.xml will give a variable with value "param.xml"
/somepath/param.anything will give rise to a variable with value "param.anything"
/somepath/param.value.json will give a variable with value "param.value.json"

So it would look like this:

@RequestMapping(value = "/usuario/{login:.+}", method = RequestMethod.GET)
@ResponseBody
public ResponseEntity<InputStreamResource> usuario(@PathVariable("login") String login, HttpServletRequest request) {
    User user = userService.buscarPorNickname(login);
    InputStream is = null;
    if (user != null) {
        try {
            is = new FileInputStream(user.getPicture().getCaminho());
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    if (is == null) {
        return ResponseEntity.status(HttpStatus.NOT_FOUND).body(null);
    } else {
        return ResponseEntity.ok().contentType(MediaType.IMAGE_PNG).body(new InputStreamResource(is));
    }
}

I could reinvent the wheel, but I got the answer directly from the original StackOverflow EN - Spring MVC @PathVariable with dot (.) Is getting truncated

If it is possible to exist more than one point, the expression would be:
/somepath/{variable:. * } in your case "/ username / {login:. * }"

    
20.07.2017 / 15:40