【问题标题】:Spring 406 status code issue for .com as pathvar.com 作为 pathvar 的 Spring 406 状态代码问题
【发布时间】:2016-04-28 11:05:31
【问题描述】:

我有以下控制器方法:

@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,但对我没有任何作用。 请帮忙解决这个问题。

【问题讨论】:

  • 您是否尝试记录该值?它来得正确吗?
  • 是的,它来了。在返回响应时,这个问题正在发生。

标签: java spring http-status-code-406


【解决方案1】:

终于,我得到了解决方案。

应该扩展为 WebMvcConfigurerAdapter,而不是扩展为 WebMvcConfigurationSupport 类。那么代码就变成了:

@配置

@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();
    }
}

【讨论】:

    猜你喜欢
    • 2011-06-26
    • 1970-01-01
    • 2020-11-27
    • 1970-01-01
    • 2023-03-17
    • 1970-01-01
    • 2014-02-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多