2013-06-15 2 views
0

get 요청이 내 서버에 호출 될 때 XML 파일을 응답으로 보내려고합니다.XML 파일을 읽고 restlet에서 응답으로 보내는 방법

@Get 
public Representation getRootDeviceXML() throws IOException { 
    File xmlFile = new File("rootdevices.xml"); 
    if (!xmlFile.exists()) 
     throw new ResourceException(Status.SERVER_ERROR_INTERNAL); 

    SaxRepresentation result; 
    try { 
     InputStream inputStream = new FileInputStream(xmlFile); 
     InputSource inputSource = new InputSource(new InputStreamReader(
       inputStream)); 

     result = new SaxRepresentation(MediaType.TEXT_XML, inputSource); 
    } catch (IOException e) { 
     throw new IOException(e.toString()); 
    } 

    Writer writer = new OutputStreamWriter(System.out); 
    result.write(writer); 

    return result; 

} 

그러나 아무것도 실제로 클라이언트 측에서 응답으로까지 보여주지는 (아니 (404), 헤더가 제대로 콘텐츠 형식으로 전송됩니다 텍스트/XML을; 문자셋 = UTF-8). 내가 도대체 ​​뭘 잘못하고있는 겁니까?

답변

1

나는 이것이 왜 필요한 것보다 더 복잡하게 만들었는지 알지 못합니다.

이렇게하면 XML 파일 콘텐츠를 성공적으로 응답으로 보냈습니다.

@Get ("xml") 
public String getRootDeviceXML() throws IOException { 
    BufferedReader br = new BufferedReader(new FileReader(xmlFile)); 
    String line; 
    StringBuilder sb = new StringBuilder(); 

    while((line=br.readLine())!= null){ 
     sb.append(line.trim()); 
    } 

    br.close(); 

    return sb.toString(); 
} 
관련 문제