2011-02-04 3 views
0

JaxRS를 사용하여 공급 업체의 API와 상호 작용하는 클라이언트 측 코드를 생성하고 있습니다. 나는 GET과 POST를 수행하는 코드를 작성할 수 있었다. 둘 다 요청 본문이 없다. 현재 요청 본문 만 전송되는 POST를 시도하는 중에 문제가 발생합니다.JSON 문자열의 요청 본문이있는 게시물을 수행하는 Jax-RS 클라이언트

{"server":{"name":"HelloKitty","imageId":71,"flavorId":1}} 

다음은 내가 사용하고있는 웹 클라이언트에 대한 정의입니다 (스칼라로 작성,하지만 난 헤더와 권한이 올바르게 수행되고 있다는 것을 알고) : 나는 간단한 JSON 문자열을 보내려고하고

여기

def postClient: WebClient = { 
    val authClient = WebClient.create(authServer) 
    authClient.header("X-Auth-User", apiUsername) 
    authClient.header("X-Auth-Key", apiKey) 

    val response = authClient.get 
    val metadata = response getMetadata() map { case (k, v) => k -> v(0).toString } 

    val client = WebClient.create(metadata.get("X-Server-Management-Url").getOrElse("Error in authentication response")) 
    client.header("X-Auth-User", apiUsername) 
    client.header("X-Auth-Key", apiKey) 
    client.header("X-Auth-Token", metadata.get("X-Auth-Token").getOrElse("Error in authentication response")) 
    client.header("Content-Type","application/json") 
} 
내 자원 인터페이스 : 여기
@Produces(Array("text/json")) 
trait RackspaceServerApi { 
    @Path("/servers.json") 
    @GET 
    def listServers(): String 

    @Path("/servers/detail.json") 
    @GET 
    def listServersDetailed(): String 

    @POST 
    @Path("/servers") 
    @Consumes(Array("application/json")) 
    def createServerData(server: String) 
} 

프록시를 생성하고 요청을 보내려고 내 방법입니다

,
def createServer(name: String, imageId: Long, flavorId: Int) = { 
    // this creates a simple pojo for the server 
    val serverObject = new RackspaceCreateServerObject(name, imageId, flavorId, None, None) 

    // i am using lift-web to handle the json 
    // here I am unmarshalling the serverObject into a json string 
    val jsonServerData = write(serverObject) 

    val postProxy = JAXRSClientFactory.fromClient(RackspaceConnection.postClient, classOf[RackspaceServerApi], true) 
    postProxy.createServerData(jsonServerData) 
} 

그리고 여기에 내가 얻을 특정 WebApplicationException입니다 :

상태 코드 : [500]
엔티티 : [응답 클래스 찾을 수 없습니다 메시지 본문 작가 :. JAXBElement 첨부]

그러나, I 다음과 같이 프록시 대신 webclient를 사용하여 요청을 보내보십시오.

def createServer(name: String, imageId: Long, flavorId: Int) = { 
    // this creates a simple pojo for the server 
    val serverObject = new RackspaceCreateServerObject(name, imageId, flavorId, None, None) 

    // i am using lift-web to handle the json 
    // here I am unmarshalling the serverObject into a json string 
    val jsonServerData = write(serverObject) 

    val webClient = RackspaceConnection.postClient 
    webClient.path("/servers") 
    webClient.post(jsonServerData) 
} 

작동합니다. 인터페이스에서 뭔가 잘못하고있는 것입니까 (주석의 일부 마법 조합이 누락 되었습니까?)? 또는 나는 무엇인가 설정하지 않았 느냐? Jax-RS 2.3.2를 사용하고 있습니다.

답변

0

어떤 JAX-RS 구현을 사용하고 있습니까?

오류 메시지는 JAXB bean을 JSON으로 마샬링하는 것을 처리하기 위해 플러그인/모듈을 등록해야한다는 것을 나타냅니다. 일부 JAX-RS 구현은이 작업을 자동으로 수행하고 일부는 초기화 작업을 필요로합니다.