java web 教程
Welcome to the Java Web Services Tutorial. Here we will learn about web services, useful concepts in web services and then different types of API we have in Java to create web services.
欢迎使用Java Web Services教程 。 在这里,我们将学习Web服务 , Web服务中的有用概念以及Java中用于创建Web服务的不同类型的API。
In simple words, services that can be accessed over network are called web services. So how does it differ from web application, they are also services that are accessed over network. There are few attributes that clarifies this difference.
简而言之,可以通过网络访问的服务称为Web服务。 因此,它与Web应用程序有何不同,它们也是通过网络访问的服务。 几乎没有属性可以阐明这种差异。
I hope above differences are good enough to clear any confusion with web applications and web services. Both are different concepts and meant for different purpose.
我希望上述差异足以消除与Web应用程序和Web服务的任何混淆。 两者都是不同的概念,并且具有不同的用途。
There are two types of web services.
Web服务有两种类型。
Java provides it’s own API to create both SOAP as well as REST web services.
Java提供了自己的API来创建SOAP和REST Web服务。
Both of these APIs are part of standard JDK installation, so we don’t need to add any jars to work with them. Both of these APIs use annotations very heavily.
这两个API都是标准JDK安装的一部分,因此我们不需要添加任何jar即可使用它们。 这两个API都非常频繁地使用注释。
Let’s create a very simple Hello World JAX-WS application.
让我们创建一个非常简单的Hello World JAX-WS应用程序。
TestService.java
TestService.java
package com.journaldev.jaxws.service;import javax.jws.WebMethod;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
import javax.xml.ws.Endpoint;@WebService
@SOAPBinding(style = SOAPBinding.Style.DOCUMENT)
public class TestService {@WebMethodpublic String sayHello(String msg){return "Hello "+msg;}public static void main(String[] args){Endpoint.publish("https://localhost:8888/testWS", new TestService());}
}
That’s it. Just run this application and our Hello World JAX-WS SOAP web service is published. Below image shows the invocation of this JAX-WS web service through SOAP UI.
而已。 只需运行此应用程序,我们的Hello World JAX-WS SOAP Web服务就会发布。 下图显示了通过SOAP UI对该JAX-WS Web服务的调用。
That’s it for a very basic tutorial of JAX-WS web service. Below are some of the articles you should read for better understanding of SOAP web services and JAX-WS.
这是一个非常基本的JAX-WS Web服务教程。 以下是您应该阅读的一些文章,以更好地理解SOAP Web服务和JAX-WS。
Jersey is the reference implementation of JAX-RS API, it’s not part of standard JDK and we have to include all the required jars. Best way is to use Maven build, so create a simple Dynamic web project and then convert it to Maven in Eclipse.
Jersey是JAX-RS API的参考实现,它不是标准JDK的一部分,我们必须包含所有必需的jar。 最好的方法是使用Maven构建,因此创建一个简单的Dynamic Web项目,然后在Eclipse中将其转换为Maven。
Here is the final pom.xml file having required dependencies.
这是具有必需依赖项的最终pom.xml文件。
Now add Jersey servlet to our deployment descriptor web.xml as front controller.
现在,将Jersey Servlet添加到我们的部署描述符web.xml中作为前端控制器。
Above two steps are required for initial setup, below is our Hello World JAX-RS service class.
初始设置需要上面的两个步骤,下面是我们的Hello World JAX-RS服务类。
package com.journaldev.jaxrs.service;import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;@Path("/test")
public class TestService {@GET@Path("/hello/{msg}")public String sayHello(@PathParam(value="msg") String msg){return "Hello "+msg;}
}
Just export it as WAR file and then access it in the browser as shown in below image.
只需将其导出为WAR文件,然后在浏览器中对其进行访问,如下图所示。
You can change the last part of URL and returned message will change accordingly.
您可以更改URL的最后一部分,返回的消息也将相应更改。
You can see how easy it was to create RESTful web service using JAX-RS API. However there is more to it, follow below articles to learn more.
您可以看到使用JAX-RS API创建RESTful Web服务是多么容易。 但是,还有更多内容,请按照以下文章了解更多信息。
That’s all for a quick introduction of java web services, finally if you are preparing for any interview then go through Web Services Interview Questions.
这就是快速介绍Java Web服务的全部内容 ,最后,如果您准备进行任何面试,请阅读Web服务面试问题 。
References: JAX-WS Oracle Page, JAX-RS Oracle Page
参考: JAX-WS Oracle Page , JAX-RS Oracle Page
翻译自: https://www.journaldev.com/9191/java-web-services-tutorial
java web 教程