3

I am sending an email-id to server as a path variable as:

@RequestMapping(value = "/resetPassword/{email:.+}", method = RequestMethod.GET)
public @ResponseBody MyResponse resetPassword(HttpServletRequest request, @PathVariable("email") String email) 
{
    MyResponse res = new MyResponse();
    res.setMsg("some Text");
    return res;
}

and I am calling the method by jQuery as:

var email = $("#fpusername").val();
$.ajax({
    type : "GET",
    url : "./useraccount/resetPassword/" + email,
    dataType : "json",
    async : true,
    success : function(data) {
        alert(data.msg);
    }
});

The same method is working when I am sending myname@gmail as the email value but getting the below error when sending myname@gmail.com

406 [The resource identified by this request is only capable of generating responses with characteristics not acceptable according to the request "accept" headers]

Arpan Das
  • 1,015
  • 3
  • 24
  • 57
  • 1
    possible duplicate of [Spring MVC @PathVariable with dot (.) is getting truncated](http://stackoverflow.com/questions/16332092/spring-mvc-pathvariable-with-dot-is-getting-truncated) – Martin Frey May 03 '15 at 15:08
  • @Martin Frey : no here the problem is not that my path variable get truncated after the (.). here the problem is when the path variable contains a (.) then the response body annotation is not working, thus ajax is not getting the proper response from server – Arpan Das May 03 '15 at 16:02
  • Ah true. I missed the regex. Have you tried fixing the response mediatype to json? – Martin Frey May 03 '15 at 16:07
  • I think that is not the problem here, because the same code is working for an email like "myname@gmail" and returning the response in expected JSON format, but not working for "myname@gmail.com", I think its related to url encoding. – Arpan Das May 03 '15 at 16:12
  • I guess it may be related with the fact that ".com" is taken as "file extension". See the second answer from the suggested duplicate (http://stackoverflow.com/a/20743941/853558). Have you tried sending e.g. "myname@gmail.json". What's the response? – Lukasz Wiktor May 08 '15 at 10:12

2 Answers2

2

The doc warns about this pitfall using an equivalent example

By default Spring MVC automatically performs ".*" suffix pattern matching so that a controller mapped to /person is also implicitly mapped to /person.*. This allows indicating content types via file extensions, e.g. /person.pdf, /person.xml, etc. A common pitfall however is when the last path segment of the mapping is a URI variable, e.g. /person/{id}. While a request for /person/1.json would correctly result in path variable id=1 and extension ".json", when the id naturally contains a dot, e.g. /person/joe@email.com the result does not match expectations. Clearly here ".com" is not a file extension.

When deciding on the content type of the resonse, Spring uses the so called PPA strategy (Path, Parameter, Accept header). Here your .com is taken as a path (extension), and an attempt is made to resolve the representation based on it, hence your exception. You can take two roads into fixing this.

Either configure Spring to use only the registered suffixes e.g. in XML

<mvc:annotation-driven>
    <mvc:path-matching registered-suffixes-only="true" />
</mvc:annotation-driven>

the doc and the equivalent java config available here

OR, turn off mathching by path if that is viable solution for you, e.g.

<mvc:annotation-driven content-negotiation-manager="contentNegotiationManager" />

<bean id="contentNegotiationManager" class="org.springframework.web.accept.ContentNegotiationManagerFactoryBean">
    <property name="favorPathExtension" value="false" />
    <property name="mediaTypes" >
        <value>
            json=application/json
            xml=application/xml
        </value>
    </property>
</bean>

the doc and the equivalent java config available here

Master Slave
  • 27,771
  • 4
  • 57
  • 55
0

Have you mapped the url pattern in web.xml? If not you have to map the url this way to accept your url as valid by the spring:

<servlet-mapping>
    <servlet-name>spring</servlet-name>
    <url-pattern>*.com</url-pattern>
</servlet-mapping>
Akash Rajbanshi
  • 1,553
  • 11
  • 23