java - 通过邮递员调用时模拟第三方 api
问题描述
我有一个控制器类,它公开一个 Get API 并返回人员信息:
@RestController
public class PersonController {
@Autowired
PersonService personService;
@GetMapping
public Person getPersonInfo() {
return personService.getPerson();
}
}
在 personService 中,我连接到第三方并从此 API 获取人员信息。现在我想使用邮递员在我的控制器中调用 getPersonInfo() API。我如何在这个场景中模拟第三方响应?我知道如果您使用测试(集成或单元)和线模,这是可能的。仅使用邮递员调用我的 API 怎么样?
解决方案
您可以使用@Profile
注释。
public interface PersonService {
...
}
@Service
@Profile("default")
public class PersonServiceImpl implements PersonService {
...
}
@Service
@Profile("mock")
public class MockPersonService implements PersonService {
...
}
如上定义,您可以选择带有 的配置文件appliation.properties
。例如:
spring.profiles.active=mock
推荐阅读
- javascript - Android反应原生接近传感器
- python - sqlalchemy 连接烧瓶
- javascript - 获取请求后重定向需要两次点击?
- objective-c - TVOS 上的 contentOverlayView 未显示
- assembly - 如何在 x86 程序集中将 16 位寄存器移动到 8 位寄存器?
- regex - Delphi中用于StringList中所有匹配行的正则表达式
- c++ - 我是否正确推理缓存性能?
- java - 是否可以拦截 java jar 的传出 tcp 请求?
- python - 如何比较来自多个csv的数据
- javascript - Javascript/jQuery 缓存实际上是如何促进 DOM 的修改的?