java创建webService接口实列

1.导入相应的maven包

<!--cxf  webservice-->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web-services</artifactId>
</dependency>
<dependency>
    <groupId>org.apache.cxf</groupId>
    <artifactId>cxf-rt-frontend-jaxws</artifactId>
    <version>3.1.1</version>
</dependency>
<dependency>
    <groupId>org.apache.cxf</groupId>
    <artifactId>cxf-rt-transports-http</artifactId>
    <version>3.1.1</version>
</dependency>
<dependency>
    <groupId>org.apache.cxf</groupId>
    <artifactId>cxf-rt-frontend-jaxrs</artifactId>
    <version>3.1.1</version>
</dependency>
<!-- Jetty is needed if you're are not using the CXFServlet -->
<dependency>
    <groupId>org.apache.cxf</groupId>
    <artifactId>cxf-rt-transports-http-jetty</artifactId>
    <version>3.1.1</version>
</dependency>

2.创建接口和其实现类(注意endpointInterface= "com.rc.demo.webservice.say.DemoService" // 接口地址  换成和自己相对应的。

)

package com.rc.demo.webservice.say;

import javax.jws.WebService;

@WebService(name="DemoService" , // 暴露的服务名
		targetNamespace = "http://webService.demo.rc.com" //命名空间,设置为接口的包名倒写(默认是本类包名倒写)
		)

public interface DemoService {
	//做两个测试的接口,一个带参数,一个不带参数。
	public String sayHello(String user);

	public String sayBye();
}
package com.rc.demo.webservice.say;

import java.util.Date;

import javax.jws.WebService;

@WebService(serviceName="DemoService" , //与接口的服务名一致
		targetNamespace="http://webService.demo.rc.com", // 与接口空间 命名一致
		endpointInterface= "com.rc.demo.webservice.say.DemoService" // 接口地址
		)
public class DemoServiceImpl implements DemoService {

	@Override
	public String sayHello(String user) {
		// TODO Auto-generated method stub
		return user+",现在时间:"+"("+new Date()+")";
	}

	@Override
	public String sayBye() {
		// TODO Auto-generated method stub
		return "haha---BayBay";
	}

}

3.创建cxfConfig

package com.rc.demo.config.say;

import javax.xml.ws.Endpoint;

import com.rc.demo.webservice.say.DemoServiceImpl;
import org.apache.cxf.Bus;
import org.apache.cxf.bus.spring.SpringBus;
import org.apache.cxf.jaxws.EndpointImpl;
import org.apache.cxf.transport.servlet.CXFServlet;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;



@Configuration
public class Cxfconfig {

    //访问路径http://localhost:8090/demo/api?wsdl

	@Bean
    public ServletRegistrationBean dispatcherServlet1() {
    // 这个方法名报错了,查了一下解决办法就是改个名,随便加个1就行了。
    //接口路径,可根据实际情况自定义
        return new ServletRegistrationBean(new CXFServlet(),"/demo/*");
    }
 
    @Bean(name = Bus.DEFAULT_BUS_ID)
    public SpringBus springBus() {
        return new SpringBus();
    }
 
 // 此方法为每一个接口的实现类都创建一个
    @Bean
    public Endpoint endpoint() {
        System.out.println("--------------启动项目成功,接口发布开始-------------");
        EndpointImpl endpoint = new EndpointImpl(springBus(), new DemoServiceImpl());
        //endpoint.getInInterceptors().add(new AuthInterceptor()) ; // 添加自己的拦截器,做认证之前可以先注掉。
        endpoint.publish("/api");
        System.out.println("-------------------接口发布成功---------------------");
        return endpoint;

    }
	
}

最后访问地址为(自己本地对应的服务器) 可以使用soap进行解析

http://localhost:8090/demo/api?wsdl

 

访问http://localhost:8090/demo

可以访问到

 

  • 2
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值