添加example

1.x
tanghc 6 years ago
parent 77cfec981c
commit 78f85256d6
  1. 15
      sop-example/sop-book/pom.xml
  2. 25
      sop-example/sop-book/sop-book-api/.gitignore
  3. 46
      sop-example/sop-book/sop-book-api/pom.xml
  4. 12
      sop-example/sop-book/sop-book-api/src/main/java/com/hhdd/book/api/domain/Book.java
  5. 14
      sop-example/sop-book/sop-book-api/src/main/java/com/hhdd/book/api/service/BookService.java
  6. 25
      sop-example/sop-book/sop-book-web/.gitignore
  7. 89
      sop-example/sop-book/sop-book-web/pom.xml
  8. 20
      sop-example/sop-book/sop-book-web/src/main/java/com/gitee/sop/bookweb/SopBookApplication.java
  9. 22
      sop-example/sop-book/sop-book-web/src/main/java/com/gitee/sop/bookweb/config/OpenServiceConfig.java
  10. 14
      sop-example/sop-book/sop-book-web/src/main/java/com/gitee/sop/bookweb/consumer/StoryServiceConsumer.java
  11. 40
      sop-example/sop-book/sop-book-web/src/main/java/com/gitee/sop/bookweb/controller/AlipayBookController.java
  12. 74
      sop-example/sop-book/sop-book-web/src/main/java/com/gitee/sop/bookweb/controller/ApiResult.java
  13. 73
      sop-example/sop-book/sop-book-web/src/main/java/com/gitee/sop/bookweb/controller/BookController.java
  14. 11
      sop-example/sop-book/sop-book-web/src/main/java/com/gitee/sop/bookweb/param/BookParam.java
  15. 9
      sop-example/sop-book/sop-book-web/src/main/resources/application.yml
  16. 7
      sop-example/sop-book/sop-book-web/src/main/resources/bootstrap.yml
  17. 17
      sop-example/sop-book/sop-book-web/src/test/java/com/gitee/sop/bookweb/SopBookApplicationTests.java
  18. 15
      sop-example/sop-story/pom.xml
  19. 25
      sop-example/sop-story/sop-story-api/.gitignore
  20. 46
      sop-example/sop-story/sop-story-api/pom.xml
  21. 18
      sop-example/sop-story/sop-story-api/src/main/java/com/gitee/sop/story/api/domain/Story.java
  22. 15
      sop-example/sop-story/sop-story-api/src/main/java/com/gitee/sop/story/api/service/StoryService.java
  23. 25
      sop-example/sop-story/sop-story-web/.gitignore
  24. 84
      sop-example/sop-story/sop-story-web/pom.xml
  25. 16
      sop-example/sop-story/sop-story-web/src/main/java/com/gitee/sop/bookweb/SopStoryApplication.java
  26. 31
      sop-example/sop-story/sop-story-web/src/main/java/com/gitee/sop/bookweb/config/OpenServiceConfig.java
  27. 27
      sop-example/sop-story/sop-story-web/src/main/java/com/gitee/sop/bookweb/controller/AlipayController.java
  28. 18
      sop-example/sop-story/sop-story-web/src/main/java/com/gitee/sop/bookweb/controller/JSR303DemoController.java
  29. 46
      sop-example/sop-story/sop-story-web/src/main/java/com/gitee/sop/bookweb/controller/Story2Controller.java
  30. 85
      sop-example/sop-story/sop-story-web/src/main/java/com/gitee/sop/bookweb/controller/StoryController.java
  31. 21
      sop-example/sop-story/sop-story-web/src/main/java/com/gitee/sop/bookweb/controller/StoryDemoController.java
  32. 33
      sop-example/sop-story/sop-story-web/src/main/java/com/gitee/sop/bookweb/controller/ThrowExceptionDemoController.java
  33. 19
      sop-example/sop-story/sop-story-web/src/main/java/com/gitee/sop/bookweb/controller/param/GoodsParam.java
  34. 9
      sop-example/sop-story/sop-story-web/src/main/java/com/gitee/sop/bookweb/controller/param/GoodsUpdateParam.java
  35. 20
      sop-example/sop-story/sop-story-web/src/main/java/com/gitee/sop/bookweb/message/GoodsErrorEnum.java
  36. 21
      sop-example/sop-story/sop-story-web/src/main/java/com/gitee/sop/bookweb/message/StoryErrorEnum.java
  37. 9
      sop-example/sop-story/sop-story-web/src/main/resources/application.yml
  38. 8
      sop-example/sop-story/sop-story-web/src/main/resources/bootstrap.yml
  39. 10
      sop-example/sop-story/sop-story-web/src/main/resources/i18n/isp/bizerror_en.properties
  40. 14
      sop-example/sop-story/sop-story-web/src/main/resources/i18n/isp/bizerror_zh_CN.properties
  41. 2
      sop-example/sop-story/sop-story-web/src/main/resources/i18n/isp/goods_error_en.properties
  42. 5
      sop-example/sop-story/sop-story-web/src/main/resources/i18n/isp/goods_error_zh_CN.properties
  43. 17
      sop-example/sop-story/sop-story-web/src/test/java/com/gitee/sop/bookweb/SopStoryApplicationTests.java

