In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly introduces "using WebService to call resources from one project to another". In daily operation, it is believed that many people have doubts about using WebService to invoke resources in another project. The editor consulted all kinds of materials and sorted out simple and useful operation methods. I hope it will be helpful for you to answer the question of "using WebService to invoke resources in another project"! Next, please follow the editor to study!
When we are developing a project, if there is a need to invoke resources from one project to another, there are usually four options:
1. RESTful
2. WebService
3. RPC
4. Socket
Each of these methods has its own usage scenarios and characteristics, and today we will mainly talk about WebService.
WebService is also a web application, which can be described, published, discovered, and coordinated using XML standards, and can be used for distributed development.
When developing WebService programs, there are some basic concepts that you need to know:
SOAP: simple object transfer Protocol. SOAP uses XML messages to call remote methods so that web services can interact with other remote data through the post and get methods of the HTTP protocol.
WSDL: WebService description language. WSDL is a formal description document provided in a machine-readable manner and a XML-based language that describes WebService and its functions, parameters, and return values.
So, how to use WebService to interact with the data of two projects? let's look at a simple example.
First, create a new project myServer, create a new Java class in the project, and develop a WebService-based service, as follows:
Package com.wl.weather
Import javax.jws.WebService
Import javax.xml.ws.Endpoint
@ WebService
Public class WeatherServer {
Public String getWeather (String cityName) {
Switch (cityName) {
Case "Beijing":
Return "sunny"
Case "Wuhan":
Return "Rain"
Default:
Return "unknown location"
}
}
Public static void main (String [] args) {
Endpoint.publish ("http://192.168.52.56:9001/Service/Weather",new WeatherServer ())
System.out.println ("release finished")
}
}
Start the main method in the project.
Then create a new project myClient to invoke the service in myServer. Create a new package com.wl.day23.controller in myClient, then open the terminal window, change to the project directory of myClient in the terminal window, and enter the following command:
Wsimport-s. -p com.wl.day23.controller-keep http://192.168.52.56:9001/Service/Weather?wsdl
If you take a look at the myClient project again, you will find that there are many more interfaces and classes. Regardless of these interfaces and classes, create a new class in the controller package and write the following code:
Package com.wl.day23.controller
Public class IndexController {
Public String list () {
WeatherServer server = new WeatherServerService () .getWeatherServerPort ()
String result = server.getWeather ("Wuhan")
System.out.println (result)
Return "/ list"
}
Public static void main (String [] args) {
New IndexController () .list ()
}
}
Run the main method to see if the data is printed, and if the data is displayed, OK, you have succeeded.
At this point, the study on "using WebService to invoke resources from one project to another" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.