mirror of
https://gitee.com/durcframework/SOP.git
synced 2025-08-11 12:56:28 +08:00
添加easyopen例子
This commit is contained in:
25
sop-example/sop-easyopen/.gitignore
vendored
Normal file
25
sop-example/sop-easyopen/.gitignore
vendored
Normal file
@@ -0,0 +1,25 @@
|
||||
target/
|
||||
!.mvn/wrapper/maven-wrapper.jar
|
||||
|
||||
### STS ###
|
||||
.apt_generated
|
||||
.classpath
|
||||
.factorypath
|
||||
.project
|
||||
.settings
|
||||
.springBeans
|
||||
|
||||
### IntelliJ IDEA ###
|
||||
.idea
|
||||
*.iws
|
||||
*.iml
|
||||
*.ipr
|
||||
|
||||
### NetBeans ###
|
||||
nbproject/private/
|
||||
build/
|
||||
nbbuild/
|
||||
dist/
|
||||
nbdist/
|
||||
.nb-gradle/
|
||||
/local-config/
|
108
sop-example/sop-easyopen/pom.xml
Normal file
108
sop-example/sop-easyopen/pom.xml
Normal file
@@ -0,0 +1,108 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<groupId>com.gitee.sop</groupId>
|
||||
<artifactId>sop-easyopen</artifactId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<name>easyopen-server-empty</name>
|
||||
<description>easyopen Demo project for Spring Boot</description>
|
||||
|
||||
<parent>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-parent</artifactId>
|
||||
<version>2.1.2.RELEASE</version>
|
||||
<relativePath /> <!-- lookup parent from repository -->
|
||||
</parent>
|
||||
|
||||
<properties>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
|
||||
<spring-cloud.version>Greenwich.RELEASE</spring-cloud.version>
|
||||
<java.version>1.8</java.version>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<!-- sop接入依赖 -->
|
||||
<dependency>
|
||||
<groupId>com.gitee.sop</groupId>
|
||||
<artifactId>sop-service-common</artifactId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>net.oschina.durcframework</groupId>
|
||||
<artifactId>easyopen</artifactId>
|
||||
<version>1.16.1</version>
|
||||
</dependency>
|
||||
<!-- sop接入依赖 end -->
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-thymeleaf</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-data-redis</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-configuration-processor</artifactId>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-devtools</artifactId>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-test</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
<dependencyManagement>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-dependencies</artifactId>
|
||||
<version>${spring-cloud.version}</version>
|
||||
<type>pom</type>
|
||||
<scope>import</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</dependencyManagement>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
</plugin>
|
||||
<!-- 打包时跳过测试 -->
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-surefire-plugin</artifactId>
|
||||
<configuration>
|
||||
<skipTests>true</skipTests>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
|
||||
</project>
|
31
sop-example/sop-easyopen/readme.md
Normal file
31
sop-example/sop-easyopen/readme.md
Normal file
@@ -0,0 +1,31 @@
|
||||
# easyopen-server
|
||||
|
||||
接口服务器空项目,可在此基础上做开发。
|
||||
|
||||
入口类:IndexController
|
||||
|
||||
业务类在api下,重点关注这里即可。
|
||||
|
||||
```
|
||||
└─com.gitee.easyopen.server
|
||||
│ EmptySpringbootApplication.java // springboot启动类
|
||||
│ IndexController.java // easyopen入口类
|
||||
│
|
||||
├─api // 存放接口
|
||||
│ │ GoodsApi.java // 示例接口
|
||||
│ │
|
||||
│ ├─param // 存放接口参数类
|
||||
│ │ GoodsParam.java
|
||||
│ │
|
||||
│ └─result // 存放放回接口类
|
||||
│ Goods.java
|
||||
│
|
||||
├─config // spring配置
|
||||
│ CorsConfig.java
|
||||
│
|
||||
├─interceptor // easyopen拦截器
|
||||
│ LogInterceptor.java
|
||||
│
|
||||
└─message // 错误处理
|
||||
CommonErrors.java
|
||||
```
|
@@ -0,0 +1,25 @@
|
||||
package com.gitee.easyopen.server;
|
||||
|
||||
import com.gitee.easyopen.ApiConfig;
|
||||
import com.gitee.easyopen.support.ApiController;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* 文档地址:http://localhost:8080/api/doc
|
||||
*/
|
||||
@Controller
|
||||
@RequestMapping("api")
|
||||
public class IndexController extends ApiController {
|
||||
@Override
|
||||
protected void initApiConfig(ApiConfig apiConfig) {
|
||||
apiConfig.setShowDoc(true); // 显示文档页面
|
||||
// 配置国际化消息
|
||||
Map<String, String> appSecretStore = new HashMap<String, String>();
|
||||
appSecretStore.put("test", "123456");
|
||||
apiConfig.addAppSecret(appSecretStore);
|
||||
}
|
||||
}
|
@@ -0,0 +1,14 @@
|
||||
package com.gitee.easyopen.server;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
|
||||
|
||||
@EnableDiscoveryClient
|
||||
@SpringBootApplication
|
||||
public class SopEasyopenApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(SopEasyopenApplication.class, args);
|
||||
}
|
||||
}
|
@@ -0,0 +1,31 @@
|
||||
package com.gitee.easyopen.server.api;
|
||||
|
||||
import com.gitee.easyopen.annotation.Api;
|
||||
import com.gitee.easyopen.annotation.ApiService;
|
||||
import com.gitee.easyopen.doc.annotation.ApiDoc;
|
||||
import com.gitee.easyopen.doc.annotation.ApiDocMethod;
|
||||
import com.gitee.easyopen.server.api.param.GoodsParam;
|
||||
import com.gitee.easyopen.server.api.result.Goods;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
|
||||
/**
|
||||
* 业务类
|
||||
*
|
||||
* @author tanghc
|
||||
*/
|
||||
@ApiService
|
||||
@ApiDoc("商品模块")
|
||||
public class GoodsApi {
|
||||
|
||||
@Api(name = "goods.get")
|
||||
@ApiDocMethod(description = "获取商品")
|
||||
Goods getGoods(GoodsParam param) {
|
||||
Goods goods = new Goods();
|
||||
goods.setId(1L);
|
||||
goods.setGoods_name("苹果iPhoneX");
|
||||
goods.setPrice(new BigDecimal(8000));
|
||||
return goods;
|
||||
}
|
||||
|
||||
}
|
@@ -0,0 +1,23 @@
|
||||
package com.gitee.easyopen.server.api.param;
|
||||
|
||||
import com.gitee.easyopen.doc.annotation.ApiDocField;
|
||||
import org.hibernate.validator.constraints.Length;
|
||||
|
||||
import javax.validation.constraints.NotEmpty;
|
||||
|
||||
public class GoodsParam {
|
||||
|
||||
@ApiDocField(description = "商品名称", required = true, example = "iphoneX")
|
||||
@NotEmpty(message = "商品名称不能为空")
|
||||
@Length(min = 3, max = 20, message = "{goods.name.length}=3,20")
|
||||
private String goods_name;
|
||||
|
||||
public String getGoods_name() {
|
||||
return goods_name;
|
||||
}
|
||||
|
||||
public void setGoods_name(String goods_name) {
|
||||
this.goods_name = goods_name;
|
||||
}
|
||||
|
||||
}
|
@@ -0,0 +1,46 @@
|
||||
package com.gitee.easyopen.server.api.result;
|
||||
|
||||
import com.gitee.easyopen.doc.DataType;
|
||||
import com.gitee.easyopen.doc.annotation.ApiDocField;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
|
||||
public class Goods {
|
||||
|
||||
@ApiDocField(description = "id")
|
||||
private Long id;
|
||||
@ApiDocField(description = "商品名称")
|
||||
private String goods_name;
|
||||
@ApiDocField(description = "价格", dataType = DataType.FLOAT)
|
||||
private BigDecimal price;
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getGoods_name() {
|
||||
return goods_name;
|
||||
}
|
||||
|
||||
public void setGoods_name(String goods_name) {
|
||||
this.goods_name = goods_name;
|
||||
}
|
||||
|
||||
public BigDecimal getPrice() {
|
||||
return price;
|
||||
}
|
||||
|
||||
public void setPrice(BigDecimal price) {
|
||||
this.price = price;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Goods [id=" + id + ", goods_name=" + goods_name + ", price=" + price + "]";
|
||||
}
|
||||
|
||||
}
|
@@ -0,0 +1,37 @@
|
||||
package com.gitee.easyopen.server.config;
|
||||
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.web.cors.CorsConfiguration;
|
||||
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
|
||||
import org.springframework.web.filter.CorsFilter;
|
||||
|
||||
/**
|
||||
* 解决js跨域
|
||||
* @author tanghc
|
||||
*/
|
||||
@Configuration
|
||||
public class CorsConfig {
|
||||
|
||||
/**
|
||||
* 跨域过滤器
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
@Bean
|
||||
public CorsFilter corsFilter() {
|
||||
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
|
||||
source.registerCorsConfiguration("/**", buildConfig()); // 4
|
||||
return new CorsFilter(source);
|
||||
}
|
||||
|
||||
private CorsConfiguration buildConfig() {
|
||||
CorsConfiguration corsConfiguration = new CorsConfiguration();
|
||||
corsConfiguration.addAllowedOrigin("*");
|
||||
corsConfiguration.addAllowedHeader("*");
|
||||
corsConfiguration.addAllowedMethod("*");
|
||||
return corsConfiguration;
|
||||
}
|
||||
|
||||
|
||||
}
|
@@ -0,0 +1,11 @@
|
||||
package com.gitee.easyopen.server.config;
|
||||
|
||||
import com.gitee.sop.servercommon.configuration.EasyopenServiceConfiguration;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
/**
|
||||
* @author tanghc
|
||||
*/
|
||||
@Configuration
|
||||
public class SopConfig extends EasyopenServiceConfiguration {
|
||||
}
|
@@ -0,0 +1,23 @@
|
||||
package com.gitee.easyopen.server.interceptor;
|
||||
|
||||
import com.gitee.easyopen.ApiContext;
|
||||
import com.gitee.easyopen.interceptor.ApiInterceptorAdapter;
|
||||
import com.gitee.easyopen.util.RequestUtil;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.net.URLDecoder;
|
||||
|
||||
public class LogInterceptor extends ApiInterceptorAdapter {
|
||||
|
||||
Logger logger = LoggerFactory.getLogger(getClass());
|
||||
|
||||
@Override
|
||||
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object serviceObj, Object argu)
|
||||
throws Exception {
|
||||
logger.info("收到客户端请求,ip={},参数={}", RequestUtil.getClientIP(request), URLDecoder.decode(ApiContext.getApiParam().toJSONString(), "UTF-8"));
|
||||
return true;
|
||||
}
|
||||
}
|
@@ -0,0 +1,16 @@
|
||||
package com.gitee.easyopen.server.message;
|
||||
|
||||
import com.gitee.easyopen.message.ErrorMeta;
|
||||
|
||||
/**
|
||||
* 错误码放在这里
|
||||
*
|
||||
* 使用方式:throw CommonErrors.NUll_ERROR.getException();
|
||||
*/
|
||||
public interface CommonErrors {
|
||||
String isvModule = "isv.error_"; // error_zh_CN2.properties内容前缀
|
||||
|
||||
ErrorMeta NUll_ERROR = new ErrorMeta(isvModule, "200", "不能为空");
|
||||
|
||||
|
||||
}
|
26
sop-example/sop-easyopen/src/main/resources/application.yml
Normal file
26
sop-example/sop-easyopen/src/main/resources/application.yml
Normal file
@@ -0,0 +1,26 @@
|
||||
server:
|
||||
port: 2121
|
||||
|
||||
|
||||
|
||||
# 指定注册中心
|
||||
eureka:
|
||||
port: 1111
|
||||
host: localhost
|
||||
client:
|
||||
serviceUrl:
|
||||
defaultZone: http://${eureka.host}:${eureka.port}/eureka/
|
||||
|
||||
spring:
|
||||
application:
|
||||
# 指定服务名
|
||||
name: easyopen-service
|
||||
|
||||
# 指定zookeeper
|
||||
cloud:
|
||||
zookeeper:
|
||||
connect-string: localhost:2181
|
||||
|
||||
# 指定接口路径
|
||||
easyopen:
|
||||
prefix-path: /api
|
@@ -0,0 +1,2 @@
|
||||
# common errors here
|
||||
isv.error_200=\u4E0D\u80FD\u4E3A\u7A7A
|
@@ -0,0 +1,16 @@
|
||||
package com.gitee.durcframework.easyopen.server;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest
|
||||
public class SopEasyopenApplicationApplicationTests {
|
||||
|
||||
@Test
|
||||
public void contextLoads() {
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user