当前位置:网站首页>WebService details
WebService details
2022-06-23 02:05:00 【answer】
What is? Webservice
WebService It's a SOA( Service oriented programming ) The architecture of , It is not dependent on language , It doesn't depend on the platform , It can realize the mutual call between different languages , adopt Internet Based on Http The interaction between network applications of the protocol . Actually WebService It's not something mysterious , It is just a class that can be called remotely , Or components , Open your local functions to others .
HttpClient and WebService The difference between
Both of them call the other service interface , The difference lies in :
- HttpClient To call a service , It simulates a browser , send out Http Request , The server will return a response result of the request ,Httpclient Then take out the result of the response .HttpClinet It's like a client , Use Http Protocol calls methods or interfaces in the system .
- webService It's using soap Agreement, not Http agreement .
What is? soap agreement
SOAP Is based on XML A simple agreement , Can make the application in HTTP On the exchange of information . Or more simply :SOAP Is the protocol used to access network services .
SOAP Message real column :
<?xml version="1.0"?>
<soap:Envelope
xmlns:soap="http://www.w3.org/2001/12/soap-envelope"
soap:encodingStyle="http://www.w3.org/2001/12/soap-encoding">
<soap:Header>
...
...
</soap:Header>
<soap:Body>
...
...
<soap:Fault>
...
...
</soap:Fault>
</soap:Body>
</soap:Envelope>xml Detailed explanation of elements :https://www.runoob.com/soap/soap-intro.html
SpringBoot Use CXF Integrate WebService
Add dependency
<!--cxf-->
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-jaxws</artifactId>
<version>3.1.6</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-transports-http</artifactId>
<version>3.1.6</version>
</dependency>
<!--axis-->
<dependency>
<groupId>org.apache.axis</groupId>
<artifactId>axis</artifactId>
<version>1.4</version>
</dependency>
<dependency>
<groupId>org.apache.axis</groupId>
<artifactId>axis-jaxrpc</artifactId>
<version>1.4</version>
</dependency>
<dependency>
<groupId>commons-discovery</groupId>
<artifactId>commons-discovery</artifactId>
<version>0.2</version>
</dependency>
Create server interface
@WebService(name = "DemoWebService", // Expose service name
targetNamespace = "http://service.webservicedemo.example.com"// Namespace , Generally, the package names of interfaces are in reverse order
)
public interface DemoWebService {
String sayHello(String message);
}Server interface implementation
@WebService(
serviceName = "DemoService", // With the specified in the interface name Agreement
targetNamespace = "http://service.webservicedemo.example.com", // Consistent with the namespace in the interface , Generally, the package name of the interface is inverted
endpointInterface = "com.example.webservicedemo.service.DemoWebService" // Address of the interface
)
public class DemoWebServiceImpl implements DemoWebService {
@Override
public String sayHello(String message) {
return message+", present time :"+"("+new Date()+")";
}
}CXF To configure
@Configuration
public class CxfConfig {
@Bean
public ServletRegistrationBean createServletRegistrationBean() {
return new ServletRegistrationBean(new CXFServlet(),"/demo/*");
}
@Bean(name = Bus.DEFAULT_BUS_ID)
public SpringBus springBus() {
return new SpringBus();
}
@Bean
public DemoWebService demoService() {
return new DemoWebServiceImpl();
}
@Bean
public Endpoint endpoint() {
EndpointImpl endpoint = new EndpointImpl(springBus(), demoService());
endpoint.publish("/api");
return endpoint;
}
}start-up SpringBoot service
Input http://localhost:8090/demo/api?wsdl that will do

