如何在java中将文件内容写入流中

时间:2015-06-09 08:12:07

标签: java xml web-services rest

我正在使用JAX-WS API编写一个基本的Restful Web服务,您可以在其中提供城市名称,它会为您提供最低和最高温度以及其他一些信息。

我收到http://api.openweathermap.org/data/2.5/weather?q=singapore&mode=xml

的回复
@WebServiceProvider
@ServiceMode(value = javax.xml.ws.Service.Mode.MESSAGE)
@BindingType(value = HTTPBinding.HTTP_BINDING)

public class RESTfulWeather implements Provider<Source> {

@Resource
protected WebServiceContext wsContext;

@Override
public Source invoke(Source request) {      
    MessageContext msg_cxt = wsContext.getMessageContext();
    String httpMethod = (String) msg_cxt
            .get(MessageContext.HTTP_REQUEST_METHOD);
    System.out.println("Http Method : " + httpMethod);
    if (httpMethod.equalsIgnoreCase("GET")) {
         doGet(msg_cxt);
    }
    return null;
}

private void doGet(MessageContext msg_cxt) {
    String query_string = (String) msg_cxt.get(MessageContext.QUERY_STRING);
    System.out.println("Request String : " + query_string);
    try {
        URL url = new URL(
                "http://api.openweathermap.org/data/2.5/weather?q=singapore&mode=xml");
        HttpURLConnection urlConnection = (HttpURLConnection) url
                .openConnection();
        urlConnection.setRequestMethod("GET");
        urlConnection.connect();

        BufferedReader bReader = new BufferedReader(new InputStreamReader(
                urlConnection.getInputStream()));
        String line = null;         
        String text="";
        while ((line = bReader.readLine()) != null) {
            System.out.println(line);           
            text=text+line;     
        }   
        System.out.println(text);

    } catch (Exception e) {
        e.printStackTrace();
    }

}   
}

以下是发布商

public class WeatherPublisher {

public static void main(String[] args) {
    System.out.println("Publishing Weather Service");
    Endpoint.publish("http://127.0.0.1:8880/weather",new RESTfulWeather());

}
}

我确实在控制台中打印了xml,但是当我在浏览器中点击URL时并非如此。

如何将xml提供给浏览器,如何将XML响应从天气服务写入浏览器

以下是在控制台中打印的服务的响应,我也想在浏览器中使用

<?xml version="1.0" encoding="utf-8"?>
<current>
<city id="1880252" name="Singapore">
<coord lon="103.85" lat="1.29"/>
<country>SG</country>
<sun rise="2015-06-08T22:57:58" set="2015-06-09T11:09:41"/>
</city>
<temperature value="303.54" min="303.15" max="304.15" unit="kelvin"/>
<humidity value="66" unit="%"/>
<pressure value="1010" unit="hPa"/>
<wind>
<speed value="5.1" name="Gentle Breeze"/>
<direction value="260" code="W" name="West"/>
</wind>
<clouds value="75" name="broken clouds"/>
<visibility value="10000"/>
<precipitation mode="no"/>
<weather number="521" value="proximity shower rain" icon="09d"/>
<lastupdate value="2015-06-09T06:38:21"/>
</current>

1 个答案:

答案 0 :(得分:0)

在servlet的doGet中尝试

import static java.lang.System.out;

PrintWriter out = response.getWriter(); 
response.getWriter().println("text");
out.println( "text");

其中响应​​是ServletResponse类型(或HttpServletResponse ingherited)。当尝试从另一个类调用它时,要么将整个ServletResponse或从getWriter()返回的PrinterWriter作为varibale传递。