我在
我的控制器
package com.howtodoinjava.demo.controller;
import com.howtodoinjava.demo.model.Employee;
import com.howtodoinjava.demo.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import java.time.Duration;
@RestController
public class EmployeeController {
@Autowired
private EmployeeService employeeService;
@RequestMapping(value = {"/create", "/"}, method = RequestMethod.POST)
@ResponseStatus(HttpStatus.CREATED)
@ResponseBody
public void create(@RequestBody Employee e) {
employeeService.create(e);
}
@RequestMapping(value = "/{id}", method = RequestMethod.GET)
@ResponseBody
public ResponseEntity<Mono<Employee>> findById(@PathVariable("id") Integer id) {
Mono<Employee> e = employeeService.findById(id);
HttpStatus status = e != null ? HttpStatus.OK : HttpStatus.NOT_FOUND;
return new ResponseEntity<Mono<Employee>>(e, status);
}
@RequestMapping(value = "/name/{name}", method = RequestMethod.GET)
@ResponseBody
public Flux<Employee> findByName(@PathVariable("name") String name) {
return employeeService.findByName(name);
}
@RequestMapping(method = RequestMethod.GET, produces = MediaType.TEXT_EVENT_STREAM_VALUE)
@ResponseBody
public Flux<Employee> findAll() {
// Flux<Employee> emps = employeeService.findAll();
Flux<Employee> emps = employeeService.findAll().log().delayElements(Duration.ofSeconds(1));
emps.subscribe();
return emps;
}
@RequestMapping(value = "/update", method = RequestMethod.PUT)
@ResponseStatus(HttpStatus.OK)
public Mono<Employee> update(@RequestBody Employee e) {
return employeeService.update(e);
}
@RequestMapping(value = "/delete/{id}", method = RequestMethod.DELETE)
@ResponseStatus(HttpStatus.OK)
public void delete(@PathVariable("id") Integer id) {
employeeService.delete(id).subscribe();
}
}
在除錯控制臺中,我看到了結果


uj5u.com熱心網友回復:
回應為空的原因是Employee物體類中沒有定義getter方法。添加以下內容應使其作業:
public int getId() {
return id;
}
public String getName() {
return name;
}
public long getSalary() {
return salary;
}
作為旁注,請考慮將您的物體映射到EmployeeDTO實體。
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/404713.html
標籤:
