gpt4 book ai didi

java - Spring rest xml 日期时间格式

转载 作者:行者123 更新时间:2023-12-05 03:08:54 26 4
gpt4 key购买 nike

我们将 SpringBoot 与 Spring Rest 和 Jackson 结合使用。我们使用 Java 8 LocalDateTime

休息 Controller 。

@RestController
@RequestMapping(method = RequestMethod.GET, produces = {MediaType.APPLICATION_XML_VALUE, MediaType.APPLICATION_JSON_VALUE})
public class SimpleRestController {

@Autowired
private RestService restService;

@RequestMapping("/api/{id}")
public ResponseEntity<RestObject> getModel(@PathVariable Long id) {
RestObject restObject = restService.getModel(id);
HttpStatus httpStatus = HttpStatus.OK;

if (restObject == null) {
httpStatus = HttpStatus.NO_CONTENT;
}

return new ResponseEntity<>(restObject, httpStatus);
}
}

RestObject 由 Controller 返回。

import javax.xml.bind.annotation.XmlRootElement;
import java.io.Serializable;
import java.time.LocalDateTime;

@XmlRootElement
public class RestObject implements Serializable {

private LocalDateTime timestamp;
private String title;
private String fullText;
private Long id;
private Double value;

@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
public LocalDateTime getTimestamp() {
return timestamp;
}

//Other getters and setters.
}

当我发送带有 Accept=application/json header 的 GET 请求时,它运行良好。这是回应。

{
"timestamp": "2017-06-09 15:58:32",
"title": "Rest object",
"fullText": "This is the full text. ID: 10",
"id": 10,
"value": 0.22816149915219197
}

但是Accept=application/xml:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<restObject>
<fullText>This is the full text. ID: 10</fullText>
<id>10</id>
<timestamp/>
<title>Rest object</title>
<value>0.15697306201038086</value>
</restObject>

时间戳字段为空。如何让它发挥作用?

最佳答案

解决方案来了!

这是响应

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<restObject>
<fullText>This is the full text. ID: 10</fullText>
<id>10</id>
<timestamp>2017-06-09 16:31:01</timestamp>
<title>Rest object</title>
<value>0.0021564103099468435</value>
</restObject>

1) 添加类DateTimeAdapter

import javax.xml.bind.annotation.adapters.XmlAdapter;
import java.time.LocalDateTime;

public class DateTimeAdapter extends XmlAdapter<String, LocalDateTime> {

public static final String DATE_FORMAT = "yyyy-MM-dd HH:mm:ss";
public static final DateTimeFormatter DATE_TIME_FORMATTER = DateTimeFormatter.ofPattern(DATE_FORMAT);

public LocalDateTime unmarshal(String v) throws Exception {
return LocalDateTime.parse(v, DATE_TIME_FORMATTER);
}

public String marshal(LocalDateTime v) throws Exception {
return DATE_TIME_FORMATTER.format(v);
}
}

2) 更新RestObject 类。在 LocalDateTime 字段上添加 @XmlJavaTypeAdapter(DateTimeAdapter.class) 注释。

import com.ca.training.rest.server.config.DateTimeAdapter;
import com.fasterxml.jackson.annotation.JsonFormat;

import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
import java.io.Serializable;
import java.time.LocalDateTime;

import static com.ca.training.rest.server.config.DateTimeAdapter.DATE_FORMAT;

@XmlRootElement
public class RestObject implements Serializable {

private LocalDateTime timestamp;
private String title;
private String fullText;
private Long id;
private Double value;

@XmlJavaTypeAdapter(DateTimeAdapter.class)
@JsonFormat(pattern = DATE_FORMAT)
public LocalDateTime getTimestamp() {
return timestamp;
}

//Other getters and setters.
}

我的想法来自这里 http://blog.bdoughan.com/2011/05/jaxb-and-joda-time-dates-and-times.html和这里 JAXB: Isn't it possible to use an XmlAdapter without @XmlJavaTypeAdapter?

关于java - Spring rest xml 日期时间格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44459786/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com