gpt4 book ai didi

java - RESTful 服务 : Unable to return array of object using json

转载 作者:行者123 更新时间:2023-12-01 18:23:51 27 4
gpt4 key购买 nike

此样本是从通过 Google 找到的众多样本之一中提取的。应用程序返回 3 个对象,但所有属性均为 NULL。

以下是 Controller 对象。

import java.net.URI;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.support.ServletUriComponentsBuilder;

@RestController
@RequestMapping(path = "/employees")
public class EmployeeController
{
@Autowired
private EmployeeDAO employeeDao;

@GetMapping(path="/", produces = "application/json")
public Employees getEmployees()
{
return employeeDao.getAllEmployees();
}

@PostMapping(path= "/", consumes = "application/json", produces = "application/json")
public ResponseEntity<Object> addEmployee(@RequestBody Employee employee)
{
Integer id = employeeDao.getAllEmployees().getEmployeeList().size() + 1;
employee.setId(id);

employeeDao.addEmployee(employee);

URI location = ServletUriComponentsBuilder.fromCurrentRequest()
.path("/{id}")
.buildAndExpand(employee.getId())
.toUri();

return ResponseEntity.created(location).build();
}
}

这是通过 getEmployees 调用返回的Employees 对象。

import java.util.ArrayList;
import java.util.List;

public class Employees
{
private List<Employee> employeeList;

public List<Employee> getEmployeeList() {
if(employeeList == null) {
employeeList = new ArrayList<>();
}
return employeeList;
}

public void setEmployeeList(List<Employee> employeeList) {
this.employeeList = employeeList;
}
}

如果您需要它,这里是 DAO。

import org.springframework.stereotype.Repository;

@Repository
public class EmployeeDAO
{
private static Employees list = new Employees();

static
{
list.getEmployeeList().add(new Employee(11, "Lokesh", "Gupta", "howtodoinjava@gmail.com"));
list.getEmployeeList().add(new Employee(22, "Alex", "Kolenchiskey", "abc@gmail.com"));
list.getEmployeeList().add(new Employee(33, "David", "Kameron", "titanic@gmail.com"));
}

public Employees getAllEmployees()
{
return list;
}

public void addEmployee(Employee employee) {
list.getEmployeeList().add(employee);
}
}

根据要求,这里是 Employee 类。

import javax.annotation.ManagedBean;

import org.springframework.web.context.annotation.ApplicationScope;

import lombok.Data;
import lombok.Getter;
import lombok.Setter;

//@ManagedBean
//@ApplicationScope
//@Data

public class Employee {


public Employee() {

}
public Employee(Integer id, String firstName, String lastName, String email) {
super();
this.id = id;
this.firstName = firstName;
this.lastName = lastName;
this.email = email;
}

@Setter @Getter private Integer id;
private String firstName;
private String lastName;
private String email;

//Getters and setters

@Override
public String toString() {
return "Employee [id=" + id + ", firstName=" + firstName + ", lastName=" + lastName + ", email=" + email + "]";
}
}

这是部分客户端代码......

    private static void getEmployees() 
{

// HttpHeaders
HttpHeaders headers = new HttpHeaders();

headers.setAccept(Arrays.asList(new MediaType[] { MediaType.APPLICATION_JSON }));
// Request to return JSON format
headers.setContentType(MediaType.APPLICATION_JSON);
headers.set("my_other_key", "my_other_value");

// HttpEntity<String>: To get result as String.
HttpEntity<String> entity = new HttpEntity<String>(headers);

// RestTemplate
RestTemplate restTemplate = new RestTemplate();

// Send request with GET method, and Headers.
///This works but only prints IDs.
ResponseEntity<String> response = restTemplate.exchange(URL_EMPLOYEES, HttpMethod.GET, entity, String.class);
String result = response.getBody();
System.out.println(result);

HttpEntity<Employees> entity2 = new HttpEntity<Employees>(headers);
ResponseEntity<Employees> eResponse = restTemplate.exchange(URL_EMPLOYEES, HttpMethod.GET, entity2, Employees.class);
List <Employee> eList = eResponse.getBody().getEmployeeList();
for (Employee e : eList)
{
System.out.println(e.toString());
}
}

根据上面的内容,以下调用有效并打印出 3 个 ID。

    ResponseEntity<String> response = restTemplate.exchange(URL_EMPLOYEES, HttpMethod.GET, entity, String.class);
String result = response.getBody();
System.out.println(result);

返回 ID 后,下一步是使用下一组调用打印出每个员工的所有属性。

  ResponseEntity<Employees> eResponse = restTemplate.exchange(URL_EMPLOYEES, HttpMethod.GET, entity, Employees.class);
List <Employee> eList = eResponse.getBody().getEmployeeList();
for (Employee e : eList)
{
System.out.println(e.toString());
}

我在执行 LIST 调用的行上放置了一个断点,并验证了 eResponse 对象在列表中有 3 个项目,但所有 3 个项目均显示其关联属性均为 NULL。

由于我确认调用似乎在第一次调用中返回了 ID,因此第二次调用缺少什么命令或设置来允许返回完整的对象?

最佳答案

我认为你应该删除 super();在构造函数中。我们用它来访问父类(super class)的成员。您的 Employee 类没有父类。

关于java - RESTful 服务 : Unable to return array of object using json,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60263060/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com