@ -0,0 +1,15 @@
<?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-book-parent</artifactId>
<version>1.0.0-SNAPSHOT</version>
<packaging>pom</packaging>
<modules>
<module>sop-book-api</module>
<module>sop-book-web</module>
</modules>
</project>

@ -0,0 +1,25 @@
/target/
!.mvn/wrapper/maven-wrapper.jar
### STS ###
.apt_generated
.classpath
.factorypath
.project
.settings
.springBeans
.sts4-cache
### IntelliJ IDEA ###
.idea
*.iws
*.iml
*.ipr
### NetBeans ###
/nbproject/private/
/nbbuild/
/dist/
/nbdist/
/.nb-gradle/
/build/

@ -0,0 +1,46 @@
<?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>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.2.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.gitee.sop</groupId>
<artifactId>sop-book-api</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Greenwich.RELEASE</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.4</version>
<scope>provided</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>
</project>

@ -0,0 +1,12 @@
package com.hhdd.book.api.domain;
import lombok.Data;
/**
* @author tanghc
*/
@Data
public class Book {
private int id;
private String name;
}

@ -0,0 +1,14 @@
package com.hhdd.book.api.service;
import com.gitee.book.api.domain.Book;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
/**
* @author tanghc
*/
@RequestMapping("/book")
public interface BookService {
@RequestMapping("/getBook")
Book getBook(@RequestParam("id") int id);
}

@ -0,0 +1,25 @@
/target/
!.mvn/wrapper/maven-wrapper.jar
### STS ###
.apt_generated
.classpath
.factorypath
.project
.settings
.springBeans
.sts4-cache
### IntelliJ IDEA ###
.idea
*.iws
*.iml
*.ipr
### NetBeans ###
/nbproject/private/
/nbbuild/
/dist/
/nbdist/
/.nb-gradle/
/build/

@ -0,0 +1,89 @@
<?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>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.2.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.gitee.sop</groupId>
<artifactId>sop-book-web</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>sop-story</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Greenwich.RELEASE</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>com.gitee.sop</groupId>
<artifactId>sop-service-common</artifactId>
<version>1.0.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.gitee.sop</groupId>
<artifactId>sop-book-api</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.gitee.sop</groupId>
<artifactId>sop-story-api</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.4</version>
<scope>compile</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>
</plugins>
</build>
<repositories>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
</repository>
</repositories>
</project>

@ -0,0 +1,20 @@
package com.gitee.sop.bookweb;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
// 允许调用其他服务
@EnableFeignClients
// 服务注册
@EnableDiscoveryClient
@SpringBootApplication
public class SopBookApplication {
public static void main(String[] args) {
SpringApplication.run(SopBookApplication.class, args);
}
}

