java对外提供服务接口如何写?

讨论了Java对外提供服务接口的不同方式,包括使用HTTP+JSON、RESTful、RPC等方法,并探讨了安全性和加密措施。
java对外提供服务接口如何写? 终于搞明白,存储TCO原来是这样算的>>>    java对外提供服务接口如何写? 跨系统,跨客户端,安全性要求高。webserice? rmi ?还有其他的解决方式吗?
zzuqiang
发帖于3年前  10回/28K+阅
zzuqiang
共有10个答案  最后回答: 3年前
0

Thrift

Protobuf


评论(0) 引用此答案 举报
excepiton
0

http://git.oschina.net/fengfei/feige-rpc

看看这个

--- 共有 1 条评论 ---
zzuqiang:  好的,谢谢你!  3年前
评论(1) 引用此答案 举报 来自 Android
小飞有点不高兴
0
restful--- 共有 1 条评论 ---
zzuqiang:  如何实现呢?有具体的方案和应用demo  3年前
评论(1) 引用此答案 举报
jack_jones
0
感觉你要的是通讯报文。http和socket都能满足你的要求。 评论(0) 引用此答案 举报
零下三度
0
个人接触过的接口,基本都是  http post方式,要么就是webservice,再加上一些加密,基本都可以满足了~~--- 共有 2 条评论 ---
zzuqiang:  是需要一些加解密  3年前
zzuqiang:  点击此处输入评论  3年前
评论(2) 引用此答案 举报
咖啡加糖
0
引用来自“jack_jones”的评论
restful
增加用户,put请求 
接口 
http://xxx.com/user 


删除用户,del请求 
接口 
http://xxx.com/user/{id} 


修改用户,post请求 
接口 
http://www.com/user 


查询用户,get请求 
接口 
http://xxx.com/user 


如果要用restful,需要了解put、delete、post、get请求的区别 
我用过jersey实现过,具体demo去看看官网的 
评论(0) 引用此答案 举报
jack_jones
0
HTTP + JSON 评论(0) 引用此答案 举报
dreamers
0
这个得看你的系统是一个中间平台,还是仅仅是一个子系统,很多种通信方式http(简单明了), webservice(传统的方式), socket,至于要安全,那你可能得加上签名,加密或者证书之类的。 评论(0) 引用此答案 举报
ihaolin
0

简单的servlet+json足矣

复杂的RPC,RESTFUL

--- 共有 1 条评论 ---
zzuqiang:  你说的很对,谢谢!  3年前
评论(1) 引用此答案 举报
roywang
0
方法太多了 评论(0) 引用此答案 举报
Java Spring Boot 应用中提供对外接口,通常需要考虑接口的定义、安全性和调用方式。以下是实现对外接口的关键步骤和相关代码示例。 ### 定义对外接口 首先,定义一个接口来声明对外暴露的方法。这可以通过创建一个 `@FeignClient` 注解的接口来实现,或者直接使用 `RestTemplate` 或 `WebClient` 来调用外部服务。 ```java import org.springframework.cloud.openfeign.FeignClient; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; @FeignClient(name = "external-service", url = "http://external-service.com/api") public interface ExternalServiceClient { @PostMapping("/process") String processRequest(@RequestBody TestDto testDto); } ``` ### 实现接口服务 接下来,创建一个服务类来实现接口中定义的方法。这个类可以包含具体的业务逻辑,并调用外部系统的接口。 ```java import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @Service public class ExternalServiceClientImpl implements ExternalServiceClient { @Autowired private PrintService printService; @Override public String processRequest(@RequestBody TestDto testDto) { return printService.print(testDto); } } ``` ### 调用外部接口 在 Spring Boot 中,可以使用 `RestTemplate` 或 `WebClient` 来调用外部接口。`RestTemplate` 是同步的,而 `WebClient` 是异步的,适用于非阻塞 I/O。 #### 使用 RestTemplate 调用外部接口 ```java import org.springframework.web.client.RestTemplate; public class ExternalServiceCaller { private final RestTemplate restTemplate = new RestTemplate(); public String callExternalService(String url, TestDto testDto) { return restTemplate.postForObject(url, testDto, String.class); } } ``` #### 使用 WebClient 调用外部接口 ```java import org.springframework.web.reactive.function.client.WebClient; public class ExternalServiceCaller { private final WebClient webClient = WebClient.create("http://external-service.com"); public String callExternalService(TestDto testDto) { return webClient.post() .uri("/api/process") .bodyValue(testDto) .retrieve() .bodyToMono(String.class) .block(); } } ``` ### 接口安全性 为了确保接口的安全性,可以采用以下措施: - **身份认证**:使用 OAuth2 或 JWT 来进行身份验证。 - **权限控制**:通过 Spring Security 来控制访问权限。 - **数据加密**:使用 HTTPS 来加密传输数据。 - **防止攻击**:通过限制请求频率和验证输入数据来防止常见攻击。 #### 使用 Spring Security 进行权限控制 ```java import org.springframework.context.annotation.Configuration; import org.springframework.security.config.annotation.web.builders.HttpSecurity; import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; @Configuration @EnableWebSecurity public class SecurityConfig extends WebSecurityConfigurerAdapter { @Override protected void configure(HttpSecurity http) throws Exception { http .authorizeRequests() .antMatchers("/api/**").authenticated() .and() .httpBasic() .and() .csrf().disable(); } } ``` ### 系统逻辑分析 在 Spring Boot 中提供对外接口时,系统逻辑主要涉及以下几个方面: 1. **接口定义**:明确对外暴露的功能,并通过 `@FeignClient` 或 `RestTemplate` 进行定义。 2. **服务实现**:实现接口的具体逻辑,并确保与外部系统的兼容性。 3. **安全控制**:通过身份认证和权限控制来保证接口的安全性。 4. **调用方式**:选择合适的调用方式(同步或异步)来提高性能和可扩展性。 通过以上步骤,可以在 Spring Boot 应用中有效地提供对外接口,并确保其安全性和可维护性。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值