WebMvcConfigurationSupport 在 SpringBoot 中产生错误
WebMvcConfigurationSupport produce errors in SpringBoot
我正在对其余部分进行版本控制 api。我在这里找到了一个很好的方法:
@RestController
@RequestMapping("/user")
@VersionedResource(media = Version.MEDIA)
public class UserController {
@Autowired
private UserService userService;
@GetMapping("")
@VersionedResource(from = "1.0", to = "1.9")
public String getUser() {
return "user v1";
}
@GetMapping("")
@VersionedResource(from = "2.0")
public String getUserV2() {
return "user v2";
}
效果很好,但要做到这一点需要 class 扩展 WebMvcConfigurationSupport:
@Configuration
public class WebConfiguration extends WebMvcConfigurationSupport {
@Autowired
private ContentNegotiationManager contentNegotiationManager;
@Bean
public RequestMappingHandlerMapping requestMappingHandlerMapping() {
VersionRequestMappingHandlerMapping handlerMapping = new VersionRequestMappingHandlerMapping();
handlerMapping.setOrder(0);
handlerMapping.setRemoveSemicolonContent(false);
handlerMapping.setContentNegotiationManager(contentNegotiationManager);
return handlerMapping;
}
}
这会产生许多奇怪的错误,例如新的 LazyInitializationException、更改日期格式、带有 uri 参数的错误...即使我将此 class 留空,它也会产生错误并且版本控制将被禁用。当我评论此 extends WebMvcConfigurationSupport 时,一切都再次正常运行,当然,除了版本控制被禁用。所以我确定错误来自这里。
那么问题来了,为什么WebMvcConfigurationSupport会报错,我有哪些解决办法呢?谢谢
解决方案是使用 WebMvcRegistrations
而不是 WebMvcConfigurationSupport
,因为它会禁用自动配置。
@Configuration
public class WebConfiguration implements WebMvcRegistrations {
@Autowired
private ContentNegotiationManager contentNegotiationManager;
@Override
public RequestMappingHandlerMapping getRequestMappingHandlerMapping() {
VersionRequestMappingHandlerMapping handlerMapping = new VersionRequestMappingHandlerMapping();
handlerMapping.setOrder(0);
handlerMapping.setRemoveSemicolonContent(false);
handlerMapping.setContentNegotiationManager(contentNegotiationManager);
return handlerMapping;
}
}
我正在对其余部分进行版本控制 api。我在这里找到了一个很好的方法:
@RestController
@RequestMapping("/user")
@VersionedResource(media = Version.MEDIA)
public class UserController {
@Autowired
private UserService userService;
@GetMapping("")
@VersionedResource(from = "1.0", to = "1.9")
public String getUser() {
return "user v1";
}
@GetMapping("")
@VersionedResource(from = "2.0")
public String getUserV2() {
return "user v2";
}
效果很好,但要做到这一点需要 class 扩展 WebMvcConfigurationSupport:
@Configuration
public class WebConfiguration extends WebMvcConfigurationSupport {
@Autowired
private ContentNegotiationManager contentNegotiationManager;
@Bean
public RequestMappingHandlerMapping requestMappingHandlerMapping() {
VersionRequestMappingHandlerMapping handlerMapping = new VersionRequestMappingHandlerMapping();
handlerMapping.setOrder(0);
handlerMapping.setRemoveSemicolonContent(false);
handlerMapping.setContentNegotiationManager(contentNegotiationManager);
return handlerMapping;
}
}
这会产生许多奇怪的错误,例如新的 LazyInitializationException、更改日期格式、带有 uri 参数的错误...即使我将此 class 留空,它也会产生错误并且版本控制将被禁用。当我评论此 extends WebMvcConfigurationSupport 时,一切都再次正常运行,当然,除了版本控制被禁用。所以我确定错误来自这里。
那么问题来了,为什么WebMvcConfigurationSupport会报错,我有哪些解决办法呢?谢谢
解决方案是使用 WebMvcRegistrations
而不是 WebMvcConfigurationSupport
,因为它会禁用自动配置。
@Configuration
public class WebConfiguration implements WebMvcRegistrations {
@Autowired
private ContentNegotiationManager contentNegotiationManager;
@Override
public RequestMappingHandlerMapping getRequestMappingHandlerMapping() {
VersionRequestMappingHandlerMapping handlerMapping = new VersionRequestMappingHandlerMapping();
handlerMapping.setOrder(0);
handlerMapping.setRemoveSemicolonContent(false);
handlerMapping.setContentNegotiationManager(contentNegotiationManager);
return handlerMapping;
}
}