首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >RESTful伺服

RESTful伺服
EN

Stack Overflow用户
提问于 2015-11-10 19:19:17
回答 1查看 76关注 0票数 0

我想在java上创建RESTful web服务,我选择了并得到了下面的代码。

代码语言:javascript
复制
@Path("/message")
public class Rest_test {

    @Context
    @SuppressWarnings("unused")
    private UriInfo context;

    /**
     * Default constructor. 
     */
    public Rest_test() {
        // TODO Auto-generated constructor stub
    }

    /**
     * Retrieves representation of an instance of Rest_test
     * @return an instance of String
     */
    @GET
    @Produces("application/xml")
    public String getXml() {
        // TODO return proper representation object
        return "sdfgdsd";
    }

    /**
     * PUT method for updating or creating an instance of Rest_test
     * @param content representation for the resource
     * @return an HTTP response with content of the updated or created resource.
     */
    @PUT
    @Consumes("application/xml")
    public void putXml(String content) {
    }

}

为了测试我的应用程序,我使用程序邮递员,并从这个广告localhost:8080/test/rest/message调用我的服务器,但我总是有相同的页面HTTP状态404 -没有找到。请帮帮我

EN

回答 1

Stack Overflow用户

发布于 2015-11-10 19:44:10

评论是正确的!您应该调用: localhost:8080/nameOfYourService/rest/message

我也有一个TestWebService,不能突然调用它。

我创建了一个本地客户端应用程序并使用了一个httpURLconnection:

代码语言:javascript
复制
private class yourTestApp {
....
public static void getStringOrWhatEver() {
    try {
        URL url = new URL("http://localhost:8080/<nameOfYourService>/rest/message");
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setRequestMethod("GET");
        conn.setRequestProperty("Accept", "application/xml");

        if (conn.getResponseCode() != 200) {
            throw new RuntimeException("Failed : HTTP error code : "
                    + conn.getResponseCode());
        }

        BufferedReader br = new BufferedReader(new InputStreamReader(
                (conn.getInputStream())));

        String output;
        System.out.println("...and the message is: ");
        while ((output = br.readLine()) != null) {
            System.out.println(output);
        }
        System.out.println(".... End of content\n");
        conn.disconnect();

    } catch (MalformedURLException ex) {
        System.err.println("MalformedURLException, " + ex);
    } catch (IOException ex) {
        System.err.println("IOException, " + ex);
    }
}
....
}

在这里输入图像描述

对你来说,"ps“应该是”信息“

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/33637852

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档