[Java] "T" is included in date type JSON in API response

When creating a Web API in Java, the date type such as LocalDateTime type is displayed in the response.

2019-01-01T01:00:00

Solution if it becomes like

Solution

--Create a serialized adapter --Create package-info.java in the package with the response model and load the created adapter

Example in JAXB

Create an adapter

LocalDateAdapter.java


package api.adapters;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;

import javax.xml.bind.annotation.adapters.XmlAdapter;

public class LocalDateAdapter extends XmlAdapter<String, LocalDate> {
	@Override
	public LocalDate unmarshal(String s) throws Exception {

		if (s == null) {
			return null;
		}
		DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
		LocalDate dateTime = LocalDate.parse(s, formatter);
		return dateTime;
	}

	@Override
	public String marshal(LocalDate d) throws Exception {
		if (d == null) {
			return null;
		}
		DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
		String formattedDateTime = d.format(formatter);
		return formattedDateTime;
	}
}

LocalDateTimeAdapter.java


package api.adapters;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

import javax.xml.bind.annotation.adapters.XmlAdapter;

public class LocalDateTimeAdapter extends XmlAdapter<String, LocalDateTime> {
	@Override
	public LocalDateTime unmarshal(String s) throws Exception {

		if (s == null) {
			return null;
		}
		DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
		LocalDateTime dateTime = LocalDateTime.parse(s, formatter);
		return dateTime;
	}

	@Override
	public String marshal(LocalDateTime d) throws Exception {
		if (d == null) {
			return null;
		}
		DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
		String formattedDateTime = d.format(formatter);
		return formattedDateTime;
	}
}

Read with package-info

package-info.java


//Annotate the adapter
@XmlJavaTypeAdapters(value = { @XmlJavaTypeAdapter(value = LocalDateTimeAdapter.class, type = java.time.LocalDateTime.class),
@XmlJavaTypeAdapter(value = LocalDateAdapter.class, type = java.time.LocalDate.class) })
package api.model.response;

import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapters;

//Import adapter
import api.adapters.LocalDateAdapter;
import api.adapters.LocalDateTimeAdapter;

Recommended Posts

[Java] "T" is included in date type JSON in API response
Studying Java 8 (date API in java.time package)
Try using JSON format API in Java
The intersection type introduced in Java 10 is amazing (?)
Read JSON in Java
Pack API response (java)
Type determination in Java
Zabbix API in Java
POST JSON in Java
Create JSON in Java
Date manipulation in Java 8
[Java] Date type conversion
Try functional type in Java! ①
Difference in included SDK (Jar) by Java8 type JDK Vender
Java Stream API in 5 minutes
POST Json in Java ~ HttpURLConnection ~
Json serialization / deserialization in Java 1.4
View current date in Java
Date processing in Java (LocalDate: Initialization)
Generate CloudStack API URL in Java
Hit Zaim's API (OAuth 1.0) in Java
Parsing the COTOHA API in Java
Is there no type in Ruby?
JPA (Java Persistence API) in Eclipse
What is a class in Java language (3 /?)
I tried using Elasticsearch API in Java
Implement API Gateway Lambda Authorizer in Java Lambda
[Easy-to-understand explanation! ] Reference type type conversion in Java
[HTTP] Status code included in the HTTP response
Try using the Stream API in Java
Call the Windows Notification API in Java
[Personal memo] Java data type is annoying
What is a class in Java language (1 /?)
What is a class in Java language (2 /?)
Change List <Optional <T >> to Optional <List <T >> in Java
Output Date in Java in ISO 8601 extended format
Java 8 to start now ~ Date time API ~
What is the main method in Java?
The Java EE Security API is here!
How to get the date in java
Working with huge JSON in Java Lambda
[Java] Difference between equals and == in a character string that is a reference type
About the meaning of type variables, E, T, etc. used in generics used in Java