我遇到了从Spring Mvc 4 Xml迁移到JavaConfig的问题。
我有一个完美的转换
的xml配置for (var i = 1; i < 100; i++) {
if ($(".child:nth-child(" + i + ")").length) {
}
else {
alert(i-1);
break;
}
}
Jackson2ObjectMapperFlexmaint
<bean id="objectMapper" class="com.xetec.flexmaint.common.Jackson2ObjectMapperFlexmaint" />
<mvc:annotation-driven validator="validator" conversion-service="conversionService">
<mvc:message-converters>
<bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
<property name="objectMapper" ref="objectMapper"/>
</bean>
</mvc:message-converters>
</mvc:annotation-driven>
<bean id="conversionService"
class="org.springframework.context.support.ConversionServiceFactoryBean">
<property name="converters">
<list>
<bean class="com.xetec.flexmaint.common.JodaLocalTimeConverter"/>
<bean class="com.xetec.flexmaint.common.JavaUtilDateConverter"/>
<bean class="com.xetec.flexmaint.common.JodaDateTimeConverter"/>
<bean class="com.xetec.flexmaint.common.JodaLocalDateConverter"/>
<bean class="com.xetec.flexmaint.common.JodaLocalDateTimeConverter"/>
</list>
</property>
</bean>
现在我正在尝试从Xml Config转换为Javaconfig / annotation。
public class Jackson2ObjectMapperFlexmaint extends Jackson2ObjectMapperFactoryBean {
@Override
public void afterPropertiesSet() {
super.afterPropertiesSet();
getObject().setSerializationInclusion(JsonInclude.Include.NON_NULL).setSerializationInclusion(JsonInclude.Include.NON_EMPTY);
Hibernate5Module hibernateModule = new Hibernate5Module();
hibernateModule.disable(Feature.USE_TRANSIENT_ANNOTATION);
hibernateModule.enable(Feature.FORCE_LAZY_LOADING);
getObject().registerModules(new JavaTimeModule(), hibernateModule);
getObject().configure(SerializationFeature.INDENT_OUTPUT, true);
getObject().configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
getObject().setDateFormat(new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss"));
}
}
如果我发送json属性(“scheduledStart”:“2015-08-21T16:07:30”),则使用命令行我得到 HTTP状态400 - 由客户端在语法上是不正确的。
@EnableWebMvc
public class WebMvcConfig extends WebMvcConfigurerAdapter {
@Override
public void addFormatters(FormatterRegistry registry) {
registry.addConverter(new LocalTimeConverter());
registry.addConverter(new JavaUtilDateConverter());
registry.addConverter(new ZonedDateTimeConverter());
registry.addConverter(new LocalDateConverter());
registry.addConverter(new LocalDateTimeConverter());
registry.addConverter(new StringToZonedDateTimeConverter());
}
}
@Bean(name = "validatorBeanFactory")
public HibernateValidatorDefaultMessagesBeanFactory validator() {
return new HibernateValidatorDefaultMessagesBeanFactory();
}
Field sheduledStart
curl -H "Content-Type: application/json" -X POST -d '
{
"title": "New JSON",
"scheduledStart":"2015-08-21T16:07:30",
"assetId": "2",
"workOrderStatus": {
"workOrderStatusID": "1"
},
"partDtoList": [
{
"barcode": "1440169305037",
"quantity": "1"
}
]
}
' -b ~/Downloads/cookies2.txt http://localhost:8080/flexmaintwebservice/api/workOrder/formCreate
如果我使用Xml Config尝试相同的命令行,为什么?
谢谢!
更新
我的整个Java Config类
@Type(type="org.hibernate.type.ZonedDateTimeType")
private ZonedDateTime scheduledStart;
ADD控制器映射
@Configuration
@PropertySource({
"classpath:webServiceConfig.properties",
"classpath:config.properties"
})
@ComponentScan({
"com.xetec.flexmaint.webservice",
"com.xetec.flexmaint.controller.validator",
})
@EnableWebMvc
public class WebMvcConfig extends WebMvcConfigurerAdapter {
@Value("${apiUrl}")
private String basePath;
@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
converters.add(mappingJackson2HttpMessageConverter());
super.configureMessageConverters(converters);
}
@Override
public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
configurer.enable();
}
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/static/**").addResourceLocations("/resources/");
}
@Override
public void addInterceptors(InterceptorRegistry registry) {
LocaleChangeInterceptor interceptor = new LocaleChangeInterceptor();
interceptor.setParamName("lang");
registry.addInterceptor(interceptor);
}
@Override
public void addFormatters(FormatterRegistry registry) {
registry.addConverter(new LocalTimeConverter());
registry.addConverter(new JavaUtilDateConverter());
registry.addConverter(new ZonedDateTimeConverter());
registry.addConverter(new LocalDateConverter());
registry.addConverter(new LocalDateTimeConverter());
registry.addConverter(new StringToZonedDateTimeConverter());
}
@Bean
public JSONDocController documentationController() {
List<String> packages = Arrays.asList("com.xetec.flexmaint.webservice", "com.xetec.flexmaint.common.helper", "com.xetec.flexmaint.entity.entity");
JSONDocController jsonDocController = new JSONDocController("1.0", basePath, packages);
jsonDocController.setPlaygroundEnabled(true);
jsonDocController.setDisplayMethodAs(MethodDisplay.URI);
return jsonDocController;
}
@Bean
public static PropertySourcesPlaceholderConfigurer propertyConfigInDev() {
return new PropertySourcesPlaceholderConfigurer();
}
@Bean
public CookieLocaleResolver localeResolver() {
CookieLocaleResolver cookieLocaleResolver = new CookieLocaleResolver();
cookieLocaleResolver.setDefaultLocale(Locale.getDefault());
return cookieLocaleResolver;
}
@Bean(name = "validatorBeanFactory")
public HibernateValidatorDefaultMessagesBeanFactory validator() {
return new HibernateValidatorDefaultMessagesBeanFactory();
}
@Bean
public BeanNameUrlHandlerMapping beanNameUrlHandlerMapping() {
return new BeanNameUrlHandlerMapping();
}
@Bean
public CommonsMultipartResolver multipartResolver() {
return new CommonsMultipartResolver();
}
@Bean
public RequestMappingHandlerMapping requestMappingHandlerMapping() {
return new RequestMappingHandlerMapping();
}
@Bean
public MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter() {
return new MappingJackson2HttpMessageConverter(jackson2ObjectMapperFlexmaint().getObject());
}
@Bean
public Jackson2ObjectMapperFlexmaint jackson2ObjectMapperFlexmaint() {
return new Jackson2ObjectMapperFlexmaint();
}
@Bean
public TilesViewResolver tilesViewResolver() {
TilesViewResolver url = new TilesViewResolver();
url.setOrder(1);
return url;
}
@Bean
public InternalResourceViewResolver internalResourceViewResolver() {
InternalResourceViewResolver url = new InternalResourceViewResolver();
url.setViewClass(JstlView.class);
url.setPrefix("/WEB-INF/tiles/");
url.setSuffix(".jsp");
url.setOrder(2);
return url;
}
//TODO How to migrate this?
/**
* <wss:binding url="/asset">
<wss:service>
<ws:service bean="#assetControllerWs" />
</wss:service>
</wss:binding>
<wss:binding url="/barcodeScannerWs">
<wss:service>
<ws:service bean="#barcodeScannerWs" />
</wss:service>
</wss:binding>
<wss:binding url="/maintenanceWebServiceWs">
<wss:service>
<ws:service bean="#maintenanceWebServiceWs" />
</wss:service>
</wss:binding>
*/
}
添加堆栈跟踪
@Api(name = "work order services", description = "Methods for managing work orders")
@Controller
@RequestMapping("/api/workOrder")
public class WorkOrderRestFulApiController extends AbstractBaseController {
@ApiMethod(description = "Api for create work order")
@RequestMapping(value = "/formCreate", method = RequestMethod.POST, consumes="application/json;charset=utf-8", produces="application/json;charset=utf-8")
@ApiResponseObject @ResponseBody
public ResponseEntity<Object> createWorkOrder(@ApiPathParam @RequestBody WorkOrderDto workOrderDto, BindingResult result) throws Exception {
...
}
}