Java 如何从REST控制器端点返回LocalDateTime?
我正在用Spring-java编写一个REST控制器,我的一个端点应该返回一个Java 如何从REST控制器端点返回LocalDateTime?,java,spring,rest,spring-mvc,controller,Java,Spring,Rest,Spring Mvc,Controller,我正在用Spring-java编写一个REST控制器,我的一个端点应该返回一个LocalDateTime变量。 @GetMapping(path = "/lastDateTimeOfReset") LocalDateTime fetchLastDateTimeOfReset() { return mongoManagerService.fetchLastDateTimeOfReset(); } 这是客户端请求: ResponseEntity<LocalDateTime> la
LocalDateTime
变量。
@GetMapping(path = "/lastDateTimeOfReset")
LocalDateTime fetchLastDateTimeOfReset() {
return mongoManagerService.fetchLastDateTimeOfReset();
}
这是客户端请求:
ResponseEntity<LocalDateTime> lastDateEntity = restTemplate.getForEntity(url, LocalDateTime.class);
我在调试模式下检查了返回值,它是一个有效的LocalDateTime。
为什么会引发此异常以及如何克服它?您可以使用字符串解决此问题
@GetMapping(path = "/lastDateTimeOfReset")
String fetchLastDateTimeOfReset() {
return (mongoManagerService.fetchLastDateTimeOfReset()).toString();
}
客户请求:
ResponseEntity<String> lastDateEntity = restTemplate.getForEntity(url, String.class);
ResponseEntity lastDateEntity=restTemplate.getForEntity(url,String.class);
您需要检查MongoManager服务的返回类型
或者使用以下示例代码测试您的应用程序
服务
@GetMapping(path = "/lastDateTimeOfReset")
LocalDateTime fetchLastDateTimeOfReset() {
return LocalDateTime.now();
}
客户端
RestTemplate rs = new RestTemplate();
ResponseEntity<LocalDateTime> lastDateEntity = rs.getForEntity(new URI("http://localhost:8089/lastDateTimeOfReset"), LocalDateTime.class);
System.out.println(lastDateEntity);
restemplate rs=new restemplate();
ResponseEntity lastDateEntity=rs.getForEntity(新URI(“http://localhost:8089/lastDateTimeOfReset),LocalDateTime.class);
System.out.println(lastDateEntity);
输出
RestTemplate rs = new RestTemplate();
ResponseEntity<LocalDateTime> lastDateEntity = rs.getForEntity(new URI("http://localhost:8089/lastDateTimeOfReset"), LocalDateTime.class);
System.out.println(lastDateEntity);
问题解决了!
一般来说-我应该将此配置添加到我的应用程序中:
@Configuration
public class MvcConfig {
@Configuration
@EnableWebMvc
public class MessageConvertersConfiguration implements WebMvcConfigurer {
@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
converters.add(new MappingJackson2HttpMessageConverter(objectMapper()));
}
@Bean
public ObjectMapper objectMapper() {
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("dd-MM-yyyy'T'HH:mm:ss");
SimpleModule localDateModule = new SimpleModule();
localDateModule.addDeserializer(LocalDateTime.class, new LocalDateTimeDeserializer(dateTimeFormatter));
localDateModule.addSerializer(LocalDateTime.class, new LocalDateTimeSerializer(dateTimeFormatter));
ObjectMapper mapper = new ObjectMapper();
mapper.registerModules(localDateModule);
return mapper;
}
}
}
你能试试下面的客户电话吗。ResponseEntity lastDateEntity=restTemplate.getForEntity(url,String.class);作为最后的手段,我会这么做,但我想避免什么例外?就像我贴的一样。问题是,当我使用邮递员时,它工作得很好。所以我假设我的spring测试中的某些东西配置不正确。您是如何创建RestTemplate对象的。验证是否有任何bean为其定义以及配置将getForEntity替换为getForObject
restTemplate.setMessageConverters(Collections.singletonList(new MappingJackson2HttpMessageConverter(objectMapper())));