Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/381.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java ModelMapper和LocalDate-Spring启动_Java_Json_Spring Boot_Mapping_Modelmapper - Fatal编程技术网

Java ModelMapper和LocalDate-Spring启动

Java ModelMapper和LocalDate-Spring启动,java,json,spring-boot,mapping,modelmapper,Java,Json,Spring Boot,Mapping,Modelmapper,目前,我正在尝试将dto映射到一个也包含LocalDate属性的类。只是我在这里没有成功,本地日期字段始终保持为空。因此,我构建了一个简短的示例,其中我遵循了 我有一个ModelMapper类,如下所示: @Bean public ModelMapper createMapper() { ModelMapper modelMapper = new ModelMapper(); modelMapper.createTypeMap(String.class, LocalDat

目前,我正在尝试将dto映射到一个也包含LocalDate属性的类。只是我在这里没有成功,本地日期字段始终保持为空。因此,我构建了一个简短的示例,其中我遵循了 我有一个ModelMapper类,如下所示:

    @Bean
public ModelMapper createMapper() {
    ModelMapper modelMapper = new ModelMapper();
    modelMapper.createTypeMap(String.class, LocalDate.class);
    Provider<LocalDate> localDateProvider = new AbstractProvider<LocalDate>() {
        @Override
        public LocalDate get() {
            return LocalDate.now();
        }
    };

    Converter<String, LocalDate> toStringDate = new AbstractConverter<String, LocalDate>() {
        @Override
        protected LocalDate convert(String source) {
            DateTimeFormatter format = DateTimeFormatter.ofPattern("yyyy-MM-dd");
            LocalDate localDate = LocalDate.parse(source, format);
            return localDate;
        }
    };

    modelMapper.getTypeMap(String.class, LocalDate.class).setProvider(localDateProvider);
    modelMapper.addConverter(toStringDate);
    return modelMapper;
}
我创建了一个测试类,在这个类中,我试图通过LinkedHashMap模拟json部分,因为它出现在我实现的web服务中:

@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = WebEnvironment.NONE)
public class ModelMapperTest {

@Autowired
ModelMapper mapper;

String jsonLd = "2018-06-11";

LinkedHashMap<String, String> lhm;

@Before
public void init() {
    lhm = new LinkedHashMap<>();
    lhm.put("id", "1");
    lhm.put("ld", jsonLd);
}

@Test
public void checkModelMapper() {
    assertNotNull(mapper);
    Collection<TypeMap<?, ?>> c = mapper.getTypeMaps();
    assertNotNull(c);
    for (TypeMap<?, ?> typeMap : c) {
        System.out.println("TypeMap : " + typeMap.getConverter().toString());
    }
}

@Test
public void testLocalDate() {
    LocalDate ld = mapper.map(jsonLd, LocalDate.class);
    assertNotNull(ld);
    assertEquals(ld, LocalDate.parse(jsonLd, 
DateTimeFormatter.ofPattern("yyyy-MM-dd")));
}

@Test
public void testLocalDateInObject() {
    JsonLocalDate jld = mapper.map(jsonLd, JsonLocalDate.class);
    assertNotNull(jld);
    assertEquals(jld.getLd(), LocalDate.parse(jsonLd, 
DateTimeFormatter.ofPattern("yyyy-MM-dd")));
}

}
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment=webEnvironment.NONE)
公共类ModelMapperTest{
@自动连线
模型映射器映射器;
字符串jsonLd=“2018-06-11”;
LinkedHashMap lhm;
@以前
公共void init(){
lhm=新LinkedHashMap();
lhm.put(“id”、“1”);
lhm.put(“ld”,jsonLd);
}
@试验
公共void checkModelMapper(){
assertNotNull(映射器);

Collection测试失败是因为您试图将字符串映射到对象,而ModelMapper不知道如何将字符串映射到对象

所以你应该试试


也许这有助于我现在看到我的想法是错误的。我假设我定义了一次字符串如何映射到LocalDate,然后每当LocalDate字段名与节点键匹配时都会使用它。谢谢!
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = WebEnvironment.NONE)
public class ModelMapperTest {

@Autowired
ModelMapper mapper;

String jsonLd = "2018-06-11";

LinkedHashMap<String, String> lhm;

@Before
public void init() {
    lhm = new LinkedHashMap<>();
    lhm.put("id", "1");
    lhm.put("ld", jsonLd);
}

@Test
public void checkModelMapper() {
    assertNotNull(mapper);
    Collection<TypeMap<?, ?>> c = mapper.getTypeMaps();
    assertNotNull(c);
    for (TypeMap<?, ?> typeMap : c) {
        System.out.println("TypeMap : " + typeMap.getConverter().toString());
    }
}

@Test
public void testLocalDate() {
    LocalDate ld = mapper.map(jsonLd, LocalDate.class);
    assertNotNull(ld);
    assertEquals(ld, LocalDate.parse(jsonLd, 
DateTimeFormatter.ofPattern("yyyy-MM-dd")));
}

@Test
public void testLocalDateInObject() {
    JsonLocalDate jld = mapper.map(jsonLd, JsonLocalDate.class);
    assertNotNull(jld);
    assertEquals(jld.getLd(), LocalDate.parse(jsonLd, 
DateTimeFormatter.ofPattern("yyyy-MM-dd")));
}

}
modelMapper.typeMap(String.class, JsonLocalDate.class)
    .addMapping(src -> src, JsonLocalDate::setLd);