Spring MVC @Pathvariable Getting Truncated

Spring MVC @PathVariable with a underscore (_) and dot (.) gets truncated

Try this it will allow dot(.) and underscore(_).

 @RequestMapping(method = RequestMethod.GET, value = "/abc/{something:.+}")

Spring - Path variable truncate after dot - annotation

The dot in the path variable at the end of the URI causes two unexpected behaviours (unexpected for the majority of users, except those familiar with the huge number of Spring configuration properties).

The first (which can be fixed using the {email:.+} regex) is that the default Spring configuration matches all path extensions. So setting up a mapping for /api/{file} will mean that Spring maps a call to /api/myfile.html to the String argument myfile. This is useful when you want /api/myfile.html, /api/myfile.md, /api/myfile.txt and others to all point to the same resource. However, we can turn this behaviour off globally, without having to resort to a regex hack on every endpoint.

The second problem is related to the first and correctly fixed by @masstroy. When /api/myfile.* points to the myfile resource, Spring assumes the path extension (.html, .txt, etc.) indicates that the resource should be returned with a specific format. This behaviour can also be very useful in some situations. But often, it will mean that the object returned by a method mapping cannot be converted into this format, and Spring will throw a HttpMediaTypeNotAcceptableException.

We can turn both off with the following (assuming Spring Boot):

@Configuration
public class WebConfig extends WebMvcConfigurerAdapter {

@Override
public void configurePathMatch(PathMatchConfigurer configurer) {
// turn off all suffix pattern matching
configurer.setUseSuffixPatternMatch(false);
// OR
// turn on suffix pattern matching ONLY for suffixes
// you explicitly register using
// configureContentNegotiation(...)
configurer.setUseRegisteredSuffixPatternMatch(true);
}

@Override
public void configureContentNegotiation(ContentNegotiationConfigurer configurer) {
configurer.favorPathExtension(false);
}
}

More about Content Negotiation.

How can I make spring MVC PathVariable more robust?

Based on https://www.baeldung.com/spring-mvc-custom-data-binder

You can try :

public class RobustId {
private Long id;

public RobustId(Long id) {
this.id = id;
}

// getters and setters below...
}

public class RobustIdConverter implements Converter<String, RobustId> {

@Override
public RobustId convert(String from) {
Long id = null;

for (int i=0; i<from.length(); i++) {
char c = from.charAt(i);
if ( !Character.isDigit(c) ) {
id = Long.parseLong(from.substring(0, i));
break;
}
}
return new RobustId(id);
}
}

@GetMapping("/{id}")
SomeReturn get(@PathVariable RobustId id) {
....
}

SPring 3.2 Path variable truncating

Try this (note the updated value for @RequestMapping)

@RequestMapping(value = "image/{fileName:[a-z]\\.[a-z]}}", method = RequestMethod.GET, produces = { MediaType.IMAGE_JPEG_VALUE,
MediaType.IMAGE_GIF_VALUE, MediaType.IMAGE_PNG_VALUE })
public @ResponseBody
byte[] getImage(@PathVariable("fileName") final String fileName);

See reference here.

How to prevent Spring MVC parameter from being truncated after .

Declaring the DefaultAnnotationHandlerMapping bean with useDefaultSuffixPattern=false is the right approach, but make sure you also comment out:

<mvc:annotation-driven />

See: How to change Spring MVC's behavior in handling url 'dot' character

How to setUseSuffixPatternMatch(false) to avoid truncated by '.' (dot) with specify urls in Spring?

Finally, I take this suggest in Spring Doc

Blockquote nother way to avoid this issue is by adding a slash at the end of our @PathVariable. This will enclose our second variable protecting it from Spring's default behavior: @GetMapping("/example/{firstValue}/{secondValue}/")



Related Topics



Leave a reply



Submit