This site uses cookies to deliver our services. By using this site, you acknowledge that you have read and understand our Cookie and Privacy policy. Your use of Kontext website is subject to this policy. Allow Cookies and Dismiss

.Net 中引用Java的Web Services

224 views 0 comments last modified about 9 years ago Raymond Tang

.NET Java Web Services

In this page

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());
        }
    }
}

点击运行即可查看到效果

至此工作就完成了

Related pages

Set AttachDbFilename as Relative Path in .NET Core

63 views   0 comments last modified about 2 months ago

.NET Framework, you can use |DataDirectory| to configure connection string when connecting to SQL Server database file via attach mode: AttachDbFilename=|DataDirectory|\dbname.mdf In .NET Core, you cannot directly set SQL Server Express connec...

View detail

Instantiate a Service in ConfigureServices Method in .NET Core

19 views   0 comments last modified about 2 months ago

.NET Core is built in with dependency injection. Usually method ConfigureServices in Startup class is used to register services in the container. The signature of the method looks like the following: public void ConfigureServices(IServiceC...

View detail

SQLite in .NET Core with Entity Framework Core

36 views   0 comments last modified about 2 months ago

SQLite is a self-contained and embedded SQL database engine. In .NET Core, Entity Framework Core provides APIs to work with SQLite. This page provides sample code to create a SQLite database using package Microsoft.EntityFrameworkCore.Sqlite . Create sample project ...

View detail

Graphics Programming and Image Processing in .NET Core 2.x

24 views   0 comments last modified about 2 months ago

In .NET Core 2.x, Windows Forms or WPF are not implemented since they are based on GDI+ and DirectX respectively in Windows. In .NET Core 3.0, there is plan to add Desktop Packs which includes UWP. WPF and Windows Forms. However, they will still be Windows-only. In .NET Core applications, you may...

View detail

Tuples in C# (4.x, 7.0, 7.1)

138 views   0 comments last modified about 6 months ago

What is a tuple? Tuple is an ordered sequence, fixed-size, immutable and of heterogeneous objects. Tuple has been existing in other languages such as F#, Python, Perl and etc. for a long time. It was first introduced into C# from C# 4.0 and has been evolving over time. Since C# 7.1, tuple...

View detail

Invoke Hadoop WebHDFS APIs in .NET Core

273 views   0 comments last modified about 6 months ago

Background Apache doesn't provide native official .NET APIs for Hadoop HDFS. The HTTP REST API supports the complete FileSystem / ...

View detail

Add comment

Please login first to add comments.  Log in New user?  Register

Comments (0)

No comments yet.