Java 宁静的服务

Java 宁静的服务,java,eclipse,rest,Java,Eclipse,Rest,我想在java上创建RESTfulWeb服务,我选择了eclipse自动生成并得到了这段代码 @Path("/message") public class Rest_test { @Context @SuppressWarnings("unused") private UriInfo context; /** * Default constructor. */ public Rest_test() { // TOD

我想在java上创建RESTfulWeb服务,我选择了eclipse自动生成并得到了这段代码

@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) {
    }

}
为了测试我的应用程序,我使用程序Postman并从这个adrres调用我的服务器 本地主机:8080/test/rest/message
但我总是有相同的页面HTTP状态404-找不到。请帮助我,评论是正确的!你应致电: localhost:8080/nameOfYourService/rest/message

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

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

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应该是消息

类名是Rest\u test,你们试图指向/test/Rest,也许你们错过了订单?我猜是因为我看不到定义路径的其余部分。@Mariusz Nosiński,如果我排在第二位的话,我应该写下项目的名称,我的项目名为Test。您需要添加有关如何打包应用程序以及如何部署应用程序的详细信息。我怀疑应用程序没有使用“测试”上下文进行部署。@EmeralJava如果有帮助,我将显示我的web.xml为什么URL路径中有rest,我认为你应该调用。在哪里可以找到我的nameOfMyService?name=webservice应用程序的名称是否测试缓慢我得到了与EmeralJava相同的印象-你运行了你的web服务吗?您是否有Web服务器?对不起,我在后台使用带有tomcat的Netbeans。-我运行我的网络服务,等几分钟,我试着发送邮递员的截图