模拟连接 problems/errors/exceptions

Mock connection problems/errors/exceptions

我想模拟应用程序在连接到 mock 时出现异常的情况(例如 javax.net.ssl)。我知道如何构建有效或无效的响应。我不知道如何用 CXF 解释的特定异常来响应,就像异常一样。

简单来说:我想编写示例(或工具),当被询问(通过 get 或 SOAP)时将 return java 连接异常。

代码解释:

curl http://localhost:8088/myservice

作为响应,我希望抛出连接异常(例如 javax.net.ssl.SSLException ),就像 java 应用抛出一样。我知道如何抛出异常我仍然不知道如何抛出它们以响应请求(例如 GET)。

我找到了基于简单 spring 启动应用程序的解决方案 Hello World spring boot 并将其修改为特定的异常(无论如何,只要我们有应用程序或知道提供响应的框架,它就可以用于其他应用程序):

curl https://start.spring.io/starter.zip \
-d dependencies=web \
-d name=helloworld \
-d artifactId=helloworld \
-o helloworld.zip

解压helloworld.zip

然后粘贴到class:

import javax.net.ssl.SSLException;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@SpringBootApplication
public class HelloworldApplication {

    @RestController
    class HelloworldController {
        @GetMapping("/") //this will serve get
        String hello() throws SSLException {
            //this will land in Spring error message text
            throw new SSLException("Tested exception javax.net.ssl");
        }
    }

    public static void main(String[] args) {
        SpringApplication.run(HelloworldApplication.class, args);
    }
}

这将 return 一般 json 错误(在处理的请求中添加异常的相同技术可以在 servlet 或产生响应的应用程序的其他部分中使用)。

响应将如下所示

{"timestamp":"2018-08-23T09:22:26.691+0000","status":500,"error":"Internal Server Error","message":"Tested exception javax.net.ssl","path":"/"}