Currently I am using Spring mvc request mapping using headers.
Following are the two methods I have:
Method 1:
@RequestMapping(value = "/accounts", method = RequestMethod.GET,
headers="Accept=application/vnd.se.company1.product1+json; version=2.0")
@ResponseBody
public ResponseEntity<String> loginVerionOne(final HttpServletRequest request){}
Method 2:
@RequestMapping(value = "/accounts", method = RequestMethod.GET,
headers="Accept=application/vnd.se.company1.product1+json; version=3.0")
@ResponseBody
public ResponseEntity<String> loginVersionTwo(final HttpServletRequest request) {}
The problem here is that when client sends accept header in it's request in below format:
Accept=application/vnd.se.company1.product1+json; version=3.0
Then spring maps to the first method only even if the version is 3.0.
I have read in one of the post that spring ignores request mapping after ; (semi-colon) in request mapping using headers.
We are using custom accept header where we need to keep content type and version separately.
The mapping works if I use Accept header in below format:
Accept=application/vnd.se.company1.product1.v2.0+json
But I do not want to use above format.
Any help in this issue will be appreciated.
Following is my code used :
RequestMappingController
========================
@RequestMapping(value = "/helloworld",method=RequestMethod.GET)
@SubdomainMapping(subdomains="application/vnd.se.company1.product1+json;version=2.0")
public ModelAndView helloWordNew(HttpServletRequest req){
String message = "Hello World Version 2.0";
return new ModelAndView("helloworld", "message",message);
}
@RequestMapping(value = "/helloworld",method=RequestMethod.GET)
@SubdomainMapping(subdomains="application/vnd.se.company1.product1+json;version=3.0")
public ModelAndView helloWord(HttpServletRequest req){
String message = "Hello World Version 3.0";
return new ModelAndView("helloworld", "message",message);
}
RequestCondition:
================
public class SubdomainRequestCondition implements
RequestCondition<SubdomainRequestCondition> {
private final Set<String> subdomains;
public SubdomainRequestCondition(String subdomains) {
this(Arrays.asList(subdomains));
}
public SubdomainRequestCondition(Collection<String> subdomains) {
this.subdomains = Collections.unmodifiableSet(new HashSet<String>(
subdomains));
}
@Override
public SubdomainRequestCondition combine(SubdomainRequestCondition other) {
Set<String> allRoles = new LinkedHashSet<String>(this.subdomains);
allRoles.addAll(other.subdomains);
return new SubdomainRequestCondition(allRoles);
}
@Override
public SubdomainRequestCondition getMatchingCondition(
HttpServletRequest request) {
String header = request.getHeader("Accept");
for (String s : subdomains) {
if (s.equals(header)) {
return this;
}
}
return null;
}
@Override
public int compareTo(SubdomainRequestCondition other,
HttpServletRequest request) {
return org.apache.commons.collections.CollectionUtils.removeAll(other.subdomains, this.subdomains).size();
}
}
HandlerMapping
==============
public class CustomRequestMappingHandlerMapping extends
RequestMappingHandlerMapping {
@Override
protected RequestCondition<?> getCustomTypeCondition(Class<?> handlerType) {
SubdomainMapping typeAnnotation = AnnotationUtils.findAnnotation(
handlerType, SubdomainMapping.class);
return createCondition(typeAnnotation);
}
@Override
protected RequestCondition<?> getCustomMethodCondition(Method method) {
SubdomainMapping methodAnnotation = AnnotationUtils.findAnnotation(
method, SubdomainMapping.class);
return createCondition(methodAnnotation);
}
private RequestCondition<?> createCondition(SubdomainMapping accessMapping) {
return (accessMapping != null) ? new SubdomainRequestCondition(accessMapping.subdomains()) : null;
}
}
applicationContext.xml
======================
<context:component-scan base-package="com.net.controllers"/>
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix">
<value>/WEB-INF/views/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
<bean name="handlerAdapter" class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
<property name="webBindingInitializer">
<bean class="org.springframework.web.bind.support.ConfigurableWebBindingInitializer">
<property name="validator">
<bean class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean"/>
</property>
</bean>
</property>
<property name="messageConverters">
<list>
<bean class="org.springframework.http.converter.ByteArrayHttpMessageConverter"></bean>
<bean class="org.springframework.http.converter.StringHttpMessageConverter"></bean>
<bean class="org.springframework.http.converter.ResourceHttpMessageConverter"></bean>
<bean class="org.springframework.http.converter.xml.SourceHttpMessageConverter"></bean>
<bean class="org.springframework.http.converter.xml.XmlAwareFormHttpMessageConverter"></bean>
<bean class="org.springframework.http.converter.xml.Jaxb2RootElementHttpMessageConverter"></bean>
<bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"></bean>
</list>
</property>
</bean>
<bean id="re" class="com.net.controllers.WebConfig"/>
</beans>
WebConfig.java
==============
@Configuration
public class WebConfig extends WebMvcConfigurationSupport {
@Autowired
@Override
@Bean
public RequestMappingHandlerMapping requestMappingHandlerMapping() {
CustomRequestMappingHandlerMapping handlerMapping = new CustomRequestMappingHandlerMapping();
handlerMapping.setOrder(0);
handlerMapping.setInterceptors(getInterceptors());
return handlerMapping;
}
@Bean
@Override
public FormattingConversionService mvcConversionService() {
// Use the DefaultFormattingConversionService but do not register defaults
DefaultFormattingConversionService conversionService = new DefaultFormattingConversionService(false);
return conversionService;
}
}