因爲需要傳遞(對於每個請求)的參數的Controller
方法,所述清潔溶液是使用HandlerMethodArgumentResolver,使得 Spring容器可以動態地注入你ProjectVersion參數傳遞給控制器方法如下所示:
ProjectVersionArgumentResolver
類:
public class ProjectVersionArgumentResolver implements
HandlerMethodArgumentResolver {
@Override
public boolean supportsParameter(MethodParameter methodParameter) {
return methodParameter.getParameterType().equals(ProjectVersion.class);
}
@Override
public Object resolveArgument(MethodParameter methodParameter,
ModelAndViewContainer modelAndViewContainer,
NativeWebRequest nativeWebRequest,
WebDataBinderFactory webDataBinderFactory) throws Exception {
return ProjectVersion.fromString(nativeWebRequest.getParameter("version"));
}
}
春季啓動ApplicationLauncher類:
public class MyProjectApplicationLauncher extends WebMvcConfigurerAdapter {
@Override
public void addArgumentResolvers(List<HandlerMethodArgumentResolver>
argumentResolvers) {
//add the new resolver
argumentResolvers.add(new MyMethodArgumentResolver());
}
public static void main(String[] args) {
SpringApplication.run(MyProjectApplicationLauncher.class, args);
}
}
ProjectVersion
類:
public enum ProjectVersion {
//add your existing code
//Add fromString method to convert string to enum
public static ProjectVersion fromString(String input) {
for (ProjectVersion projectVersion : ProjectVersion.values()) {
if (projectVersion.version.equals(input)) {
return projectVersion;
}
}
return null;
}
}
是,自定義序列化。看到這個,正是你的情況:http://stackoverflow.com/questions/7766791/serializing-enums-with-jackson –
如果你通過'VERSION_1'它會工作,如果你通過內部價值它不會然後你會需要自己創建一個自定義轉換器。 –