@ -0,0 +1,22 @@
package com.gitee.sop.bookweb.config;
import com.gitee.sop.servercommon.configuration.AlipayServiceConfiguration;
import org.springframework.context.annotation.Configuration;
/**
* 使用支付宝开放平台功能
* @author tanghc
*/
@Configuration
public class OpenServiceConfig extends AlipayServiceConfiguration {
}
/**
* 使用淘宝开放平台功能
* @author tanghc
*/
//@Configuration
//public class OpenServiceConfig extends TaobaoServiceConfiguration {
//
//}

@ -0,0 +1,14 @@
package com.gitee.sop.bookweb.consumer;
import com.gitee.sop.story.api.service.StoryService;
import org.springframework.cloud.openfeign.FeignClient;
/**
* 调用story服务
*
* @author tanghc
*/
// value对应的spring.application.name
@FeignClient("story-service")
public interface StoryServiceConsumer extends StoryService {
}

@ -0,0 +1,40 @@
package com.gitee.sop.bookweb.controller;
import com.gitee.sop.bookweb.consumer.StoryServiceConsumer;
import com.gitee.sop.servercommon.annotation.ApiMapping;
import com.gitee.sop.story.api.domain.Story;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;
import java.util.Arrays;
/**
* 支付宝服务端假设签名验证通过后到达这里进行具体的业务处理
* 这里演示如何接受业务参数
* @author tanghc
*/
@RestController
public class AlipayBookController {
@Autowired
StoryServiceConsumer storyServiceConsumer;
@ApiMapping(value = "alipay.book.get")
public Story getBook() {
Story story = new Story();
story.setId(1);
story.setName("白雪公主(alipay.book.get)");
return story;
}
// 调用story服务
@ApiMapping(value = "alipay.book.story.get")
public Object getBook2() {
Story story = new Story();
story.setId(1);
story.setName("白雪公主(alipay.book.story.get)");
Story story2 = storyServiceConsumer.getStory(1);
return Arrays.asList(story, story2);
}
}

@ -0,0 +1,74 @@
package com.gitee.sop.bookweb.controller;
import com.thoughtworks.xstream.annotations.XStreamAlias;
import lombok.Data;
/**
* 默认的结果封装类.
* <pre>
*
* xml返回结果:
* <response>
* <code>50</code>
* <msg>Remote service error</msg>
* <sub_code>isv.invalid-parameter</sub_code>
* <sub_msg>非法参数</sub_msg>
* </response>
* 成功情况
* <response>
* <code>0</code>
* <msg>成功消息</msg>
* <data>
* ...返回内容
* </data>
* </response>
*
* json返回格式
* {
* "code":"50",
* "msg":"Remote service error",
* "sub_code":"isv.invalid-parameter",
* "sub_msg":"非法参数"
* }
* 成功情况
* {
* "code":"0",
* "msg":"成功消息内容。。。",
* "data":{
* ...返回内容
* }
* }
* </pre>
* <p>
* 字段说明
* code:网关异常码 <br>
* msg:网关异常信息 <br>
* sub_code:业务异常码 <br>
* sub_msg:业务异常信息 <br>
*
* @author tanghc
*/
@XStreamAlias("response")
@Data
public class ApiResult {
/**
* 网关异常信息
*/
private String msg;
/**
* 业务异常码
*/
private String sub_msg;
/**
* 业务异常信息
*/
private String sub_code;
/**
* 返回结果
*/
private Object data;
}