Use unit tests to simulate the client
Mode one Use cxf Simulation of the request
@SpringBootTest
class WebServiceDemoApplicationTests {
@Test
void contextLoads1() {
// Create a dynamic client
JaxWsDynamicClientFactory factory = JaxWsDynamicClientFactory.newInstance();
Client client = factory.createClient("http://localhost:8090/demo/api?wsdl");
// If you need a password, you need to add a user name and password
//client.getOutInterceptors().add(new ClientLoginInterceptor(USER_NAME,PASS_WORD));
HTTPConduit conduit = (HTTPConduit) client.getConduit();
HTTPClientPolicy httpClientPolicy = new HTTPClientPolicy();
httpClientPolicy.setConnectionTimeout(2000); // Connection timeout
httpClientPolicy.setAllowChunking(false); // Cancel block coding
httpClientPolicy.setReceiveTimeout(120000); // Response timeout
conduit.setClient(httpClientPolicy);
//client.getOutInterceptors().addAll(interceptors);// Set up interceptors
try{
Object[] objects = new Object[0];
// invoke(" Method name ", Parameters 1, Parameters 2, Parameters 3....);
objects = client.invoke("sayHello", "xxx");
System.out.println(" Return the data :" + objects[0]);
}catch (Exception e){
e.printStackTrace();
}
}
}Running results

Mode two Use axis Simulation of the request
@SpringBootTest
class WebServiceDemoApplicationTests {
@Test
void contextLoads2() throws ServiceException, RemoteException, MalformedURLException {
Service service = new Service();
Call call = (Call) service.createCall();
call.setTargetEndpointAddress(new URL("http://localhost:8090/demo/api?wsdl"));
call.setOperationName(new QName("http://service.webservicedemo.example.com","sayHello"));
// call.setUseSOAPAction(true);
// call.setSOAPActionURI("http://service.webservicedemo.example.com"+"sayHello");
call.addParameter(new QName("http://service.webservicedemo.example.com", "message"), XMLType.XSD_STRING, ParameterMode.IN);
call.setReturnType(XMLType.XSD_STRING);
call.setTimeout(10000);
call.setEncodingStyle("utf-8");
// Set the namespace and the method name to be called
Object invoke = call.invoke(new Object[]{"xxx"});
System.out.println(invoke.toString());
}
}Runtime , An element error will be reported
Unexpected elements (uri:"http://service.webservicedemo.example.com", local:"message"). The required element is <{}message>
![]()
You need to expose the interface and parameters of the server .
@WebService(name = "DemoWebService", // Expose service name
targetNamespace = "http://service.webservicedemo.example.com"// Namespace , Generally, the package names of interfaces are in reverse order
)
public interface DemoWebService {
// Expose interfaces Parameters
@WebMethod
String sayHello(@WebParam(name = "message",targetNamespace = "http://service.webservicedemo.example.com") String message);
}Running results

边栏推荐
- Debian10 LVM logical volumes
- Download and compile ROS source code
- Common mistakes in C language (sizeof and strlen)
- Branch and loop statements (including goto statements) -part1
- //1.7 use of escape characters
- There is no corresponding change on the page after the code runs at the Chrome browser break point
- Centos7 installing postgresql12
- Campus network AC authentication failed
- Browser independent way to detect when image has been loaded
- Stop automatically after MySQL starts (unable to start)
猜你喜欢

fatal: refusing to merge unrelated histories

Google account cannot be logged in & external links cannot be opened automatically & words with words cannot be used

Network module packaging

Use elk to save syslog, NetFlow logs and audit network interface traffic

Express framework installation and start service

2021-11-11

5g spectrum

Vs Code inadvertently disable error waveform curve

1. Mx6u bare metal program (1) - Lighting master

1. introduction to MySQL database connection pool function technology points
随机推荐
Analysis of web page status code
Performance test -- 14 detailed explanation of performance test report and precautions
Error in OpenCV image operation: error: (-215:assertion failed)_ src. empty() in function ‘cv::cvtColor‘
Bc113 small leloding alarm clock
1. Mx6u image burning principle (no specific process)
II Data preprocessing
Questions not written in the monthly contest
Nebula operator cloud practice
Branch and loop statements (including goto statements) -part2
How are pub and sub connected in ros1?
1. Mx6u bare metal program (1) - Lighting master
pd. read_ CSV and np Differences between loadtext
Use of higher order functions
Circuit analysis (circuit principle)
5g core network and core network evolution
"Initial C language" (Part 2)
1.3-1.4 web page data capture
How to type Redux actions and Redux reducers in TypeScript?
Uint8 serializing and deserializing pits using stringstream
Google account cannot be logged in & external links cannot be opened automatically & words with words cannot be used