[英]@RestController and @GetMapping does not return JSON array in simple Hello World List
我正在使用 Spring Boot 尝试通过 @RestController 和 @GetMapping 获取 JSON 响应,但它没有在本地主机上以 JSON 形式出现。 这是我的代码。 任何人都可以解决这个问题吗?
@SpringBootApplication
@RestController
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
@GetMapping
public List<Employee> hello () {
return List.of(
new Employee(
1L,
"Pedro",
"rt.pedrosantos@gmail.com",
LocalDate.of(1989, Month.JUNE, 21),
32
)
);
}
}
以下是我创建的带有 setter 和 getter 的“员工”类。
package com.example.employee;
import java.time.LocalDate;
public class Employee {
private Long id;
private String name;
private String email;
private LocalDate dob;
private Integer age;
public Employee() {
}
public Employee(Long id,
String name,
String email,
LocalDate dob,
Integer age) {
this.id = id;
this.name = name;
this.email = email;
this.dob = dob;
this.age = age;
}
public Employee(String name,
String email,
LocalDate dob,
Integer age) {
this.name = name;
this.email = email;
this.dob = dob;
this.age = age;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public LocalDate getDob() {
return dob;
}
public void setDob(LocalDate dob) {
this.dob = dob;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
@Override
public String toString() {
return "Employee{" +
"id=" + id +
", name='" + name + '\'' +
", email='" + email + '\'' +
", dob=" + dob +
", age=" + age +
'}';
}
}
课程到此结束。 我无法正确地将代码返回到 JSON,我不知道为什么。 谁能解释一下?
编辑:它返回一个 Json。 检查您的浏览器或休息客户端是否正确配置。
上一个答案:参考这个:因为你已经用@RestController 进行了注释,所以不需要进行显式的 json 转换。 由于您没有返回 POJO,并且根据您发布的代码,您没有任何 getter,因此将其更改为以下内容即可:
@RequestMapping(value = "hello_world", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
public List<StringResponse> hello() {
return List.of(new StringResponse("Hello"),new StringResponse("World"));
}
}
class StringResponse {
private String response;
public StringResponse(String s) {
this.response = s;
}
public String getResponse() {
return response;
}
public void setResponse(String response) {
this.response = response;
}
}
或者使用像 Gson 这样的库: 有一种干净的方法可以在 Spring Web API 中将字符串作为 json 返回?
例子:
@RequestMapping(value = "hello_world", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
public List<String> hello() {
Gson gson = new GsonBuilder().create();
Type type = new TypeToken<List<String>>() {}.getType();
String json = "[ \"Hello\", \"World\"]";
List<String> responseList = gson.fromJson(json, type);
return responseList;
}
更多信息: Spring MVC - 如何在 Rest Controller 中将简单字符串作为 JSON 返回
如何使用@ResponseBody 从 spring Controller 返回 JSON 数据
不确定您要做什么,但应该使用相应的 setter/getter 创建模型类。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.