CustomObjectResource - rest service returning a simple POJO with text, long and local date time fields.
@Component
@Path("/resource")
public class CustomObjectResource {
private RandomCOBuilder randomCOBuilder = new RandomCOBuilder();
@GET
@Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
public Response getCustomObject(@Context HttpServletRequest httpRequest) {
String acceptHeader = httpRequest.getHeader("Accept");//I do not use it in the code. When I debug, this param is correct.
CustomObject customObject = randomCOBuilder.get();
return Response
// Set the status and Put your entity here.
.ok(customObject)
// Add the Content-Type header to tell Jersey which format it should marshall the entity into.
.build();
}
}
And this is my Postman. Part -1 accept=json Status code is 200, the JSON parsing fails. Actually, the object returned is in XML. When I choose to display the result in XML,
<customObject>
<id>5</id>
<text>CustomObject_5</text>
<timestamp>2017-08-07 17:17:40</timestamp>
</customObject>
Now, I use Accept:application/xml
It does not return anything: 404.
I use SpringBoot with Jackson.
This is my gradle.build
group 'com.ca.training.rest'
version '1.0-SNAPSHOT'
apply plugin: 'java'
sourceCompatibility = 1.8
repositories {
mavenCentral()
}
dependencies {
compile "org.springframework.boot:spring-boot-starter-jetty:1.5.3.RELEASE"
compile "org.springframework.boot:spring-boot-starter-web:1.5.3.RELEASE"
compile "com.fasterxml.jackson.datatype:jackson-datatype-jsr310:2.8.8"
compile "com.fasterxml.jackson.dataformat:jackson-dataformat-xml:2.8.8"
compile 'javax.ws.rs:javax.ws.rs-api:2.0.1'
compile "org.springframework.boot:spring-boot-starter-jersey:1.5.3.RELEASE"
testCompile 'junit:junit:4.12'
}
CustomObject
@XmlRootElement(name = "customObject")
@JsonRootName(value = "customObject")
public class CustomObject {
private Long id;
private String text;
@JsonFormat(pattern = DATE_FORMAT)
@XmlJavaTypeAdapter(DateTimeAdapter.class)
private LocalDateTime timestamp;
}
Update
I was debugging. In my code everything works well... But when I debugged further:
com.sun.xml.internal.bind.v2.runtime.IllegalAnnotationsException: 1 counts of IllegalAnnotationExceptions
Class has two properties of the same name "timestamp"
this problem is related to the following location:
at public java.time.LocalDateTime com.ca.training.rest.example.core.entity.CustomObject.getTimestamp()
at com.ca.training.rest.example.core.entity.CustomObject
this problem is related to the following location:
at private java.time.LocalDateTime com.ca.training.rest.example.core.entity.CustomObject.timestamp
at com.ca.training.rest.example.core.entity.CustomObject