簡體   English   中英

@RestController 和 @GetMapping 不會在簡單的 Hello World 列表中返回 JSON 數組

[英]@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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM