英文:
How to mock an API that returns a JSON string in Spring Boot
问题
我是初学者,尝试在Spring Boot中模拟外部API。
这是API:
https://jsonplaceholder.typicode.com/posts
它返回一个类似这样的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"
},
...
我尝试了以下代码:
@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;
}
}
但是IDE中在resp.getBody()
处出现了错误:
类型不匹配:无法从Object转换为String
这是我的BlogPost代码:
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
}
如何修复这个问题?希望能在不使用另外工具的情况下完成。这只是一个简单的概念验证应用,所以我不想使用JUnit或Mockito进行测试。
英文:
I am a beginner in trying to mock an external API in Spring Boot.
Here is the API:
https://jsonplaceholder.typicode.com/posts
It returns a JSON string like this:
[
{
"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
How can I fix this? Hopefully without the use of yet another tool. This is just a simple proof-of-concept application, so I do not want to use JUnit or Mockito for testing.
答案1
得分: 0
以下是您要求的翻译部分:
API https://jsonplaceholder.typicode.com/posts
返回一个JSON对象数组。您需要在请求的期望响应类型中体现出这一点:
public String getAllBlogPosts() {
ResponseEntity resp = restTemplate.getForEntity("http://localhost:8080/posts", BlogPost[].class);
return resp.getStatusCode() == HttpStatus.OK ? resp.getBody() : null;
}
请注意 BlogPost[].class
中的 []
。
然后,由于 ResponseBody.getBody()
返回泛型类型 T
,而这里的 T
是 BlogPost[]
,但您的方法签名返回的是 String
,因此会出现 Type mismatch: cannot convert from Object to String
。
如果您只想获取JSON字符串,可以这样做:
public String getAllBlogPosts() {
ResponseEntity resp = restTemplate.getForEntity("http://localhost:8080/posts", String.class);
return resp.getStatusCode() == HttpStatus.OK ? resp.getBody() : null;
}
或者,如果您想要实际的 POJO 数组,则可以这样做:
public BlogPost[] getAllBlogPosts() {
ResponseEntity resp = restTemplate.getForEntity("http://localhost:8080/posts", BlogPost[].class);
return resp.getStatusCode() == HttpStatus.OK ? resp.getBody() : null;
}
英文:
The API https://jsonplaceholder.typicode.com/posts
returns an array of JSON objects. 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
.
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
.
If all you want is the JSON string, then you can just do this:
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:
public BlogPost[] getAllBlogPosts() {
ResponseEntity resp = restTemplate.getForEntity("http://localhost:8080/posts", BlogPost[].class);
return resp.getStatusCode() == HttpStatus.OK ? resp.getBody() : null;
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论