MVC Post 和 Put 映射方法,响应正文值“null”但使用 Post Man 时状态 200 OK
我正在尝试使用 Spring Boot MVC 来做一个应用程序,但遇到了问题。对于 Post 和 Put 方法,我收到状态 200 ok,但值为“null”。数据库上的新注册已创建,但仅填充 Id(自动增量)列并在 Postman 响应中接收。有人可以帮我吗?
` @RequestMapping("/api") @RestController 公共类 ExtractedNumbersController { 私有最终 ExtractedNumbersService extractedNumbersService; @Autowired 公共ExtractedNumbersController(ExtractedNumbersService ExtractedNumbersService){ this.extractedNumbersService = extractedNumbersService; 邮差
@PostMapping("/addExtractedNumbers")
public ExtractedNumbers addExtractedNumbers(@RequestBody ExtractedNumbers extractedNumbers) {
return extractedNumbersService.addExtractedNumbers(extractedNumbers);
}
@GetMapping("/findAllExtractedNumbers")
public List<ExtractedNumbers> findAllActivities() {
return extractedNumbersService.findAllExtractedNumbers();
}
@GetMapping("findExtractedNumbersById/{id}")
public Optional<ExtractedNumbers> findExtractedNumbersById(@PathVariable("id") Integer id) {
return extractedNumbersService.findByIdExtractedNumbers(id);
}
@PutMapping("/updateExtractedNumbers/{id}")
public ExtractedNumbers updateProduct(@PathVariable("id") Integer id, @RequestBody ExtractedNumbers extractedNumbers) {
Optional<ExtractedNumbers> extractedNumbersFromDatabase = extractedNumbersService.findByIdExtractedNumbers(id);
if (extractedNumbersFromDatabase.isPresent()) {
extractedNumbersFromDatabase.get().setExtractedNumbers(extractedNumbers.getExtractedNumbers());
extractedNumbersFromDatabase.get().setExtractionDate(extractedNumbers.getExtractionDate());
final ExtractedNumbers updatedExtractedNumbers = extractedNumbersService.addExtractedNumbers(extractedNumbersFromDatabase.get());
return updatedExtractedNumbers;
}
return extractedNumbersService.addExtractedNumbers(extractedNumbers);
}
@DeleteMapping("/deleteExtractedNumbersById/{id}")
public void deleteExtractedNumbersById(@PathVariable("id") Integer id) {
extractedNumbersService.deleteExtractedNumbers(id);
}
}
@Data
@Table
@Entity
@AllArgsConstructor
@NoArgsConstructor
public class ExtractedNumbers {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer extractedNumbersId;
@Column
private String extractedNumbers;
@Column
private String extractionDate;
public ExtractedNumbers(String extractedNumbers, String extractionDate) {
this.extractedNumbers = extractedNumbers;
this.extractionDate = extractionDate;
}
public String getExtractedNumbers() {
return extractedNumbers;
}
public void setExtractedNumbers(String extractedNumbers) {
this.extractedNumbers = extractedNumbers;
}
public String getExtractionDate() {
return extractionDate;
}
public void setExtractionDate(String extractionDate) {
this.extractionDate = extractionDate;
}
}
public interface ExtractedNumbersService {
List<ExtractedNumbers> findAllExtractedNumbers();
ExtractedNumbers addExtractedNumbers(ExtractedNumbers extractedNumbers);
Optional<ExtractedNumbers> findByIdExtractedNumbers(Integer extractedNumbersId);
ExtractedNumbers update(ExtractedNumbers extractedNumbers);
void deleteExtractedNumbers(Integer extractedNumbersId);
}
@Service
public class ExtractedNumbersServiceImpl implements ExtractedNumbersService {
private final ExtractedNumbersRepository extractedNumbersRepository;
public ExtractedNumbersServiceImpl(ExtractedNumbersRepository extractedNumbersRepository) {
this.extractedNumbersRepository = extractedNumbersRepository;
}
@Override
public List<ExtractedNumbers> findAllExtractedNumbers() { return extractedNumbersRepository.findAll() ; }
@Override
public ExtractedNumbers addExtractedNumbers(ExtractedNumbers extractedNumbers) {
return extractedNumbersRepository.save(extractedNumbers);
}
@Override
public Optional<ExtractedNumbers> findByIdExtractedNumbers(Integer extractedNumbersId) {
return extractedNumbersRepository.findById(extractedNumbersId);
}
@Override
public ExtractedNumbers update(ExtractedNumbers extractedNumbers) {
return extractedNumbersRepository.save(extractedNumbers);
}
@Override
public void deleteExtractedNumbers(Integer extractedNumbersId) {
extractedNumbersRepository.deleteById(extractedNumbersId);
}
}`
[Database][1]
I'm trying to do an application using Spring Boot MVC and I've run into a problem. For the Post and Put methods I receive status 200 ok but the values are "null". The new registration on database is created but just the Id (autoincrement) column is populated and received in Postman response. Could someone help me please?
` @RequestMapping("/api")
@RestController
public class ExtractedNumbersController {
private final ExtractedNumbersService extractedNumbersService;
@Autowired
public ExtractedNumbersController(ExtractedNumbersService extractedNumbersService) {
this.extractedNumbersService = extractedNumbersService;
}
@PostMapping("/addExtractedNumbers")
public ExtractedNumbers addExtractedNumbers(@RequestBody ExtractedNumbers extractedNumbers) {
return extractedNumbersService.addExtractedNumbers(extractedNumbers);
}
@GetMapping("/findAllExtractedNumbers")
public List<ExtractedNumbers> findAllActivities() {
return extractedNumbersService.findAllExtractedNumbers();
}
@GetMapping("findExtractedNumbersById/{id}")
public Optional<ExtractedNumbers> findExtractedNumbersById(@PathVariable("id") Integer id) {
return extractedNumbersService.findByIdExtractedNumbers(id);
}
@PutMapping("/updateExtractedNumbers/{id}")
public ExtractedNumbers updateProduct(@PathVariable("id") Integer id, @RequestBody ExtractedNumbers extractedNumbers) {
Optional<ExtractedNumbers> extractedNumbersFromDatabase = extractedNumbersService.findByIdExtractedNumbers(id);
if (extractedNumbersFromDatabase.isPresent()) {
extractedNumbersFromDatabase.get().setExtractedNumbers(extractedNumbers.getExtractedNumbers());
extractedNumbersFromDatabase.get().setExtractionDate(extractedNumbers.getExtractionDate());
final ExtractedNumbers updatedExtractedNumbers = extractedNumbersService.addExtractedNumbers(extractedNumbersFromDatabase.get());
return updatedExtractedNumbers;
}
return extractedNumbersService.addExtractedNumbers(extractedNumbers);
}
@DeleteMapping("/deleteExtractedNumbersById/{id}")
public void deleteExtractedNumbersById(@PathVariable("id") Integer id) {
extractedNumbersService.deleteExtractedNumbers(id);
}
}
@Data
@Table
@Entity
@AllArgsConstructor
@NoArgsConstructor
public class ExtractedNumbers {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer extractedNumbersId;
@Column
private String extractedNumbers;
@Column
private String extractionDate;
public ExtractedNumbers(String extractedNumbers, String extractionDate) {
this.extractedNumbers = extractedNumbers;
this.extractionDate = extractionDate;
}
public String getExtractedNumbers() {
return extractedNumbers;
}
public void setExtractedNumbers(String extractedNumbers) {
this.extractedNumbers = extractedNumbers;
}
public String getExtractionDate() {
return extractionDate;
}
public void setExtractionDate(String extractionDate) {
this.extractionDate = extractionDate;
}
}
public interface ExtractedNumbersService {
List<ExtractedNumbers> findAllExtractedNumbers();
ExtractedNumbers addExtractedNumbers(ExtractedNumbers extractedNumbers);
Optional<ExtractedNumbers> findByIdExtractedNumbers(Integer extractedNumbersId);
ExtractedNumbers update(ExtractedNumbers extractedNumbers);
void deleteExtractedNumbers(Integer extractedNumbersId);
}
@Service
public class ExtractedNumbersServiceImpl implements ExtractedNumbersService {
private final ExtractedNumbersRepository extractedNumbersRepository;
public ExtractedNumbersServiceImpl(ExtractedNumbersRepository extractedNumbersRepository) {
this.extractedNumbersRepository = extractedNumbersRepository;
}
@Override
public List<ExtractedNumbers> findAllExtractedNumbers() { return extractedNumbersRepository.findAll() ; }
@Override
public ExtractedNumbers addExtractedNumbers(ExtractedNumbers extractedNumbers) {
return extractedNumbersRepository.save(extractedNumbers);
}
@Override
public Optional<ExtractedNumbers> findByIdExtractedNumbers(Integer extractedNumbersId) {
return extractedNumbersRepository.findById(extractedNumbersId);
}
@Override
public ExtractedNumbers update(ExtractedNumbers extractedNumbers) {
return extractedNumbersRepository.save(extractedNumbers);
}
@Override
public void deleteExtractedNumbers(Integer extractedNumbersId) {
extractedNumbersRepository.deleteById(extractedNumbersId);
}
}`
[Database][1]
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你的ExtractedNumbers中的属性和数据库表列一致吗?
如果您的表格列类似于extracted_numbers,您需要对camel添加下划线
Are your attributes in ExtractedNumbers consistent with the database table column?
if your table column is like extracted_numbers you need to do the underline to camel