新增接口

以story服务为例,新增一个获取故事内容接口

  • 在controller下新建一个类,StoryDemoController.java
  • 加上@RestController注解
  1. @RestController
  2. public class StoryDemoController {
  3. }
  • 新增一个接口
  1. @ApiMapping(value = "story.demo.get")
  2. public StoryResult getStory() {
  3. StoryResult result = new StoryResult();
  4. result.setId(1L);
  5. result.setName("海底小纵队");
  6. return result;
  7. }

这里的@ApiMapping注解作用同@RequestMapping注解,可以理解为是它的扩展

value就是接口名,对应客户端的method参数

如果要加上版本号,指定version参数:@ApiMapping(value = "story.demo.get", version = "2.0")

  • 重启story服务,这样接口就可以使用了。

绑定业务参数

网关校验通过后,请求参数会传递到微服务上来,完整的参数如下所示:

  1. 请求参数:charset=utf-8&biz_content={"goods_remark":"iphone6"}&method=goods.add&format=json&app_id=2019032617262200001&sign_type=RSA2&version=1.0&timestamp=2019-04-29 19:18:38

其中biz_content部分是我们想要的,在方法上申明一个对象,对应biz_content中的内容即可完成参数绑定,并且对参数进行JSR-303校验。

  1. @ApiMapping(value = "goods.add")
  2. public Object addGoods(GoodsParam param) {
  3. return param;
  4. }
  5. @Data
  6. public class GoodsParam {
  7. @NotEmpty(message = "不能为空") // 支持JSR-303校验
  8. private String goods_remark;
  9. }

一般情况下,只需要获取业务参数即可,如果想要获取更多的参数,可在后面跟一个HttpServletRequest对象。

  1. @ApiMapping(value = "goods.add")
  2. public Object addGoods(GoodsParam param, HttpServletRequest request) {
  3. System.out.println(request.getParameter("method"));
  4. return param;
  5. }
  • 方式2
  1. @ApiMapping(value = "story.get", version = "2.2")
  2. public Story getStory22(OpenContext<Story> openContext) {
  3. // 业务参数
  4. Story bizObject = openContext.getBizObject();
  5. // 获取appid,更多方法查看OpenContext类
  6. String appId = openContext.getAppId();
  7. System.out.println(appId);
  8. return bizObject;
  9. }

另一种方式,OpenContext泛型参数填bizObject类,调用openContext.getBizObject()可直接获得对象

此方式等价于:

  1. @ApiMapping(value = "story.get", version = "2.2")
  2. public Story getStory22(Story bizObject) {
  3. OpenContext openContext = ServiceContext.getCurrentContext().getOpenContext();
  4. String appId = openContext.getAppId();
  5. System.out.println(appId);
  6. return bizObject;
  7. }

接口命名

接口命名没有做强制要求,但我们还是推荐按照下面的方式进行命名:

接口名的命名规则为:服务模块.业务模块.功能模块.行为,如:

  • mini.user.userinfo.get 小程序服务.用户模块.用户信息.获取
  • member.register.total.get 会员服务.注册模块.注册总数.获取

如果觉得命名规则有点长可以精简为:服务模块.功能模块.行为,如member.usercount.get,前提是确保前缀要有所区分,不和其它服务冲突。

