Keywords: .Net , Java EE, Eclipse, Web Services, GlassFish, jws

由于工作的需要,我们的.Net项目需要引用一个java平台下的Web Service 为了便于开发,自己先通过Eclipse建了一个Java的WebService,然后通过Java SDK中自带的GlassFish 服务器发布,然后在.Net项目中添加Web 引用,具体方法如下面所示(由于自己今天第一次学习java 第一次用到Eclipse 所以若有错误请大家见谅):

1,配置Eclipse的Servers

打开Eclipse,在菜单栏的Windows->Preferences 在弹出的对话框中点击Server节点下的Runtime Environments 添加GlassFish 如果没有,点击Add按钮,添加,如果列表中没有GlassFish,那么可以点击链接download additional server adapters 添加GlassFish


添加GlassFish

2,创建Web项目

在菜单栏 File->New->Dynamic Web Project 创建一个动态Web项目 名称为MyJavaWebService,runtime选择刚才配置的GlassFish


3,创建包含一个类HelloWorld


类的代码如下:

package Test;

public class HelloWorld {
public String MyHelloWorld()
{
return "Hello World";

}
}

4,创建WebService

右击刚才创建的HelloWorld.java->Web Services->Create Web Service

点击next 后会生成wsdl文件 以及自动发布到服务器,如果是GlassFish服务器则有可能在Eclipse无法启动服务,如下面,这个时候直接点击Cancel ,我们在以后发布此服务。

这样wsdl就生成好了

这种方法始终出错了,因此采用Tomcat作为服务器,至于解决方法以后再探讨

5,今天早上请教了javawind的唧唧童鞋,终于成功了,不过直接通过Eclipse自带的那种部署却始终不成功,在Tomcat中提示不支持 Webservice,而GlassFish也提示出错无法启动GlassFish的服务器 实际上是启动了的

下面是具体的实现方法:

1)配置web.xml

在<web-app>根节点下添加

<servlet>
    <servlet-name>AxisServlet</servlet-name>
    <servlet-class>org.apache.axis.transport.http.AxisServlet</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>AxisServlet</servlet-name>
    <url-pattern>*.jws</url-pattern>
</servlet-mapping>
<mime-mapping>
    <extension>wsdl</extension>
    <mime-type>text/xml</mime-type>
</mime-mapping>
<mime-mapping>
    <extension>xsd</extension>
    <mime-type>text/xml</mime-type>
</mime-mapping>
<servlet>
    <display-name>Axis Admin Servlet</display-name>
    <servlet-name>AdminServlet</servlet-name>
    <servlet-class>org.apache.axis.transport.http.AdminServlet</servlet-class>
    <load-on-startup>100</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>AdminServlet</servlet-name>
    <url-pattern>/servlet/AdminServlet</url-pattern>
</servlet-mapping>

2)将类HelloWorld拷贝至下面WebContent下 并且重命名为HelloWorld.jws,同时去掉命名空间,package Test; 这行


public class HelloWorld {
public String MyHelloWorld()
{
return "Hello World";

}
}
3)导出MyJavaWebService为war

4)在GlassFish中部署这个war

在GlassFish的管理界面->Web 应用程序->点击 部署按钮


点击确定后即可看到部署的war

点击启动即可查看到

点击HelloWorld.jws即可以查看到WebService




点击超链接Click to see WSDL即可查看到Webservice

6,下面我们在.net中调用这个Webservice

http://localhost:8080/MyJavaWebService/HelloWorld.jws?wsdl

1)在.Visual Studio 中新建一个Web项目 TestMyJavaWebService


2)添加web引用 地址为

http://localhost:8080/MyJavaWebService/HelloWorld.jws?wsdl

如下图所示,点击添加引用即可


3)在Default.aspx.cs中添加下面代码

namespace TestMyJavaWebService
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            MyJavaWebService.HelloWorldService service = new TestMyJavaWebService.MyJavaWebService.HelloWorldService();
            Response.Write(service.MyHelloWorld());
        }
    }
}

点击运行即可查看到效果

至此工作就完成了


info Last modified by Raymond at 7 years ago copyright This page is subject to Site terms.

More from Kontext

local_offer teradata local_offer python local_offer Java

visibility 603
thumb_up 0
access_time 4 months ago

Python JayDeBeApi module allows you to connect from Python to Teradata databases using Java JDBC drivers. In article Connect to Teradata database through Python , I showed ho...

open_in_new Python Programming

local_offer hadoop local_offer hive local_offer Java

visibility 686
thumb_up 1
access_time 4 months ago

When I was configuring Hive 3.0.0 in Hadoop 3.2.1 environment, I encountered the following error: Exception in thread "main" java.lang.NoSuchMethodError: com.google.common.base.Preconditions.checkArgument(ZLjava/lang/String;Ljava/lang/Object;)V Ro...

open_in_new Hadoop

local_offer sqlite local_offer python local_offer Java

visibility 126
thumb_up 0
access_time 5 months ago

To read data from SQLite database in Python, you can use the built-in sqlite3 package . Another approach is to use SQLite JDBC driver via  ...

open_in_new Python Programming

local_offer Java local_offer python local_offer SQL Server

visibility 1647
thumb_up 0
access_time 5 months ago

In my previous article  Connect to SQL Server via JayDeBeApi in Python , I showed examples of u...

open_in_new Python Programming

comment Comments (0)

comment Add comment

Please log in or register to comment.

account_circle Log in person_add Register

Log in with external accounts

No comments yet.

Kontext Column

Created for everyone to publish data, programming and cloud related articles. Follow three steps to create your columns.


Learn more arrow_forward