SpringBoot與WebService的簡單實現
SpringBoot與JAX-WS
服務端
依賴
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-spring-boot-starter-jaxws</artifactId>
<version>3.4.1</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
介面
import javax.jws.WebService;
import java.util.List;
@WebService(targetNamespace = "http://localhost")
public interface UserService {
void saveUser(User user);
void updateUser(User user);
void deleteUser(Integer id);
List<User> findAllUser();
User findById(Integer id);
}
假裝實現
@Service
public class UserServiceImpl implements UserService {
@Override
public void saveUser(User user) {
System.out.println("儲存使用者:" + user.getId() + "----" + user.getName() + "----" + user.getSex());
}
@Override
public void updateUser(User user) {
System.out.println("修改使用者:" + user.getId() + "----" + user.getName() + "----" + user.getSex());
}
@Override
public void deleteUser(Integer id) {
System.out.println("刪除使用者:" + id);
}
@Override
public List<User> findAllUser() {
User user1 = new User(1,"1",1);
User user2 = new User(2,"2",2);
User user3 = new User(3,"2",3);
List<User> users = new ArrayList<>();
users.add(user1);
users.add(user2);
users.add(user3);
return users;
}
@Override
public User findById(Integer id) {
return new User(1,"1",1);
}
}
配置類
@Configuration
public class JaxWsConfig {
@Autowired
private Bus bus;
@Autowired
private UserService userService;
@Bean
public Endpoint createEndpoint(){
Endpoint endpoint = new EndpointImpl(bus,userService);
endpoint.publish("/userService");
return endpoint;
}
}
客戶端
沒有介面的實現類和釋出服務的功能,只有實體類和介面,並且與服務端一樣
訪問服務端:
@Test
void contextLoads() {
//建立工廠物件
JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
//設定訪問路徑
factory.setAddress("http://localhost:8080/services/userService");
//設定介面
factory.setServiceClass(UserService.class);
//建立介面的代理類
UserService userService = (UserService) factory.create();
//呼叫服務端介面
userService.saveUser(new User(1l,"烏托邦",2));
}
服務端列印輸出了證明成功
SpringBoot與JAX-RS
依賴
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-spring-boot-starter-jaxrs</artifactId>
<version>3.4.1</version>
</dependency>
<!--json轉換-->
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-rs-extension-providers</artifactId>
<version>3.4.1</version>
</dependency>
<dependency>
<groupId>org.codehaus.jettison</groupId>
<artifactId>jettison</artifactId>
<version>1.4.1</version>
</dependency>
restful風格service,
public interface UserService {
@POST
void saveUser(User user);
@PUT
void updateUser(User user);
@DELETE
@Path("/{id}")
void deleteUser(@PathParam("id") Integer id);
@GET
List<User> findAllUser();
@GET
@Path("/{id}")
//轉json
@Produces(MediaType.APPLICATION_JSON)
User findById(@PathParam("id")Integer id);
}
實現類跟ws的一樣
配置類
@Configuration
public class JaxRsConfig {
@Autowired
private Bus bus;
@Autowired
private UserService userService;
@Bean
public Server createdServer(){
JAXRSServerFactoryBean endpoint = new JAXRSServerFactoryBean();
//設定訪問地址
endpoint.setAddress("/userService");
//設定bus
endpoint.setBus(bus);
//設定實體類物件
endpoint.setServiceBean(userService);
return endpoint.create();
}
}
瀏覽器訪問
相關文章
- WebService就是這麼簡單Web
- SpringBoot+webserviceSpring BootWeb
- redux簡單實現與分析Redux
- SpringBoot整合MongoDB(實現一個簡單快取)Spring BootMongoDB快取
- 虛擬Dom與Diff的簡單實現
- Servlet實現、與html的簡單互動ServletHTML
- SpringBoot基於資料庫實現簡單的分散式鎖Spring Boot資料庫分散式
- 好程式設計師分享WebService的簡單使用程式設計師Web
- 在 SpringBoot 專案中簡單實現 JWT 驗證Spring BootJWT
- springboot2.0整合webserviceSpring BootWeb
- springboot-stater + redis + lua 實現一個簡單的發號器(3)-- 實現篇Spring BootRedis
- webservice簡介Web
- virtual-dom原理與簡單實現
- AOP的簡單實現
- 簡單的 HashMap 實現HashMap
- 實現簡單的BitMap
- ArrayList的簡單實現
- SpringBoot2 整合 WebSocket 簡單實現聊天室功能Spring BootWeb
- WebService安全機制的思考與實踐Web
- SpringBoot 實現傳送簡訊Spring Boot
- PostgreSQLlibpqmulti-host配置與簡單HA實現SQL
- 簡單實現.NET Hook與事件模擬Hook事件
- springboot簡單的專案Spring Boot
- C#中WebService的建立、部署和呼叫的簡單例項C#Web單例
- 簡單的實現vue原理Vue
- 簡單的實現React原理React
- [Linux]簡單的shell實現Linux
- java實現簡單的JDBCJavaJDBC
- SpringBoot基礎學習(一) SpringBoot概念、簡單案例實現、單元測試及熱部署講解Spring Boot熱部署
- SpringBoot簡單梳理Spring Boot
- Proteus實現簡單51程式的設計與模擬
- Promise 簡單實現Promise
- ReadableStream 簡單實現
- Express 簡單實現Express
- AspectJ簡單實現
- FastClick簡單實現AST
- Linux下實現 OpenSSL 簡單加密與解密字串Linux加密解密字串
- 用setTimeout和clearTimeout簡單實現setInterval與clearInterval