java crud mvc 端点上无法识别的路径问题

Unrecognized path problem on java crud mvc endpoint

提问人:Prova Henrique 提问时间:11/16/2023 最后编辑:Prova Henrique 更新时间:11/17/2023 访问量:44

问:

我的 crud spring mvc 端点上遇到了路径识别问题,我的 postam 返回:错误 404 路径未找到。

我的控制器:

package com.example.crud.controllers;

import com.example.crud.domain.product.Product;
import com.example.crud.domain.product.ProductRepository;
import com.example.crud.domain.product.RequestProduct;
import jakarta.persistence.EntityNotFoundException;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.Optional;

@RestController
@RequestMapping("/product")
public class ProductController {
    @Autowired
    private ProductRepository repository;
    @GetMapping
    public ResponseEntity getAllProducts(){
        var allProducts = repository.findAllByActiveTrue();
        return ResponseEntity.ok(allProducts);
    }

    @PostMapping
    public ResponseEntity registerProduct(@RequestBody @Valid RequestProduct data){
        Product newProduct = new Product(data);
        repository.save(newProduct);
        return ResponseEntity.ok().build();
    }

    @PutMapping
    @Transactional
    public ResponseEntity updateProduct(@RequestBody @Valid RequestProduct data){
        Optional<Product> optionalProduct = repository.findById(data.id());
        if (optionalProduct.isPresent()) {
            Product product = optionalProduct.get();
            product.setName(data.name());
            product.setPrice_in_cents(data.price_in_cents());
            return ResponseEntity.ok(product);
        } else {
            throw new EntityNotFoundException();
        }
    }

    @DeleteMapping("/{id}")
    @Transactional
    public ResponseEntity deleteProduct(@PathVariable String id){
        Optional<Product> optionalProduct = repository.findById(id);
        if (optionalProduct.isPresent()) {
            Product product = optionalProduct.get();
            product.setActive(false);
            return ResponseEntity.noContent().build();
        } else {
            throw new EntityNotFoundException();
        }
    }

}

我的实体:

package com.example.crud.domain.product;

import jakarta.persistence.*;
import lombok.*;

import java.util.UUID;

@Table(name="product")
@Entity(name="product")
@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
@EqualsAndHashCode(of = "id")
public class Product {
    @Id @GeneratedValue(strategy = GenerationType.UUID)
    private String id;

    private String name;

    private Integer price_in_cents;

    private Boolean active;

    public Product(RequestProduct requestProduct){
        this.name = requestProduct.name();
        this.price_in_cents = requestProduct.price_in_cents();
        this.active = true;
    }
}

我的仓库:

package com.example.crud.domain.product;

import org.springframework.data.jpa.repository.JpaRepository;

import java.util.List;
import java.util.UUID;

public interface ProductRepository extends JpaRepository<Product, String> {
    List<Product> findAllByActiveTrue();
}

我的要求:

package com.example.crud.domain.product;

import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;

import java.util.UUID;

public record RequestProduct(
        String id,

        @NotBlank
        String name,
        @NotNull
        Integer price_in_cents
) {
}

特别是在 getById 端点中,我的变量的路径显然没有被识别,我已经尝试重做导入,但仍然一无所获,我想在路径变量中解决这个问题。

ATT:我的 PUT 端点实际上是这样的:

@PutMapping{"/{id}"}

public ResponseEntity update(@PathVariable Long id, @RequestBody ProductRequest novosDados){

Product produtoAlterar= repository.findById(id).orElse(new Product);

produtoAlterar.setNome(novosDados.getNome);

produtoAlterar.setPrice_in_cents(novosDados.getPrice_in_cents);

repository.save(produtoAlterar);

return ResponseEntity.ok(produtoAlterar);

}
Java Spring CRUD

评论

0赞 Jens 11/16/2023
停止在 API 中使用实体。了解 3 层架构。
2赞 Jens 11/16/2023
你如何调用你的api?

答: 暂无答案