Java "error" : "Not Found" , "message" : "No message available" ,



我与一个小的弹簧应用程序一起工作,在该应用程序中我的值很少,我想使用可变的呼叫来检索它们。

API在这里,

@RestController
@RequestMapping("/api/v1/products")
public class ProductAPI {
    private ProductService service;
    @Autowired
    public void setService(ProductService service) {
        this.service = service;
    }

@GetMapping("/stock/")
public ResponseEntity<Product> findById(@RequestParam("productId") String productId) {
    Product product = service.findById(productId).get();
    return ResponseEntity.of(Optional.of(product));
}
...........
}

服务电话,

@Service
public class ProductService {

 private ProductRepository repository;
    @Autowired
    public void setProductRepository(ProductRepository productRepository) {
        this.repository = productRepository;
    }
    public Optional<Product> findById(String id) {
       return repository.findById(id);
    }
}

存储库类,

@Repository
 public interface ProductRepository extends CrudRepository<Product, String>{

 }

当我使用卷发打电话时,我会收到消息,

   $ curl -X GET http://localhost:8080/api/v1/products/stock?productId=Product%20ID | jq
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   142    0   142    0     0    845      0 --:--:-- --:--:-- --:--:--   850
{
  "timestamp": "2019-02-25T12:19:31.797+0000",
  "status": 404,
  "error": "Not Found",
  "message": "No message available",
  "path": "/api/v1/products/stock"
}

i正确插入数据库中的条目。这里有什么问题?

,因为您在映射中有额外的/

@GetMapping("/stock/")

所以,如果您想要这样的请求

curl -x获取 http://localhost:8080/api/v1/products/products/stock/productid = product%20ID

您需要映射:

@GetMapping("/stock")

在您的当前版本中,正确的卷发看起来像:

http://localhost:8080/api/v1/products/stock/?productId=Product%20ID

,因为您已经清楚地提到了映射为@getMapping("/stock/"),因此而且,当您试图通过路径/库存访问资源时,显然没有这样的映射。因此,您找到了404个例外。

因此,更新映射(例如@getMapping)("/stock")。

快乐学习!

相关内容