Rest Spring server identifying a point as a regular expression

Asked

Viewed 177 times

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 username has a point, the server understands it as a regular expression and returns me an empty file. How to get around this?

  • In the case of the query method that does not return the user? If yes, post the code of this method

1 answer

0


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

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

Results that you will obtain:

/somepath/param will give rise to a variable with value "param"
/somepath/param.json will give rise to a variable with value "param.json"
/somepath/param.xml will give rise to 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 rise to 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));
    }
}

It could reinvent the wheel, but response obtained directly from the original version of Stackoverflow EN - Spring MVC @Pathvariable with dot (.) is Getting truncated

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

  • It had already gotten this answer in this link: https://www.mkyong.com/spring-mvc/spring-mvc-pathvariable-dot-get-truncated/ However, the addition of the information about more points, was very good. Thank you very much!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.