简体   繁体   English

如何模拟在 Spring Boot 中返回 json 字符串的 api

[英]How to mock an api that returns a json string in spring boot

I am a beginner in trying to mock an external api in Spring Boot.我是尝试在 Spring Boot 中模拟外部 api 的初学者。 Here is the api: https://jsonplaceholder.typicode.com/posts这是 API: https : //jsonplaceholder.typicode.com/posts

It returns a json string like this:它返回一个 json 字符串,如下所示:

[
  {
    "userId": 1,
    "id": 1,
    "title": "sunt aut facere repellat provident occaecati excepturi optio reprehenderit",
    "body": "quia et suscipit\nsuscipit recusandae consequuntur expedita et cum\nreprehenderit molestiae ut ut quas totam\nnostrum rerum est autem sunt rem eveniet architecto"
  },
...

I've tried the following code:我试过以下代码:

    @Service
    public class BlogPostService {
        @Autowired
        private RestTemplate restTemplate;
     
        public String getAllBlogPosts() {
            ResponseEntity resp = restTemplate.getForEntity("http://localhost:8080/posts", String.class);
            
            return resp.getStatusCode() == HttpStatus.OK ? resp.getBody() : null;
        }
    
    }

But there is an error in the IDE at resp.getBody() which says 

Type mismatch: cannot convert from Object to String类型不匹配:无法从对象转换为字符串

Here is my BlogPost code:这是我的博客帖子代码:

public class BlogPost {
    private int userId;
    private String title;
    private String body;
     
    public BlogPost() {
    }
     
    public BlogPost(int userId, String title, String body) {
        this.title = title;
        this.body = body;
    }
//...getters & setters

Could one of you experts give me some guidance?你们中的一位专家能给我一些指导吗? Hopefully without the use of yet another tool.希望不使用另一种工具。 This is just a simple proof-of-concept app so not wanting to use JUnit or Mockito for testing.这只是一个简单的概念验证应用程序,因此不想使用 JUnit 或 Mockito 进行测试。

The API https://jsonplaceholder.typicode.com/posts returns an array of JSON objects. API https://jsonplaceholder.typicode.com/posts返回一个 JSON 对象数组。 You will need to reflect this in your request's desired response type您需要在请求所需的响应类型中反映这一点

public String getAllBlogPosts() {
    ResponseEntity resp = restTemplate.getForEntity("http://localhost:8080/posts", BlogPost[].class);
            
    return resp.getStatusCode() == HttpStatus.OK ? resp.getBody() : null;
}

Notice the [] in BlogPost[].class .注意到[]BlogPost[].class

Then, you are getting a Type mismatch: cannot convert from Object to String since ResponseBody.getBody() returns a generic type T , which is BlogPost[] but your method signature returns a String .然后,您遇到Type mismatch: cannot convert from Object to String因为ResponseBody.getBody()返回泛型类型T ,即BlogPost[]但您的方法签名返回String

If all you want is the JSON string, then you can just do this如果你想要的只是 JSON 字符串,那么你可以这样做

public String getAllBlogPosts() {
    ResponseEntity resp = restTemplate.getForEntity("http://localhost:8080/posts", String.class);

    return resp.getStatusCode() == HttpStatus.OK ? resp.getBody() : null;
}

Otherwise, if you want the actual POJO array then do this否则,如果您想要实际的 POJO 数组,请执行此操作

public BlogPost[] getAllBlogPosts() {
    ResponseEntity resp = restTemplate.getForEntity("http://localhost:8080/posts", BlogPost[].class);

    return resp.getStatusCode() == HttpStatus.OK ? resp.getBody() : null;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM