117.info
人生若只如初见

Java WebService 简单实例(附实例代码)

下面是一个简单的Java WebService实例,使用了JAX-WS标准:

  1. 编写WebService接口:
import javax.jws.WebMethod;
import javax.jws.WebService;
@WebService
public interface HelloWorld {
@WebMethod
String sayHello(String name);
}
  1. 实现WebService接口:
import javax.jws.WebService;
@WebService(endpointInterface = "HelloWorld")
public class HelloWorldImpl implements HelloWorld {
@Override
public String sayHello(String name) {
return "Hello, " + name + "!";
}
}
  1. 配置Web Service部署描述文件(web.xml):

WebServiceExample

WebServiceExample
com.sun.xml.ws.transport.http.servlet.WSServlet

com.sun.xml.ws.transport.http.servlet.WSServletContextListener
1



WebServiceExample
/hello


  1. 配置Web Service绑定描述文件(sun-jaxws.xml):



  1. 使用JAX-WS提供的工具生成Web Service服务端和客户端代码:
wsimport -keep -p com.example.ws http://localhost:8080/WebServiceExample/hello?wsdl
  1. 部署到Servlet容器(如Tomcat)并启动。

  2. 测试WebService服务端:

在浏览器中访问http://localhost:8080/WebServiceExample/hello?wsdl,能够看到生成的WSDL文件。

  1. 测试WebService客户端:
import com.example.ws.HelloWorld;
import com.example.ws.HelloWorldImplService;
public class HelloWorldClient {
public static void main(String[] args) {
HelloWorldImplService service = new HelloWorldImplService();
HelloWorld port = service.getHelloWorldImplPort();
String result = port.sayHello("Alice");
System.out.println(result);
}
}

这个简单的Java WebService实例演示了一个简单的问候服务。通过WebService接口定义了一个sayHello方法,实现类返回一个问候语。客户端通过调用WebService接口的方法与服务端进行交互。

未经允许不得转载 » 本文链接:https://www.117.info/ask/fed7fAzsLBgJfAlc.html

推荐文章

  • java判断闰年的代码怎么写

    以下是一个Java程序示例,用于判断给定的年份是否为闰年:
    import java.util.Scanner;
    public class LeapYear {
    public static void main(String...

  • java怎么求长方形的面积和周长

    要求长方形的面积和周长,首先需要知道长方形的长和宽。
    假设长方形的长为length,宽为width,则长方形的面积为:
    面积 = 长 宽 = length width

  • java怎么实现简单的俄罗斯方块

    要实现简单的俄罗斯方块游戏,可以按照以下步骤进行: 创建一个游戏窗口:使用Java的Swing或JavaFX库创建一个空白窗口,设置窗口的大小和标题。 创建一个游戏面板...

  • java如何计算矩形的周长和面积

    Java中计算矩形的周长和面积可以通过以下代码实现:
    public class Rectangle {
    private double length;
    private double width;
    public Rect...

  • SQL Server中索引的用法详解

    索引是一种数据结构,用于提高数据库中数据的查询效率。SQL Server中的索引可以分为聚集索引和非聚集索引两种类型。聚集索引决定了表中数据的物理存储顺序,一个...

  • SpringBoot如何使用applicationContext.xml配置文件

    在Spring Boot中,通常不直接使用applicationContext.xml配置文件来配置应用程序的上下文,而是通过Java配置类来代替。
    以下是使用applicationContext.xml配...

  • maven为MANIFEST.MF文件添加内容的方法

    要为Maven项目的MANIFEST.MF文件添加内容,你可以通过以下方法之一来实现:方法1:使用maven-jar-plugin插件1. 在项目的pom.xml文件中,找到标签。2. 在标签内添...

  • 关于C语言多线程pthread库的相关函数说明

    pthread库是C语言中用于多线程编程的一个标准库,包含了一系列的函数,用于创建、控制和管理线程。下面是一些常用的pthread库函数的简要说明: pthread_create:...