如何在 Java 中创建 Web 服务(使用 Eclipse)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7110794/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 18:40:55  来源:igfitidea点击:

How to create Web Services in Java (with Eclipse)

javaweb-services

提问by bobetko

I am really really having hard time to create simple "Hello World" Web Service in Java. If you exclude several mobile Android mobile apps I have done, I am pretty new to Java environment.

我真的很难用 Java 创建简单的“Hello World”Web 服务。如果排除我做过的几个移动Android移动应用程序,我对Java环境还很陌生。

First I tried Axis2 and that simply doesn't work out of the box. Fresh installation of Eclipse, Tomcat 6.0 and Axis2. Tried sample test app and it failed miserably. You can read about it here. My conclusion is that Axis2 simply doesn't work (maybe it used to work). I will try to go back to install older versions, maybe one will magically start working. I have to modify some old project at work that was using Axis2, so that's why I have to stick with it. I would hate to have to migrate to some other tool.

首先,我尝试了 Axis2,但它无法立即使用。全新安装 Eclipse、Tomcat 6.0 和 Axis2。尝试了示例测试应用程序,但失败了。你可以在这里阅读它。我的结论是 Axis2 根本不起作用(也许它曾经起作用)。我会尝试回去安装旧版本,也许一个人会神奇地开始工作。我必须在工作中修改一些使用 Axis2 的旧项目,所以这就是我必须坚持使用它的原因。我不想迁移到其他工具。

Then I ran on Oracle article Getting Started with JAX-RPCand I was thinking, cool, let's try that. Well, I just managed to get pissed off. After getting half way through the article and trying to write some simple test web service I realized that their code examples are poorly written. Missing brackets, wrong references, missing explanations, etc...

然后我浏览了 Oracle 文章JAX-RPC 入门,我在想,很酷,让我们尝试一下。好吧,我只是设法生气了。在文章读到一半并尝试编写一些简单的测试 Web 服务后,我意识到他们的代码示例编写得很差。缺少括号、错误引用、缺少解释等...

First of all, an interface should extend not implement. Am I right?

首先,接口应该扩展而不是实现。我对吗?

public interface SunRegPort implementsjava.rmi.Remote {

公共接口 SunRegPort实现java.rmi.Remote {

Also, this doesn't exist:

此外,这不存在:

import java.xml.rpc.server.ServiceLifecycle;

导入java.xml.rpc.server.ServiceLifecycle;

But this does:

但这确实:

import javax.xml.rpc.server.ServiceLifecycle;

导入javax.xml.rpc.server.ServiceLifecycle;

Well, I don't feel competent to criticize too much (cause of my level of knowledge about this subject of course), but after all I am suspicious that this article is complete JUNK and therefore I am not able to follow it.

好吧,我觉得没有资格批评太多(当然是因为我对这个主题的知识水平),但毕竟我怀疑这篇文章是完整的垃圾,因此我无法遵循它。

Please somebody tell me that I am wrong and if anybody has some advice or link to some How-To page that talks about web services, I would appreciate.

请有人告诉我我错了,如果有人有一些建议或链接到一些讨论 Web 服务的操作方法页面,我将不胜感激。

Thanks.

谢谢。

回答by Bruno Grieder

The terminology Web Services is pretty vague. In Java, the modern and reasonably easy way to do this is by annotating classes. I would recommend that first, you decide whether you want to implement:

术语 Web 服务非常模糊。在 Java 中,现代且相当简单的方法是通过对类进行注释。我建议您首先决定是否要实施:

  • SOAP Web Services -> look at JAX-WS
  • REST Web Services -> look at JAX-RS
  • SOAP Web 服务 -> 查看JAX-WS
  • REST Web 服务 -> 查看JAX-RS

Once you have chosen the "type" of Web Services, choose the library that implements the specification.

一旦您选择了 Web 服务的“类型”,请选择实现规范的库。

The Wikipedia entries list a few of these implementations. Apache CXF (complete but a beast), Jersey (popular) and Restlet are very common choices. I personally like the JBoss implementations as well for JAX-WS.

维基百科条目列出了其中一些实现。Apache CXF(完整但野兽)、Jersey(流行)和 Restlet 是非常常见的选择。我个人也喜欢 JAX-WS 的 JBoss 实现。

For JAX-WS with Apache CXF, here is a quick start tutorialthat looks pretty good (untested)

对于带有 Apache CXF 的 JAX-WS,这里有一个看起来不错的快速入门教程(未经测试)

For JAX-RS with Jersey, try this

对于带有 Jersey 的 JAX-RS,试试这个

Unless you have strong reasons to do otherwise (for instance you need to call an existing system), use REST which is simpler and leaner.

除非您有充分的理由不这样做(例如,您需要调用现有系统),否则请使用更简单、更精简的 REST。

回答by hequ

I would recommend you to check out the CXF project by apache. It's quite easy to use and should help you to set up a webservice.

我建议您查看 apache 的 CXF 项目。它非常易于使用,应该可以帮助您设置网络服务。

There is a nice guide to Hello world set up.

有一个很好的 Hello world 设置指南。

http://cxf.apache.org/docs/a-simple-jax-ws-service.html

http://cxf.apache.org/docs/a-simple-jax-ws-service.html

回答by DaveFar

If you are not tied to Eclipse, give NetBeans a shot. I think it's muchbetter for Web Services in Java.

如果您不依赖于 Eclipse,请尝试使用 NetBeans。我认为Java 中的 Web 服务好得多。

回答by Dave

If you're still interested in giving Tomcat a shot, here is a tutorial on youtube of making a very basic "Hello world" example using Eclipse. It leaves a lot to learn; but it looks like it will at least get you started.

如果您仍然对尝试使用 Tomcat 感兴趣,这里是 youtube 上的一个使用 Eclipse 制作非常基本的“Hello world”示例的教程。它还有很多东西需要学习;但看起来它至少会让你开始。

FYI, the version of Eclipse used is slightly outdated. So some of the menu options may not be exactly the same:

仅供参考,使用的 Eclipse 版本略有过时。所以一些菜单选项可能不完全相同:

http://www.youtube.com/watch?v=EOkN5IPoJVs

http://www.youtube.com/watch?v=EOkN5IPoJVs

Also, the meaning of "Web Service" can be subjective. So, to be specific; this just shows you how to get a basic HTTP endpoint started on your local machine.

此外,“Web 服务”的含义可能是主观的。所以,具体来说;这只是向您展示如何在本地计算机上启动基本的 HTTP 端点。

回答by Pavan_opencodez

Web Services are most widely implemented examples of Service Oriented Architecture (SOA). A service contract is defined with the help of SOAP and Web Service Definition Language (WSDL) and that is published for other applications to use.

Web 服务是最广泛实现的面向服务架构 (SOA) 的示例。服务契约是在 SOAP 和 Web 服务定义语言 (WSDL) 的帮助下定义的,并发布供其他应用程序使用。

In this article we will see how we can create web service and web service client in java.

在本文中,我们将看到如何在 Java 中创建 Web 服务和 Web 服务客户端。

http://www.opencodez.com/java/how-to-build-and-deploy-web-service-and-client-in-java.htm

http://www.opencodez.com/java/how-to-build-and-deploy-web-service-and-client-in-java.htm