如何获得简单的javax.ws REST服务URL

时间:2016-10-31 14:24:37

标签: java rest curl javax.ws.rs

以下是使用javax.ws的简单服务的玩具示例。我想获取服务URL,可以从Web浏览器或卷曲中调用。 这是玩具服务代码:

package packagename;

import javax.jws.WebMethod;
import javax.jws.WebService;
import javax.ws.rs.GET;
import javax.ws.rs.Path;

@WebService
@Path("/service")
public class testserver
{
    @GET
     @Path("/test")
     @WebMethod
     public   String test()
    {
        return "<html>Test text here</html>";
    }
}

这是服务部署者功能:

package packagename;
import javax.xml.ws.Endpoint;

    public class deploy
{
    public static void main(String [] args)
    {

        String endpointURL = "http://localhost:7777/";
        Endpoint.publish(endpointURL,new testserver());
    }
}

我通过bash运行java文件没有错误。

不应导航到http://localhost:7777/service/test生成test()函数的文本?我从浏览器中收到“未找到服务器”错误。

以下是http://localhost:7777/?wsdl处的wsdl文件。我在这里寻找的信息是什么?我通过从下面获取信息(testserverService等)尝试了一些网址但没有成功。

  <!-- Published by JAX-WS RI (http://jax-ws.java.net). RI's version is JAX-WS RI 2.2.9-b130926.1035 svn-revision#5f6196f2b90e9460065a4c2f4e30e065b245e51e. 
-->
<!-- Generated by JAX-WS RI (http://jax-ws.java.net). RI's version is JAX-WS RI 2.2.9-b130926.1035 svn-revision#5f6196f2b90e9460065a4c2f4e30e065b245e51e. 
-->
<definitions targetNamespace="http://packagename/" name="testserverService">
    <types>
        <xsd:schema>
            <xsd:import namespace="http://packagename/" schemaLocation="http://localhost:7777/?xsd=1"/>
        </xsd:schema>
    </types>
    <message name="test">
        <part name="parameters" element="tns:test"/>
    </message>
    <message name="testResponse">
        <part name="parameters" element="tns:testResponse"/>
    </message>
    <portType name="testserver">
        <operation name="test">
            <input wsam:Action="http://packagename/testserver/testRequest" message="tns:test"/>
            <output wsam:Action="http://packagename/testserver/testResponse" message="tns:testResponse"/>
        </operation>
    </portType>
    <binding name="testserverPortBinding" type="tns:testserver">
        <soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="document"/>
        <operation name="test">
            <soap:operation soapAction=""/>
            <input>
                <soap:body use="literal"/>
            </input>
            <output>
                <soap:body use="literal"/>
            </output>
        </operation>
    </binding>
    <service name="testserverService">
        <port name="testserverPort" binding="tns:testserverPortBinding">
            <soap:address location="http://localhost:7777/"/>
        </port>
    </service>
</definitions>

我猜测答案很简单,或者我在代码中犯了严重的语法错误。 你能帮忙吗?

1 个答案:

答案 0 :(得分:4)

您正在混合SOAP和REST API,这是不正确的。您不能将它们一起用于同一端点。

javax.jws.*包(称为JAX-WS)表示SOAP API

javax.ws.rs.*包(称为JAX-RS)表示REST API

你需要了解SOAP和amp;之间的区别。 REST Web服务。您可以查看here以获取有关这些概念的更多详细信息。

假设您正在寻找REST服务实现,通常,REST服务被部署到服务器(如Tomcat,Jetty,Weblogic)中,但如果您需要独立运行它们,请查看here