MVC Post 和 Put 映射方法,响应正文值“null”但使用 Post Man 时状态 200 OK

发布于 2025-01-12 04:13:28 字数 4719 浏览 0 评论 0原文

我正在尝试使用 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]

Postman
Extracted Numbers Class

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

巾帼英雄 2025-01-19 04:13:28

你的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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文