【发布时间】:2018-01-14 23:15:03
【问题描述】:
CustomObjectResource - REST 服务返回一个简单的 POJO,其中包含文本、长日期和本地日期时间字段。
@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();
}
}
这是我的邮递员。 Part -1 accept=json 状态码为 200,JSON 解析失败。实际上,返回的对象是 XML 格式的。当我选择以 XML 显示结果时,
<customObject>
<id>5</id>
<text>CustomObject_5</text>
<timestamp>2017-08-07 17:17:40</timestamp>
</customObject>
现在,我使用 Accept:application/xml
它不返回任何东西:404。
我在 Jackson 上使用 SpringBoot。
这是我的 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;
}
更新
我正在调试。在我的代码中一切正常......但是当我进一步调试时:
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
【问题讨论】:
-
@peeskillet 有日志消息
2017-08-08 10:21:58.048 WARN 8436 --- [qtp620412175-32] o.e.jetty.server.handler.ErrorHandler : Error page loop /error -
这是一个警告,不是错误,但它看起来与上面的#2有关。
-
@peeskillet 当我调试时,控制器的(资源)逻辑中没有异常。感觉 Spring 有一个 bug。
-
stackoverflow.com/questions/12392235/… '@XmlAccessorType(XmlAccessType.FIELD)' 到班级。stackoverflow.com/questions/12392235/…
标签: java spring-boot jackson jersey jsr