在本文中,我们将为您详细介绍设置Jackson功能WRITE_DATES_AS_TIMESTAMPS在SpringBoot中不起作用的相关知识,并且为您解答关于jacksondateformat的疑问
在本文中,我们将为您详细介绍设置Jackson功能WRITE_DATES_AS_TIMESTAMPS在Spring Boot中不起作用的相关知识,并且为您解答关于jackson date format的疑问,此外,我们还会提供一些关于@Valid注释在Spring Boot中不起作用、DataSourceInitializer在Spring Boot 1.2上不起作用、Does accepts_nested_attributes_for work with belongs_to?、Hive date/timestamp/date_sub/date_add/date_format/日期时间格式转换的有用信息。
本文目录一览:- 设置Jackson功能WRITE_DATES_AS_TIMESTAMPS在Spring Boot中不起作用(jackson date format)
- @Valid注释在Spring Boot中不起作用
- DataSourceInitializer在Spring Boot 1.2上不起作用
- Does accepts_nested_attributes_for work with belongs_to?
- Hive date/timestamp/date_sub/date_add/date_format/日期时间格式转换
设置Jackson功能WRITE_DATES_AS_TIMESTAMPS在Spring Boot中不起作用(jackson date format)
我将spring.jackson.serialization.WRITE_DATES_AS_TIMESTAMPS=false
在Spring Boot配置,但杰克逊仍然串行产生[1942,4,2]
,而不是"1942-04-02"
一个DateTime
值。
一些调试快照
- 在
org.springframework.boot.autoconfigure.jackson.JacksonAutoConfiguration.Jackson2ObjectMapperBuilderCustomizerConfiguration.StandardJackson2ObjectMapperBuilderCustomizer#customize
里面
configureFeatures(builder, this.jacksonProperties.getSerialization());
显示“ WRITE_DATES_AS_TIMESTAMPS”->“假”
- 然后在
org.springframework.http.converter.json.Jackson2ObjectMapperBuilder#configure
这个循环中
for (Object feature : this.features.keySet()) {configureFeature(objectMapper, feature, this.features.get(feature)); }
并再次this.features
说“ WRITE_DATES_AS_TIMESTAMPS”->“假”
- 但是在a的序列化过程中
DateTime
com.fasterxml.jackson.datatype.jsr310.ser.JSR310FormattedSerializerBase#useTimestamp
说false是因为provider.isEnabled(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS)
返回false。
尝试修复
- 绝望的是,我替换了它
spring.jackson.serialization.WRITE_DATES_AS_TIMESTAMPS=false
,spring.jackson.serialization.write-dates-as-timestamps=false
因为我发现很多地方都提到了这一点(即使Boot文档没有对此进行提示)。那这个呢?它们似乎是同义词-没有效果。 - 在写这个问题时,SO建议WRITE_DATES_AS_TIMESTAMPS在Springboot1.3.5上不要唤醒。回答说,以取代
WebMvcConfigurationSupport
用WebMvcConfigurerAdapter
。虽然确实可以帮助我,但我不明白为什么会这样。
答案1
小编典典Spring Boot将WebMvcConfigurationSupport
Bean 的存在指示为您要完全控制Spring
MVC的配置。通常,您最终会通过使用来获得这样的bean,@EnableWebMvc
但是您也可以声明自己的bean或配置类,它是一个WebMvcConfigurationSupport
。
如果您继承WebMvcConfigurerAdapter
而不是继承,那么您将对WebMvcConfigurationSupport
Spring
Boot的Spring MVC自动配置进行附加更改,而不是完全接管。
Spring Boot的Spring
MVC自动配置的一部分是对其进行配置,以使用自动配置ObjectMapper
的HTTP消息转换。如果关闭Spring
MVC的Boot的自动配置,它将使用它自己的,ObjectMapper
不受任何spring.jackson.*
配置设置影响的单独配置。
@Valid注释在Spring Boot中不起作用
这是方案,带有注释的控制器@RestController
和需要验证PUT
其@RequestBody
参数的方法。我@Valid
在参数上使用了注释@NotNull
,@Min
在bean字段上使用了注释,但是它们不起作用。
代码在这里:
豆:
public class PurchaseWrapper { @DecimalMin(value = "0.00",message = "discount must be positive") @NotNull private BigDecimal discount; @NotNull private Long merchandiseId; @NotNull private Long addressId; @Min(1) @NotNull private Integer count;}
控制器
@RestController@RequestMapping("merchandises")public class MerchandiseController {@RequestMapping(value = "purchase",method = RequestMethod.PUT)public ResponseEntity<RestEntity> purchase(@Valid @Validated @RequestBody PurchaseWrapper purchaseWrapper, @RequestParam String token){ return new ResponseEntity<>(merchandiseService.purchase(purchaseWrapper,token),HttpStatus.OK);}@AutowiredPurchaseWrapperValidator purchaseWrapperValidator;@InitBinder(value = "purchaseWrapper")protected void initBinder(WebDataBinder binder) { binder.setValidator(purchaseWrapperValidator);}}
pom文件:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <dependency> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-validator</artifactId> </dependency>
我不知道这是怎么回事…而且我猜这是我在同一个参数上使用@Valid
和@Validated
注释都存在的问题。但是,即使我省略了@Validated
注释,@Valid
它仍然无法正常工作…
有任何想法吗?
答案1
小编典典我想通了…这是因为PurchaseWrapperValidator
实现的功能org.springframework.validation.Validator
会覆盖默认javax.validation.*
注释。
DataSourceInitializer在Spring Boot 1.2上不起作用
如何解决DataSourceInitializer在Spring Boot 1.2上不起作用?
您可以利用Spring Boot数据库初始化功能。最简单的方法是将“ data.sql”文件放在类路径的根目录中。因此,您只需要:
- 将您的sql文件名更改为“ data.sql”。
- 将其放在“ src / main / resources”中。
Spring Boot将自动选择文件,并在启动时使用它来初始化数据库。
如果需要自定义文件名,位置等,可以查看文档。
解决方法
我是Spring Boot的新手,我想在数据库创建时像种子数据一样添加一些sql。
@Value("classpath:com/foo/sql/db-test-data.sql")
private Resource dataScript;
@Bean
public DataSourceInitializer dataSourceInitializer(final DataSource dataSource) {
final DataSourceInitializer initializer = new DataSourceInitializer();
initializer.setDataSource(dataSource);
initializer.setDatabasePopulator(databasePopulator());
return initializer;
}
private DatabasePopulator databasePopulator() {
final ResourceDatabasePopulator populator = new ResourceDatabasePopulator();
populator.addScript(dataScript);
return populator;
}
props.put("hibernate.query.substitutions","true 1,false 0");
props.put("hibernate.hbm2ddl.auto","create-drop");
props.put("hibernate.show_sql","false");
props.put("hibernate.format_sql","true");
我已经执行了此操作,但是在Spring Boot上不起作用,任何人都可以帮助我。
Does accepts_nested_attributes_for work with belongs_to?
问题:
I have been getting all kinds of conflicting information regarding this basic question, and the answer is pretty crucial to my current problems. 关于这个基本问题,我得到了各种各样相互矛盾的信息,而答案对于我当前的问题至关重要。 So, very simply, in Rails 3, is it allowed or not allowed to use accepts_nested_attributes_for with a belongs_to relationship? 因此,很简单,在Rails 3中,是否允许将带有accepts_to关系的accepts_nested_attributes_for使用?
class User < ActiveRecord::Base
belongs_to :organization
accepts_nested_attributes_for :organization
end
class Organization < ActiveRecord::Base
has_many :users
end
In a view: 在视图中:
= form_for @user do |f|
f.label :name, "Name"
f.input :name
= f.fields_for :organization do |o|
o.label :city, "City"
o.input :city
f.submit "Submit"
解决方案:
参考: https://stackoom.com/en/question/UuClHive date/timestamp/date_sub/date_add/date_format/日期时间格式转换