测试接口

  • 在sop-test工程下新建一个测试用例,StoryDemoTest,继承TestBase
  1. public class StoryDemoTest extends TestBase {
  2. String url = "http://localhost:8081";
  3. String appId = "2019032617262200001";
  4. // 私钥
  5. String privateKey = "MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCXJv1pQFqWNA/++OYEV7WYXwexZK/J8LY1OWlP9X0T6wHFOvxNKRvMkJ5544SbgsJpVcvRDPrcxmhPbi/sAhdO4x2PiPKIz9Yni2OtYCCeaiE056B+e1O2jXoLeXbfi9fPivJZkxH/tb4xfLkH3bA8ZAQnQsoXA0SguykMRZntF0TndUfvDrLqwhlR8r5iRdZLB6F8o8qXH6UPDfNEnf/K8wX5T4EB1b8x8QJ7Ua4GcIUqeUxGHdQpzNbJdaQvoi06lgccmL+PHzminkFYON7alj1CjDN833j7QMHdPtS9l7B67fOU/p2LAAkPMtoVBfxQt9aFj7B8rEhGCz02iJIBAgMBAAECggEARqOuIpY0v6WtJBfmR3lGIOOokLrhfJrGTLF8CiZMQha+SRJ7/wOLPlsH9SbjPlopyViTXCuYwbzn2tdABigkBHYXxpDV6CJZjzmRZ+FY3S/0POlTFElGojYUJ3CooWiVfyUMhdg5vSuOq0oCny53woFrf32zPHYGiKdvU5Djku1onbDU0Lw8w+5tguuEZ76kZ/lUcccGy5978FFmYpzY/65RHCpvLiLqYyWTtaNT1aQ/9pw4jX9HO9NfdJ9gYFK8r/2f36ZE4hxluAfeOXQfRC/WhPmiw/ReUhxPznG/WgKaa/OaRtAx3inbQ+JuCND7uuKeRe4osP2jLPHPP6AUwQKBgQDUNu3BkLoKaimjGOjCTAwtp71g1oo+k5/uEInAo7lyEwpV0EuUMwLA/HCqUgR4K9pyYV+Oyb8d6f0+Hz0BMD92I2pqlXrD7xV2WzDvyXM3s63NvorRooKcyfd9i6ccMjAyTR2qfLkxv0hlbBbsPHz4BbU63xhTJp3Ghi0/ey/1HQKBgQC2VsgqC6ykfSidZUNLmQZe3J0p/Qf9VLkfrQ+xaHapOs6AzDU2H2osuysqXTLJHsGfrwVaTs00ER2z8ljTJPBUtNtOLrwNRlvgdnzyVAKHfOgDBGwJgiwpeE9voB1oAV/mXqSaUWNnuwlOIhvQEBwekqNyWvhLqC7nCAIhj3yvNQKBgQCqYbeec56LAhWP903Zwcj9VvG7sESqXUhIkUqoOkuIBTWFFIm54QLTA1tJxDQGb98heoCIWf5x/A3xNI98RsqNBX5JON6qNWjb7/dobitti3t99v/ptDp9u8JTMC7penoryLKK0Ty3bkan95Kn9SC42YxaSghzqkt+uvfVQgiNGQKBgGxU6P2aDAt6VNwWosHSe+d2WWXt8IZBhO9d6dn0f7ORvcjmCqNKTNGgrkewMZEuVcliueJquR47IROdY8qmwqcBAN7Vg2K7r7CPlTKAWTRYMJxCT1Hi5gwJb+CZF3+IeYqsJk2NF2s0w5WJTE70k1BSvQsfIzAIDz2yE1oPHvwVAoGAA6e+xQkVH4fMEph55RJIZ5goI4Y76BSvt2N5OKZKd4HtaV+eIhM3SDsVYRLIm9ZquJHMiZQGyUGnsvrKL6AAVNK7eQZCRDk9KQz+0GKOGqku0nOZjUbAu6A2/vtXAaAuFSFx1rUQVVjFulLexkXR3KcztL1Qu2k5pB6Si0K/uwQ=";
  6. @Test
  7. public void testDemo() throws Exception {
  8. // 公共请求参数
  9. Map<String, String> params = new HashMap<String, String>();
  10. params.put("app_id", appId);
  11. // 这里对应@ApiMapping.value属性
  12. params.put("method", "story.demo.get");
  13. params.put("format", "json");
  14. params.put("charset", "utf-8");
  15. params.put("sign_type", "RSA2");
  16. params.put("timestamp", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
  17. // 这里对应@ApiMapping.version属性
  18. params.put("version", "1.0");
  19. // 业务参数
  20. Map<String, String> bizContent = new HashMap<>();
  21. params.put("biz_content", JSON.toJSONString(bizContent));
  22. System.out.println("----------- 请求信息 -----------");
  23. System.out.println("请求参数:" + buildParamQuery(params));
  24. System.out.println("商户秘钥:" + privateKey);
  25. String content = AlipaySignature.getSignContent(params);
  26. System.out.println("待签名内容:" + content);
  27. String sign = AlipaySignature.rsa256Sign(content, privateKey, "utf-8");
  28. System.out.println("签名(sign):" + sign);
  29. params.put("sign", sign);
  30. System.out.println("----------- 返回结果 -----------");
  31. String responseData = post(url, params);// 发送请求
  32. System.out.println(responseData);
  33. }
  34. }
  • 请求成功后,控制台会打印:
  1. ----------- 请求信息 -----------
  2. 请求参数:charset=utf-8&biz_content={}&method=story.demo.get&format=json&app_id=alipay_test&sign_type=RSA2&version=1.0&timestamp=2019-03-23 15:41:22
  3. 商户秘钥:MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCXJv1pQFqWNA/++OYEV7WYXwexZK/J8LY1OWlP9X0T6wHFOvxNKRvMkJ5544SbgsJpVcvRDPrcxmhPbi/sAhdO4x2PiPKIz9Yni2OtYCCeaiE056B+e1O2jXoLeXbfi9fPivJZkxH/tb4xfLkH3bA8ZAQnQsoXA0SguykMRZntF0TndUfvDrLqwhlR8r5iRdZLB6F8o8qXH6UPDfNEnf/K8wX5T4EB1b8x8QJ7Ua4GcIUqeUxGHdQpzNbJdaQvoi06lgccmL+PHzminkFYON7alj1CjDN833j7QMHdPtS9l7B67fOU/p2LAAkPMtoVBfxQt9aFj7B8rEhGCz02iJIBAgMBAAECggEARqOuIpY0v6WtJBfmR3lGIOOokLrhfJrGTLF8CiZMQha+SRJ7/wOLPlsH9SbjPlopyViTXCuYwbz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KOGqku0nOZjUbAu6A2/vtXAaAuFSFx1rUQVVjFulLexkXR3KcztL1Qu2k5pB6Si0K/uwQ=
  4. 待签名内容:app_id=alipay_test&biz_content={}&charset=utf-8&format=json&method=story.demo.get&sign_type=RSA2&timestamp=2019-03-23 15:41:22&version=1.0
  5. 签名(sign):YMbxTPdovi6htcn1K3USTS6/Tbg6MOAMigG6x/kG0kQFCYH8ljvxXzcY86UT056nUG3OXxnj0xkw07eV6E03HMlu7bn3/jrT3PCcV3YguhA92aWz720x2xJWdfXY13OUPS9VOCC9zIVxu6EBD+PoZ7ojYChYvOfCR5I8bR/oOc0ZLjK63PWTBdf0eFS4sybXzRf81uNLMROsMhmBDDy0Fhml3ml77qzWBIpsmq5ECZ+89rMPbkNhAUcnFAe7ik7xZIL6WcUhAOhKVa8ZQK1GMjoGnAbGRed1FbuOHZGubgffg4/vMqrY10Bcy6h9jt/zK5w9L3HVgK3aPgQlfP16Gg==
  6. ----------- 返回结果 -----------
  7. {"story_demo_get_response":{"msg":"Success","code":"10000","name":"白雪公主","id":1},"sign":"YMbxTPdovi6htcn1K3USTS6/Tbg6MOAMigG6x/kG0kQFCYH8ljvxXzcY86UT056nUG3OXxnj0xkw07eV6E03HMlu7bn3/jrT3PCcV3YguhA92aWz720x2xJWdfXY13OUPS9VOCC9zIVxu6EBD+PoZ7ojYChYvOfCR5I8bR/oOc0ZLjK63PWTBdf0eFS4sybXzRf81uNLMROsMhmBDDy0Fhml3ml77qzWBIpsmq5ECZ+89rMPbkNhAUcnFAe7ik7xZIL6WcUhAOhKVa8ZQK1GMjoGnAbGRed1FbuOHZGubgffg4/vMqrY10Bcy6h9jt/zK5w9L3HVgK3aPgQlfP16Gg=="}

开放现有接口

如果想把现有项目中的接口开放出去,提供给客户调用,具体操作如下:

  • 将现有项目接入到SOP,前往项目接入到SOP文档页查看
  • 在现有接口方法上加上一个注解@ApiAbility,如下面这个接口
  1. // 具备开放平台能力
  2. @ApiAbility
  3. @RequestMapping("getStory2")
  4. public Story getStory2_0() {
  5. Story story = new Story();
  6. story.setId(1);
  7. story.setName("海底小纵队(默认版本号)");
  8. return story;
  9. }
  • 启动程序

这种情况下,老接口依然能正常访问,同时开放平台也能访问进来。

注意 此时的开放接口对应的接口名为:类@RequestMapping.value + “.” + 方法@RequestMapping.value

举个列子:

  1. @RequestMapping("goods")
  2. public class MyController {
  3. @ApiAbility
  4. @RequestMapping("listGoods")
  5. public Object fun() {
  6. }
  7. }

fun接口对应的路径为:/goods/listGoods

那么对应开放平台的接口名会转换成:goods.listGoods,客户端的method参数要填goods.listGoods

当然也可以直接把@RequestMapping替换成@ApiMapping并指定接口名,这样的话不能兼容以前的访问形式。