2014-06-16 3 views
2

많은 버전이있는 것 같습니다. 여기 내가하는 일이있다. 나는 RESTful 웹 서비스를 가지고 있으며 자바 객체를 JAXB로 마샬링하고 와이어를 통해 웹 서비스에 보내려고한다. 여기 내 클라이언트 측 코드JAXB, REST XML 객체를 원격 URL로 전송

private static void performPost(JAXBObject obj) { 
    String url ="http://www.example.com/test?xml="; 


    HttpURLConnection urlConnection = (HttpURLConnection) new URL(url).openConnection(); 
    urlConnection.setRequestMethod("POST"); 
    urlConnection.setDoOutput(true); 
    urlConnection.setRequestProperty("accept-charset", charset); 
    urlConnection.setRequestProperty("content-type", "application/x-www-form-urlencoded"); 
    urlConnection.setRequestProperty("accept", jsonContentType); 

    JAXBContext jaxbContext = JAXBContext.newInstance(JAXBObject.class); 
    Marshaller jaxbMarshaller = jaxbContext.createMarshaller(); 
    jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); 
    jaxbMarshaller.marshal(obj, urlConnection.getOutputStream()); 
    int rspCode = urlConnection.getResponseCode(); 
    ... 
} 

WebService에 코드

@POST 
@Produces("application/json") 
@Path("test") 
public String doIt(@QueryParam("xml") String str) { 
    int ret = doSomething(str); 
    return new Gson().toJson(ret); 
} 

이 프로그램은 DOIT에 도달입니다,하지만 아무것도 문자열 STR에서 통해오고있다. 내가 뭘 잘못하고 있니?

+1

요청 본문은 실제로 어디에 설정되어 있습니까? – JamesB

+0

'marshal'이 시체를 설정하지 않았습니까? 즉, jaxbMarshaller.marshal (obj, urlConnection.getOutputStream()); – user2689782

+0

@ user2689782 - 그렇습니다. 'marshal' 연산이 본문을 설정합니다. 발생하는 문제는 XML을 검색하려는 위치입니다. http://stackoverflow.com/a/24250583/383861 –

답변

4

질문에있는 코드가 XML을 메시지로 보내고 있지만 쿼리 매개 변수로 수신하려고합니다. XML을 메시지로 보내려면 str 매개 변수에 @QueryParam이라는 주석을 달지 않아야합니다. 쿼리 매개 변수는 URL 뒤에? (즉, http://www.example.com/foo/bar?limit=25의 경우 limit이 검색어 매개 변수 임). 주석을 필요로하지 않는 XML 메시지 본문을 캡처하려고합니다.

+0

오, 알겠습니다. 여러 개의 XML 파일을 전달할 수 있도록 "http://www.example.com/test?xml="을 보낼 수 없습니까? xml 파일의 단지 하나의 출력 스트림, 나는 ... – user2689782

+0

@ user2689782 - 하나의 요청으로 여러 개의 XML 파일을 보내시겠습니까? –

+0

예, 했어요 ... 나는 그 길을 찾아야 할 것 같아요. – user2689782