0

我有以下控制器方法:

@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 发送 406 状态代码,而不为 .randomvalue 发送。我尝试在 stackoverdflow 上添加其他线程建议的 jackson-core-asl 和 jackson-databind-asl jar,但对我没有任何作用。请帮忙解决这个问题。

4

1 回答 1

0

最后,我得到了解决方案。

而不是扩展到WebMvcConfigurationSupport类,它应该扩展到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();
    }
}
于 2016-04-28T18:38:26.823 回答