@ -0,0 +1,73 @@
package com.gitee.sop.bookweb.controller;
import com.gitee.book.api.domain.Book;
import com.gitee.sop.bookweb.consumer.StoryServiceConsumer;
import com.gitee.sop.bookweb.param.BookParam;
import com.gitee.sop.story.api.domain.Story;
import com.hhdd.book.api.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.Arrays;
/**
* book服务
*
* @author tanghc
*/
@RestController
public class BookController implements BookService {
@Autowired
StoryServiceConsumer storyServiceConsumer;
@Override
public Book getBook(int id) {
Book book = new Book();
book.setId(id);
book.setName("汪汪队");
return book;
}
@RequestMapping("listBookAndStory")
public Object listBookAndStory(int id) {
Book book = new Book();
book.setId(id);
book.setName("汪汪队");
// 调用story服务
Story story = storyServiceConsumer.getStory(id);
return Arrays.asList(book, story);
}
@RequestMapping("getBook2")
public Object getBookError(int id) {
if (id == 0) {
throw new RuntimeException("id不能为空");
}
Book book = new Book();
book.setId(id);
book.setName("汪汪队");
return Arrays.asList(book);
}
@RequestMapping("getBook3")
public Object getBook3(@RequestBody BookParam param) {
if (param.getId() == 0) {
throw new RuntimeException("id不能为空");
}
Book book = new Book();
book.setId(param.getId());
book.setName("小马宝莉");
ApiResult apiResult = new ApiResult();
apiResult.setData(book);
return apiResult;
}
}

@ -0,0 +1,11 @@
package com.gitee.sop.bookweb.param;
import lombok.Data;
/**
* @author tanghc
*/
@Data
public class BookParam {
private int id;
}

@ -0,0 +1,9 @@
server:
port: 3333
eureka:
port: 1111
host: localhost
client:
serviceUrl:
defaultZone: http://${eureka.host}:${eureka.port}/eureka/

@ -0,0 +1,7 @@
spring:
application:
name: book-service
cloud:
zookeeper:
connect-string: localhost:2181

@ -0,0 +1,17 @@
package com.gitee.sop.bookweb;
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 SopBookApplicationTests {
@Test
public void contextLoads() {
}
}

@ -0,0 +1,15 @@
<?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-story</groupId>
<artifactId>sop-story-parent</artifactId>
<version>1.0.0-SNAPSHOT</version>
<packaging>pom</packaging>
<modules>
<module>sop-story-api</module>
<module>sop-story-web</module>
</modules>
</project>

@ -0,0 +1,25 @@
/target/
!.mvn/wrapper/maven-wrapper.jar
### STS ###
.apt_generated
.classpath
.factorypath
.project
.settings
.springBeans
.sts4-cache
### IntelliJ IDEA ###
.idea
*.iws
*.iml
*.ipr
### NetBeans ###
/nbproject/private/
/nbbuild/
/dist/
/nbdist/
/.nb-gradle/
/build/

@ -0,0 +1,46 @@
<?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>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.2.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.gitee.sop</groupId>
<artifactId>sop-story-api</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Greenwich.RELEASE</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.4</version>
<scope>provided</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>
</project>

@ -0,0 +1,18 @@
package com.gitee.sop.story.api.domain;
import lombok.Data;
import org.hibernate.validator.constraints.Length;
import javax.validation.constraints.NotBlank;
/**
* @author tanghc
*/
@Data
public class Story {
private int id;
@NotBlank(message = "name不能为空")
@Length(max = 20, message = "name长度不能超过20")
private String name;
}

@ -0,0 +1,15 @@
package com.gitee.sop.story.api.service;
import com.gitee.sop.story.api.domain.Story;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
/**
* @author tanghc
*/
@RequestMapping("/story")
public interface StoryService {
@RequestMapping("/getStory")
Story getStory(@RequestParam("id")/* 必须指定@RequestParam,且value不能少 */
int id);
}

@ -0,0 +1,25 @@
/target/
!.mvn/wrapper/maven-wrapper.jar
### STS ###
.apt_generated
.classpath
.factorypath
.project
.settings
.springBeans
.sts4-cache
### IntelliJ IDEA ###
.idea
*.iws
*.iml
*.ipr
### NetBeans ###
/nbproject/private/
/nbbuild/
/dist/
/nbdist/
/.nb-gradle/
/build/

