많은 버전이있는 것 같습니다. 여기 내가하는 일이있다. 나는 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에서 통해오고있다. 내가 뭘 잘못하고 있니?
요청 본문은 실제로 어디에 설정되어 있습니까? – JamesB
'marshal'이 시체를 설정하지 않았습니까? 즉, jaxbMarshaller.marshal (obj, urlConnection.getOutputStream()); – user2689782
@ user2689782 - 그렇습니다. 'marshal' 연산이 본문을 설정합니다. 발생하는 문제는 XML을 검색하려는 위치입니다. http://stackoverflow.com/a/24250583/383861 –