Spring 作为 pathvar 的 .com 的 406 状态代码问题

Spring 406 status code issue for .com as pathvar

我有以下控制器方法:

@RequestMapping(method = RequestMethod.GET, value = "/account/{loginId:.+}")
    public @ResponseBody CloudWebServiceResponse getLogin(@PathVariable(value = "loginId") String loginId) throws CloudWebServiceInvocationException {
        return internalService.getLogin(progressId);
    }

当将 loginId 作为 "abc.com" 传递时,它会给出 406 状态代码,否则它工作得很好。

我有以下 WebConfig 文件:

@Configuration
@Import(HibernateConfig.class)
@EnableWebMvc
// @EnableAsync()
// @EnableAspectJAutoProxy
@ComponentScan(basePackages = "com.azim.web.service.*",  basePackageClasses = { WebSecurityConfig.class }, excludeFilters = { @ComponentScan.Filter(Configuration.class) })
public class WebConfig extends WebMvcConfigurationSupport {

    @Override
    protected void configureContentNegotiation(ContentNegotiationConfigurer configurer) {
        configurer.favorPathExtension(false).favorParameter(true).parameterName("mediaType").ignoreAcceptHeader(true).useJaf(false).defaultContentType(MediaType.APPLICATION_JSON)
                .mediaType("xml", MediaType.APPLICATION_XML).mediaType("json", MediaType.APPLICATION_JSON).mediaType("html", MediaType.APPLICATION_JSON);
    }

    @Bean(name = "validator")
    public Validator validator() {
        return new LocalValidatorFactoryBean();
    }
}

它仅针对 .com 而非 .randomvalue 发送 406 状态代码。 我尝试添加 stackoverdflow 上其他线程建议的 jackson-core-asl 和 jackson-databind-asl jar,但对我没有任何作用。 请帮忙解决这个问题。

终于找到解决办法了。

与其扩展到 WebMvcConfigurationSupport class,不如扩展到 WebMvcConfigurerAdapter。那么代码就变成了:

@配置

@Import(HibernateConfig.class)
@EnableWebMvc
// @EnableAsync()
// @EnableAspectJAutoProxy
@ComponentScan(basePackages = "com.azim.web.service.*",  basePackageClasses = { WebSecurityConfig.class }, excludeFilters = { @ComponentScan.Filter(Configuration.class) })
public class WebConfig extends WebMvcConfigurerAdapter {

    @Override
    protected void configureContentNegotiation(ContentNegotiationConfigurer configurer) {
        configurer.favorPathExtension(false).favorParameter(true).parameterName("mediaType").ignoreAcceptHeader(true).useJaf(false).defaultContentType(MediaType.APPLICATION_JSON)
                .mediaType("xml", MediaType.APPLICATION_XML).mediaType("json", MediaType.APPLICATION_JSON).mediaType("html", MediaType.APPLICATION_JSON);
    }

    @Bean(name = "validator")
    public Validator validator() {
        return new LocalValidatorFactoryBean();
    }
}