@ -0,0 +1,84 @@
<?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>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.2.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.gitee.sop</groupId>
<artifactId>sop-story-web</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>sop-story</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Greenwich.RELEASE</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>com.gitee.sop</groupId>
<artifactId>sop-service-common</artifactId>
<version>1.0.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.gitee.sop</groupId>
<artifactId>sop-story-api</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.4</version>
<scope>provided</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>
</plugins>
</build>
<repositories>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
</repository>
</repositories>
</project>

@ -0,0 +1,16 @@
package com.gitee.sop.bookweb;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
@EnableDiscoveryClient
@SpringBootApplication
public class SopStoryApplication {
public static void main(String[] args) {
SpringApplication.run(SopStoryApplication.class, args);
}
}

@ -0,0 +1,31 @@
package com.gitee.sop.bookweb.config;
import com.gitee.sop.servercommon.bean.ServiceConfig;
import com.gitee.sop.servercommon.configuration.AlipayServiceConfiguration;
import com.gitee.sop.servercommon.configuration.TaobaoServiceConfiguration;
import org.springframework.context.annotation.Configuration;
import javax.annotation.PostConstruct;
/**
* 使用支付宝开放平台功能
* @author tanghc
*/
@Configuration
public class OpenServiceConfig extends AlipayServiceConfiguration {
static {
ServiceConfig.getInstance().getI18nModules().add("i18n/isp/goods_error");
}
}
/**
* 使用淘宝开放平台功能
* @author tanghc
*/
//@Configuration
//public class OpenServiceConfig extends TaobaoServiceConfiguration {
//
//}

@ -0,0 +1,27 @@
package com.gitee.sop.bookweb.controller;
import com.gitee.sop.servercommon.annotation.ApiMapping;
import com.gitee.sop.story.api.domain.Story;
import org.springframework.web.bind.annotation.RestController;
/**
* 支付宝服务端假设签名验证通过后到达这里进行具体的业务处理
* 这里演示如何接受业务参数
* @author tanghc
*/
@RestController
public class AlipayController {
@ApiMapping(value = "alipay.story.get")
public Story getStory() {
Story story = new Story();
story.setId(1);
story.setName("海底小纵队(alipay.story.get)");
return story;
}
@ApiMapping(value = "alipay.story.get", version = "1.2")
public Story getStory11(Story story) {
return story;
}
}

@ -0,0 +1,18 @@
package com.gitee.sop.bookweb.controller;
import com.gitee.sop.bookweb.controller.param.GoodsParam;
import com.gitee.sop.servercommon.annotation.ApiMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* 演示参数验证
* @author tanghc
*/
@RestController
public class JSR303DemoController {
@ApiMapping(value = "goods.add")
public Object addGoods(GoodsParam param) {
return param;
}
}

@ -0,0 +1,46 @@
package com.gitee.sop.bookweb.controller;
import com.gitee.sop.servercommon.annotation.ApiAbility;
import com.gitee.sop.servercommon.annotation.ApiMapping;
import com.gitee.sop.story.api.domain.Story;
import com.gitee.sop.story.api.service.StoryService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @author tanghc
*/
@ApiAbility // 放在这里,下面所有的接口都具备接口提供能力
@RestController
@RequestMapping("story2")
public class Story2Controller{
@RequestMapping("getStory4")
public Story getStory4() {
Story story = new Story();
story.setId(1);
story.setName("海底小纵队(getStory4)");
return story;
}
// 优先使用方法上@ApiAbility
@ApiAbility(version = "1.4")
@RequestMapping("getStory4")
public Story storyget() {
Story story = new Story();
story.setId(1);
story.setName("海底小纵队(1.4)");
return story;
}
// 优先使用@ApiMapping
@ApiMapping(value = "story.get2")
public Story getStory2() {
Story story = new Story();
story.setId(1);
story.setName("海底小纵队story.get2");
return story;
}
}

