webService使用教程

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

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值