【问题标题】:How to write file contents to a stream in java如何将文件内容写入java中的流
【发布时间】:2015-06-09 08:12:07
【问题描述】:

我正在使用 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>

【问题讨论】:

  • but when I hit the URL in browser its not the case 这是什么意思,你有什么错误吗?

标签: java xml web-services rest


【解决方案1】:

在 servlet 的 doGet 中尝试使用

import static java.lang.System.out;

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

其中 response 是 ServletResponse 类型(或 HttpServletResponse 继承)。当尝试从不同的类调用它时,要么将整个 ServletResponse 或从 getWriter() 返回的 PrinterWriter 作为输出变量传递。

【讨论】:

    猜你喜欢
    • 2015-07-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多