The request cannot be received. Email addresses as Spring interprets it as extensions

(see part of the edit below 08/20/2015)

I had a similar problem recently ( Request for final works only with a slash (spring REST annotations) ), and the solution is to add a regular expression to the value of @RequestMapping (cm. And Spring MVC @PathVariable, truncated ) ...

But now we realized that the problem still exists, but only for email addresses ending in .com or .org. It is very strange.

In a nutshell, I am using Spring Annotations building a REST service. I have a GET request with three parameters, the last one is an email.

My controller:

@RequestMapping(method = GET, value = "/{value1}/{value2}/{value3:.+}",
  produces = MediaType.APPLICATION_JSON_VALUE + ";charset=UTF-8")    
  public ResponseEntity<MyResponseType> getMyResource(
    @ApiParam(value = "...",...)
    @PathVariable("value1") String value1,
    @ApiParam(value = "...",...)
    @PathVariable("value2") String value2,
    @ApiParam(value = "...",...)
    @PathVariable("value3") String value3) {
      //...
}

      

If I call: http: // myserver: 8080 / myresource / value1 / value2 / value3

with value3 = email@domain.de /co.uk/.name/.biz/.info, no problem.

But with some top level domains (.com, .org so far) I am getting Http status code 406 (not accepted).

It works if I add a trailing slash:

http: // myserver: 8080 / myresource / value1 / value2 / value3 /

Since we are using swagger and the swagger does not add trailing slashes, adding a trailing slash is not an option.

What could be causing this problem?

I am using ErrorHandler which extends ResponseEntityExceptionHandler. I debugged it and found that HttpMediaTypeNotAcceptableException ("Couldn't find an acceptable view"). But I still can't find out who is leaving him and why.


change

I found out that the path is http: // myserver: 8080 / myresource / value1 / value2 / myemail@somewhere.com

is interpreted as a file and "com" is the file extension for the media type "application / x-msdownload", so the Spring class of ProducesRequestCondition.ProduceMediaTypeExpression uses the string getMediaType () in the matchMediaType method. isCompatibleWith (AcceptedMediaType) "fails because the actual media type generated is" application / json; charset = UTF-8 ", not" application / x-msdownload ".

So, the questions change to: How can I make Spring understand that .com is not an extension?

+3


source to share


2 answers


This thread helped me: SpringMVC: Inconsistent rendering behavior based on url extension

Obviously this is not a bug but a "feature" and there are two different ways to disable it. I used the annotation version:



@Configuration
@EnableWebMvc
public class RestCommonsMvcConfig extends WebMvcConfigurerAdapter {

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

      

+4


source


I ran into this issue when upgrading from spring from 3.1.2 to 3.2.7. Nina's answer helped me (which should be marked as an answer). But instead of expanding, WebMvcConfigurerAdapter

I did it in mine WebConfig

which expanded WebMvcConfigurationSupport

.



@Configuration
public class WebConfig extends WebMvcConfigurationSupport {
  @Override
  public void configureContentNegotiation(ContentNegotiationConfigurer configurer) {
      super.configureContentNegotiation(configurer);
      configurer.favorPathExtension(false).favorParameter(true);
  }
}

      

0


source







All Articles