2013-07-12 2 views
2
class AgentResponse[T] @JsonCreator()(@JsonProperty("result") val result: T, @JsonProperty("status") val status: ResponseStatus) 

class ResponseStatus @JsonCreator()(@JsonProperty("succeeded") val succeeded: Boolean, @JsonProperty("message") val message: String, @JsonProperty("timeStamp") val timeStamp: Long) 

new ObjectMapper().registerModule(DefaultScalaModule).writer().writeValue(out, new AgentResponse(result, new ResponseStatus(true, "OK", now))) 

이 발생 오류 :Jackson을 사용하여 여러 인수 생성자로 스칼라 객체를 직렬화하는 방법은 무엇입니까?

JsonMappingException: No serializer found for class com.fg.mail.smtp.rest.Handler$AgentResponse and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationConfig.SerializationFeature.FAIL_ON_EMPTY_BEANS)) 

어떻게 스칼라 객체가 예상대로 작동하려면 같이해야합니까?

답변

3

scala.annotation.meta에서 인용 :

기본적으로 (val 또는 일반 - -, var)에 주석 생성자의 매개 변수가없는 다른 개체에 매개 변수에 끝낸다. 필드의 주석은 기본적으로 필드에서 끝납니다.

패키지 scala.annotation.meta의 메타 주석은 필드 및 클래스 매개 변수의 주석을 복사하는 위치를 제어하는 ​​데 사용됩니다. 이것은 주석 유형 또는 주석 클래스에이 패키지의 하나 또는 여러 개의 메타 주석으로 주석을 추가하여 수행됩니다.

따라서 주석은 생성자 매개 변수로 이동합니다. getter 및 생성자 매개 변수에 할당해야합니다.

class MyClass @JsonCreator() (@(JsonProperty @getter @param) val arg: String) 
+0

감사합니다. – lisak

+0

@ 스핀 제대로 작동하는지 알려주세요. 나는 그것을 시험 할 시간이 없었다. –

+0

예, 예상대로 작동합니다. – lisak

관련 문제