@ -0,0 +1,85 @@
package com.gitee.sop.bookweb.controller;
import com.gitee.sop.servercommon.annotation.ApiMapping;
import com.gitee.sop.servercommon.annotation.ApiAbility;
import com.gitee.sop.story.api.domain.Story;
import com.gitee.sop.story.api.service.StoryService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @author tanghc
*/
@RestController
public class StoryController implements StoryService {
// 提供给Feign的服务
@Override
public Story getStory(int id) {
Story story = new Story();
story.setId(id);
story.setName("海底小纵队(Feign)");
return story;
}
// http://localhost:2222/story/story_get
// 原生的接口,可正常调用
@RequestMapping("story_get")
public Story getStory4() {
Story story = new Story();
story.setId(1);
story.setName("海底小纵队(原生)");
return story;
}
// http://localhost:2222/story/story.get/
// 接口名,使用默认版本号
@ApiMapping(value = "story.get")
public Story storyget() {
Story story = new Story();
story.setId(1);
story.setName("海底小纵队(默认版本号)");
return story;
}
// http://localhost:2222/story/story.get/?version=1.1
// 接口名 + 版本号
@ApiMapping(value = "story.get", version = "1.1")
public Story getStory2() {
Story story = new Story();
story.setId(1);
story.setName("海底小纵队1.0");
return story;
}
// http://localhost:2222/story/story.get/?version=2.0
// 接口名 + 版本号
@ApiMapping(value = "story.get", version = "2.0")
public Story getStory20(Story story) {
return story;
}
// http://localhost:2222/story/getStory2
// 遗留接口具备开放平台能力
@ApiAbility
@RequestMapping("getStory2")
public Story getStory2_0() {
Story story = new Story();
story.setId(1);
story.setName("海底小纵队(默认版本号)");
return story;
}
// http://localhost:2222/story/getStory2?version=2.1
// 在原来的基础上加版本号
@ApiAbility(version = "2.1")
@RequestMapping("getStory2")
public Story getStory2_1() {
Story story = new Story();
story.setId(1);
story.setName("海底小纵队2.1");
return story;
}
}

@ -0,0 +1,21 @@
package com.gitee.sop.bookweb.controller;
import com.gitee.sop.servercommon.annotation.ApiMapping;
import com.gitee.sop.story.api.domain.Story;
import org.springframework.web.bind.annotation.RestController;
/**
* @author tanghc
*/
@RestController
public class StoryDemoController {
@ApiMapping(value = "story.demo.get")
public Story getStory() {
Story story = new Story();
story.setId(1);
story.setName("白雪公主");
return story;
}
}

@ -0,0 +1,33 @@
package com.gitee.sop.bookweb.controller;
import com.gitee.sop.bookweb.controller.param.GoodsUpdateParam;
import com.gitee.sop.bookweb.message.GoodsErrorEnum;
import com.gitee.sop.servercommon.annotation.ApiMapping;
import com.gitee.sop.servercommon.exception.ServiceException;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.RestController;
/**
* 演示如何抛出异常
* @author tanghc
*/
@RestController
public class ThrowExceptionDemoController {
@ApiMapping(value = "goods.update")
public Object updateGoods(GoodsUpdateParam param) {
// 方式1
if ("iphone6".equals(param.getGoods_name())) {
throw new ServiceException("不能更新iphone6");
}
// 方式2,国际化
if (StringUtils.isEmpty(param.getGoods_name())) {
throw GoodsErrorEnum.NO_GOODS_NAME.getErrorMeta().getException();
}
// 方式3,国际化传参
if (param.getGoods_name().length() <= 3) {
throw GoodsErrorEnum.LESS_GOODS_NAME_LEN.getErrorMeta().getException(3);
}
return param;
}
}

