Springboot之webservice

本文将详细介绍如何使用Springboot搭建Webservice服务:从创建Maven项目、引入依赖,到定义Webservice接口及其实现,再到配置类的编写,最后启动Springboot应用,实现Webservice功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

下面记录并分享一版用springboot写的webservice实例:

1、新建一个maven项目,并加入依赖

<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
		<java.version>1.8</java.version>
	</properties>

	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
		<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>
	</dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>

2、新建一个webiservice接口

package com.web.webservice;

import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService;
/**
* author:wxy_jdhk
*/
@WebService
public interface ISendMessage {
	@WebMethod
	public String send(@WebParam(name = "data") String data);
}

3、新建一个webservice接口的实现类

package com.web.webservice.impl;
import javax.jws.WebService;
import com.example.demo.web.webservice.ISendMessage;
/**
* author:wxy_jdhk
*/
@WebService(targetNamespace="http://webservice.web.com/",endpointInterface = "com.web.webservice.ISendMessage")
public class SendMessage implements ISendMessage{
	@Override
	public String send(String data) {
		return data;
	}
}

4、新建一个配置类

package com.example.demo.web.webservice.config;

import javax.xml.ws.Endpoint;

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;
import org.springframework.core.Ordered;

import com.example.demo.web.webservice.ISendMessage;
import com.example.demo.web.webservice.impl.SendMessage;
/**
* author:wxy_jdhk
*/
@Configuration
public class ReleaseWebservice {
	 @Bean
	 public ServletRegistrationBean cxfServlet() {
		 ServletRegistrationBean bean = new ServletRegistrationBean(new CXFServlet(), "/webservice/*");
		 bean.setLoadOnStartup(0);
		 bean.setOrder(Ordered.HIGHEST_PRECEDENCE);
		 return bean;
	 }
	 @Bean(name = Bus.DEFAULT_BUS_ID)
	 public SpringBus springBus() {
		 return new SpringBus();
	 }
	 @Bean
	 public ISendMessage sendService() {
		 return new SendMessage();
	 }
	 @Bean
	 public Endpoint endpoint() {
		 EndpointImpl endpoint = new EndpointImpl(springBus(), sendService());
		 endpoint.publish("/send");
		 return endpoint;
	 }
	
}

5、最后新建一个springboot启动类

package com.main;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
*	author:wxy_jdhk
*/
@SpringBootApplication
public class SpringbootWebserviceApplication {
	public static void main(String[] args) {
		SpringApplication.run(SpringbootWebserviceApplication.class, args);
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值