GVKun编程网logo

设置Jackson功能WRITE_DATES_AS_TIMESTAMPS在Spring Boot中不起作用(jackson date format)

10

在本文中,我们将为您详细介绍设置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)

设置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=falsespring.jackson.serialization.write-dates-as-timestamps=false因为我发现很多地方都提到了这一点(即使Boot文档没有对此进行提示)。那这个呢?它们似乎是同义词-没有效果。
  • 在写这个问题时,SO建议WRITE_DATES_AS_TIMESTAMPS在Springboot1.3.5上不要唤醒。回答说,以取代WebMvcConfigurationSupportWebMvcConfigurerAdapter。虽然确实可以帮助我,但我不明白为什么会这样。

答案1

小编典典

Spring Boot将WebMvcConfigurationSupportBean 的存在指示为您要完全控制Spring
MVC的配置。通常,您最终会通过使用来获得这样的bean,@EnableWebMvc但是您也可以声明自己的bean或配置类,它是一个WebMvcConfigurationSupport

如果您继承WebMvcConfigurerAdapter而不是继承,那么您将对WebMvcConfigurationSupportSpring
Boot的Spring MVC自动配置进行附加更改,而不是完全接管。

Spring Boot的Spring
MVC自动配置的一部分是对其进行配置,以使用自动配置ObjectMapper的HTTP消息转换。如果关闭Spring
MVC的Boot的自动配置,它将使用它自己的,ObjectMapper不受任何spring.jackson.*配置设置影响的单独配置。

@Valid注释在Spring Boot中不起作用

@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上不起作用

如何解决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?

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/UuCl

Hive date/timestamp/date_sub/date_add/date_format/日期时间格式转换

Hive date/timestamp/date_sub/date_add/date_format/日期时间格式转换

-- Hive 只有date('2022-06-09')、TIMESTAMP('2022-07-15 02:21:32.000000') 2个日期类型,没有datetime类型。
  • select
  • now(),
  • current_timestamp(),
  • current_date(),
  • date_sub(current_date(), 2),
  • date_add(current_date(), 2),
  • date_add('2022-06-09', 2) as c2,
  • date_sub('2022-06-09 12:13:14', 2) as c3,
  • date_format('2022-06-09 12:13:14', 'dd-MM-yyyy') as c4,
  • date_format('2022-06-09 12:13:14', 'yyyy-MM-dd') as c5,
  • date_format('2022-06-09 12:13:14', 'yyyy-MM') as c6,
  • date_format('2022-06-09 12:13:14', 'MM') as c7,
  • date_format(current_date(), 'MM') as c8,
  • from_unixtime(1657851692, 'yyyy-MM-dd HH:mm:ss') as c9,
  • date_sub(
  • from_unixtime(1657851692, 'yyyy-MM-dd HH:mm:ss'),
  • 2
  • ) as c10,
  • to_date('2018-12-08 10:03:01'),
  • date_sub(to_date('2018-12-08 10:03:
  • 关于设置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/日期时间格式转换等相关内容,可以在本站寻找。

    本文标签:

    上一篇带有Spring Boot 2的Keycloak 4.0.0 Beta 2(spring boot actuator v2)

    下一篇使用Palantir Gradle插件构建Docker容器时找不到.jar文件