@ -0,0 +1,19 @@
package com.gitee.sop.bookweb.controller.param;
import lombok.Data;
import org.hibernate.validator.constraints.Length;
import javax.validation.constraints.NotEmpty;
@Data
public class GoodsParam {
// @NotEmpty(message = "商品名称不能为空")
private String goods_name;
@NotEmpty(message = "{goods.remark.notNull}")
private String goods_remark;
// 传参的格式:{xxx}=value1,value2...
@Length(min = 3, max = 20, message = "{goods.comment.length}=3,20")
private String goods_comment;
}

@ -0,0 +1,9 @@
package com.gitee.sop.bookweb.controller.param;
import lombok.Data;
@Data
public class GoodsUpdateParam {
private String goods_name;
}

@ -0,0 +1,20 @@
package com.gitee.sop.bookweb.message;
import com.gitee.sop.servercommon.message.ServiceErrorMeta;
public enum GoodsErrorEnum {
/** 参数错误 */
NO_GOODS_NAME("100"),
/** 参数长度太短 */
LESS_GOODS_NAME_LEN("101"),
;
private ServiceErrorMeta errorMeta;
GoodsErrorEnum(String subCode) {
this.errorMeta = new ServiceErrorMeta("isp.goods_error_", subCode);
}
public ServiceErrorMeta getErrorMeta() {
return errorMeta;
}
}

@ -0,0 +1,21 @@
package com.gitee.sop.bookweb.message;
import com.gitee.sop.servercommon.message.ServiceErrorMeta;
/**
* @author tanghc
*/
public enum StoryErrorEnum {
/** 参数错误 */
param_error("isv.invalid-parameter"),
;
private ServiceErrorMeta errorMeta;
StoryErrorEnum(String subCode) {
this.errorMeta = new ServiceErrorMeta("isp.error_", subCode);
}
public ServiceErrorMeta getErrorMeta() {
return errorMeta;
}
}

@ -0,0 +1,9 @@
server:
port: 2222
eureka:
port: 1111
host: localhost
client:
serviceUrl:
defaultZone: http://${eureka.host}:${eureka.port}/eureka/

@ -0,0 +1,8 @@
spring:
application:
name: story-service
description: story服务
cloud:
zookeeper:
connect-string: localhost:2181

@ -0,0 +1,10 @@
# 错误配置
# 系统配置
isp.error_isv.common-error=The system is busy.
isp.error_isv.invalid-parameter=Invalid parameter, {0}
# ==== 参数配置 ====
goods.remark.notNull=The goods_remark can not be null
goods.comment.length=The goods_comment length must >= {0} and <= {1}

@ -0,0 +1,14 @@
# 错误配置
# 系统繁忙
isp.error_isv.common-error=\u7cfb\u7edf\u7e41\u5fd9
# 参数无效
isp.error_isv.invalid-parameter=\u53c2\u6570\u65e0\u6548, {0}
# ==== 参数配置 ====
# 商品备注不能为空
goods.remark.notNull=\u5546\u54c1\u5907\u6ce8\u4e0d\u80fd\u4e3a\u7a7a
# 商品评论长度必须在{0}和{1}之间
goods.comment.length=\u5546\u54c1\u8bc4\u8bba\u957f\u5ea6\u5fc5\u987b\u5728{0}\u548c{1}\u4e4b\u95f4

@ -0,0 +1,2 @@
isp.goods_error_100=the goods_name can NOT be null
isp.goods_error_101=the goods_name must bigger than {0}

@ -0,0 +1,5 @@
# 商品名字不能为空
isp.goods_error_100=\u5546\u54C1\u540D\u79F0\u4E0D\u80FD\u4E3A\u7A7A
# 商品名称太短,不能小于{0}个字
isp.goods_error_101=\u5546\u54C1\u540D\u79F0\u592A\u77ED\uFF0C\u4E0D\u80FD\u5C0F\u4E8E{0}\u4E2A\u5B57

@ -0,0 +1,17 @@
package com.gitee.sop.bookweb;
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 SopStoryApplicationTests {
@Test
public void contextLoads() {
}
}
Loading…
Cancel
Save