1.pom.xml
<!--Web service-->
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-jaxws</artifactId>
<version>3.2.6</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-transports-http</artifactId>
<version>3.2.6</version>
</dependency>
2.configs
@Configuration
public class WebServiceConfig {
@Bean
public ServletRegistrationBean dispatcherServlet() {
return new ServletRegistrationBean<>(new CXFServlet(), "/nangang/*");
}
@Bean(name = Bus.DEFAULT_BUS_ID)
public SpringBus springBus() {
return new SpringBus();
}
@Bean
public webService webService() {
return new WebServiceController();
}
@Bean
public Endpoint endpoint() {
EndpointImpl endpoint = new EndpointImpl(springBus(), webService());
endpoint.publish("/NG");
return endpoint;
}
}
3.service
@WebService(name = "NGService",targetNamespace = "http://webservice.demo.example.com")
public interface webService {
@WebMethod
String search(@WebParam(name = "time1") String time1, @WebParam(name = "time2") String time2);
}
4.controller
@Component
public class WebServiceController implements webService {
@Autowired
private WebSocket WebSocket;
@Autowired
private RedisConfig redisConfig;
//传来的时间
@Override
public String search(String time1, String time2) {
System.out.println(time1 + "@@@@@" + time2);
//传入redis
boolean time11 = redisConfig.set("time1", time1);
boolean time21 = redisConfig.set("time2", time2);
try {
//WebSocket.onOpw();
WebSocket.onMessage(time1 + "@@@@@" + time2);
} catch (Exception e) {
//e.printStackTrace();
System.out.println("WebSocket发送失败");
return "WebSocket发送失败";
}
return "ok";
}
}
5.Application
// 使用http请求和webservice请求时,会发生冲突,导致扫描不到controller包,要在application中用bean方法注入controller
@Bean
public ServletRegistrationBean restServlet(){
//注解扫描上下文
AnnotationConfigWebApplicationContext applicationContext = new AnnotationConfigWebApplicationContext();
//base package
applicationContext.scan("com.example.nangang.nangang.controller");
//通过构造函数指定dispatcherServlet的上下文
DispatcherServlet rest_dispatcherServlet = new DispatcherServlet(applicationContext);
//用ServletRegistrationBean包装servlet
ServletRegistrationBean registrationBean = new ServletRegistrationBean(rest_dispatcherServlet);
registrationBean.setLoadOnStartup(1);
//指定urlmapping
registrationBean.addUrlMappings("/*");
//指定name,如果不指定默认为dispatcherServlet
registrationBean.setName("rest");
return registrationBean;
}
http://localhost:8998/nangang/NG?wsdl