From 6bc3da3fa834efe0d6ef92567fbcdc18f5c2c151 Mon Sep 17 00:00:00 2001 From: tanghc Date: Wed, 24 Jul 2019 17:01:42 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BB=A3=E7=A0=81=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../WebappServiceConfiguration.java | 7 +- .../storyweb/controller/AlipayController.java | 4 +- .../TraditionalWebappController.java | 94 +++++++++++++++++++ .../controller/RedirectController.java | 11 +++ .../sop/websiteserver/bean/HttpTool.java | 1 - 5 files changed, 113 insertions(+), 4 deletions(-) create mode 100644 sop-example/sop-story/sop-story-web/src/main/java/com/gitee/sop/storyweb/controller/TraditionalWebappController.java diff --git a/sop-common/sop-service-common/src/main/java/com/gitee/sop/servercommon/configuration/WebappServiceConfiguration.java b/sop-common/sop-service-common/src/main/java/com/gitee/sop/servercommon/configuration/WebappServiceConfiguration.java index be7329d6..eac13717 100644 --- a/sop-common/sop-service-common/src/main/java/com/gitee/sop/servercommon/configuration/WebappServiceConfiguration.java +++ b/sop-common/sop-service-common/src/main/java/com/gitee/sop/servercommon/configuration/WebappServiceConfiguration.java @@ -1,9 +1,14 @@ package com.gitee.sop.servercommon.configuration; +import com.gitee.sop.servercommon.bean.ServiceConfig; + /** * 通用web * @author tanghc */ public class WebappServiceConfiguration extends BaseServiceConfiguration { - + static { + // 默认版本号为1.0 + ServiceConfig.getInstance().setDefaultVersion("1.0"); + } } diff --git a/sop-example/sop-story/sop-story-web/src/main/java/com/gitee/sop/storyweb/controller/AlipayController.java b/sop-example/sop-story/sop-story-web/src/main/java/com/gitee/sop/storyweb/controller/AlipayController.java index 835f9db7..5ea2f8ab 100644 --- a/sop-example/sop-story/sop-story-web/src/main/java/com/gitee/sop/storyweb/controller/AlipayController.java +++ b/sop-example/sop-story/sop-story-web/src/main/java/com/gitee/sop/storyweb/controller/AlipayController.java @@ -105,10 +105,10 @@ public class AlipayController { public StoryVO getStory2(StoryParam story, HttpServletRequest request) { log.info("获取故事信息参数, story: {}", story); // 获取其它参数 - System.out.println(request.getParameter("app_id")); + String app_id = request.getParameter("app_id"); StoryVO storyVO = new StoryVO(); storyVO.id = 1L; - storyVO.name = "白雪公主"; + storyVO.name = "白雪公主, app_id:" + app_id; storyVO.gmt_create = new Date(); return storyVO; } diff --git a/sop-example/sop-story/sop-story-web/src/main/java/com/gitee/sop/storyweb/controller/TraditionalWebappController.java b/sop-example/sop-story/sop-story-web/src/main/java/com/gitee/sop/storyweb/controller/TraditionalWebappController.java new file mode 100644 index 00000000..d36820c1 --- /dev/null +++ b/sop-example/sop-story/sop-story-web/src/main/java/com/gitee/sop/storyweb/controller/TraditionalWebappController.java @@ -0,0 +1,94 @@ +package com.gitee.sop.storyweb.controller; + +import com.gitee.sop.servercommon.annotation.ApiMapping; +import com.gitee.sop.servercommon.util.UploadUtil; +import lombok.Data; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.multipart.MultipartFile; + +import javax.servlet.http.HttpServletRequest; +import java.math.BigDecimal; +import java.util.Collection; + +/** + * 传统web开发实例 + * @author tanghc + */ +@RestController +@RequestMapping("food") +public class TraditionalWebappController { + + + // http://localhost:8081/getFoodById?id=1 网关入口 + // http://localhost:2222/food/getFoodById/?id=12 本地入口 + @ApiMapping(value = "getFoodById", method = RequestMethod.GET) + public Food getFoodById(Integer id) { + Food food = new Food(); + food.setId(id); + food.setName("香蕉"); + food.setPrice(new BigDecimal(20.00)); + return food; + } + + // http://localhost:8081/getFoodById?id=2&version=1.1 网关入口 + // http://localhost:2222/food/getFoodById/?id=12&version=1.1 + @ApiMapping(value = "getFoodById", method = RequestMethod.GET, version = "1.1") + public Food getFoodById2(Integer id) { + Food food = new Food(); + food.setId(id); + food.setName("香蕉2"); + food.setPrice(new BigDecimal(22.00)); + return food; + } + + // http://localhost:8081/getFoodByObj?id=2 + @ApiMapping(value = "getFoodByObj", method = RequestMethod.GET) + public Food getFoodByObj(Food food) { + return food; + } + + // http://localhost:8081/saveFood + @ApiMapping(value = "saveFood", method = RequestMethod.POST) + public Food saveFood(@RequestBody Food food) { + food.setId(3); + return food; + } + + @ApiMapping(value = "foodUpload", method = RequestMethod.POST) + public Food upload(Food food, HttpServletRequest request) { + // 获取上传的文件 + Collection uploadFiles = UploadUtil.getUploadFiles(request); + StringBuilder sb = new StringBuilder(); + for (MultipartFile multipartFile : uploadFiles) { + sb.append(multipartFile.getOriginalFilename()); + } + food.setId(4); + food.setName("文件名称+" + sb.toString()); + return food; + } + + @ApiMapping(value = "foodUpload3", method = RequestMethod.POST) + public Food upload3(Food food, MultipartFile file) { + food.setId(5); + food.setName("文件名称+" + file.getOriginalFilename()); + return food; + } + + @RequestMapping(value = "foodUpload2", method = RequestMethod.POST) + public Food upload2(Food food, MultipartFile file) { + food.setId(4); + food.setName("文件名称2+" + file.getOriginalFilename()); + return food; + } + + @Data + public static class Food { + private Integer id; + private String name; + private BigDecimal price; + } + +} diff --git a/sop-gateway/src/main/java/com/gitee/sop/gateway/controller/RedirectController.java b/sop-gateway/src/main/java/com/gitee/sop/gateway/controller/RedirectController.java index a1d09f27..a2bc0db2 100644 --- a/sop-gateway/src/main/java/com/gitee/sop/gateway/controller/RedirectController.java +++ b/sop-gateway/src/main/java/com/gitee/sop/gateway/controller/RedirectController.java @@ -32,4 +32,15 @@ public class RedirectController { request.getRequestDispatcher(path).forward(request, response); } + @RequestMapping("/{method}") + public void redirect2( + @PathVariable("method") String method + , HttpServletRequest request + , HttpServletResponse response + ) throws ServletException, IOException { + request.setAttribute(SopConstants.REDIRECT_METHOD_KEY, method); + request.setAttribute(SopConstants.REDIRECT_VERSION_KEY, "1.0"); + request.getRequestDispatcher(path).forward(request, response); + } + } diff --git a/sop-website/website-server/src/main/java/com/gitee/sop/websiteserver/bean/HttpTool.java b/sop-website/website-server/src/main/java/com/gitee/sop/websiteserver/bean/HttpTool.java index d9e057db..a0ff4e60 100644 --- a/sop-website/website-server/src/main/java/com/gitee/sop/websiteserver/bean/HttpTool.java +++ b/sop-website/website-server/src/main/java/com/gitee/sop/websiteserver/bean/HttpTool.java @@ -24,7 +24,6 @@ import java.util.concurrent.TimeUnit; * @author tanghc */ public class HttpTool { - private static final String METHOD_GET = "get"; private Map> cookieStore = new HashMap>(); private OkHttpClient httpClient;