2017-10-29 7 views
1

에 대한 파생되지 Http4s EntityDecoder이 오류가 점점 오전 :자동 간단한 경우 클래스

case req @ POST -> Root/"v1"/"profiles" => 
    req.as[UserProfile] flatMap {(up: UserProfile) => 
    Ok(service.createProfile(up).asJson) 
    } 
:

case class UserProfile(id: Option[Int], firstName: String, lastName: String) 

POST 코드에 오류가 발생했습니다 : 다음과 같은 경우 클래스

Cannot decode into a value of type com.blah.rest.model.UserProfile, 
because no EntityDecoder[cats.effect.IO, com.blah.rest.model.UserProfile] 
instance could be found. 

다음과 같이 POST 본문 :

{ 
    "firstName": "Jack", 
    "lastName": "Appleseed" 
} 

본체가 req.as[UserProfile]으로 변환 될 때 발생한다고 생각합니다!

그러나 이것은 일반 바닐라 케이스 클래스이며 EntityDecoder은 자동으로 파생되어야합니다! 나는 akka-http을 알고있다!

아이디어가 있습니까?

은 참고 사항 : Http4sVersion = "0.18.0-M4"circe version "0.9.0-M1"

답변

1

대답은 : 당신이 얻을

req.decodeJson[UserProfile] flatMap {(up: UserProfile) => 
    Ok(service.createProfile(up).asJson) 
    } 

그 이유는는 http4s EntityDecoder에 키르케 디코더에서 다리입니다 암시하지 않습니다. 순전히 JSON API 인 경우 하나를 만들 수 있지만 라이브러리가 일반적으로 만들 수있는 가정은 아닙니다.

0

이 종속성을 추가 : "io.circe" %% "circe-generic" % "0.9.1"

나를 위해 JSON에 케이스 클래스의 자동 인코딩을 해결.

필요한 가져 오기를 허용합니다. import io.circe.generic.auto._