加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

java – 当参数以(.pl)结尾时,为什么Spring MVC @RequestMappin

发布时间:2020-12-15 01:35:15 所属栏目:大数据 来源:网络整理
导读:@RequestMapping(value = "/user/{username:.+}",method = RequestMethod.GET,produces = "application/json")@ResponseBodyUser user(@PathVariable String username) { User user = userRepository.findByUsername(username); if (user == null) throw new

@RequestMapping(value = "/user/{username:.+}",method = RequestMethod.GET,produces = "application/json")
@ResponseBody
User user(@PathVariable String username) {
    User user = userRepository.findByUsername(username);
    if (user == null)
        throw new UserNotFoundException("User not found");

    return user;
}

这是表示该动作的方法. Controller使用@RestController进行注释

解决了

应覆盖内容类型协商机制.

Explonation:http://spring.io/blog/2013/05/11/content-negotiation-using-spring-mvc

码:

@Override
  public void configureContentNegotiation(ContentNegotiationConfigurer configurer) {
    configurer.mediaType("pl",MediaType.APPLICATION_JSON);
  }
最佳答案
更新的答案

PathMatchConfigurer尝试将每个/controller/path.*与每个后缀匹配,尝试使用ContentNegotiationManager查找内容类型.您可以通过禁用此功能或仅在.*是已注册的后缀时尝试更改此行为.见:http://docs.spring.io/spring/docs/current/spring-framework-reference/html/mvc.html#mvc-config-path-matching

您应该调用pathMatchConfigurer.setUseRegisteredSuffixPatternMatch(true)或pathMatchConfigurer. setUseSuffixPatternMatch(假)

老答案:)

我认为Spring MVC错误地假设.pl是一个扩展,并为这种媒体类型查找HTTPMessageConverter.在这种情况下创建一个转换器是没有意义的,但是将它标记为不同的媒体类型会起作用吗?我认为这只是一种解决方法.

我还认为您的@RequestMapping值可能很简单:value =“/ user / {username}” – 您正在使用RegEx.对于您的用户名变量,这实际上意味着您仍然匹配整个模式.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读