gpt4 book ai didi

spring - REST API 调用 : Missing URI template variable 'productoId' for method parameter of type Long

转载 作者:行者123 更新时间:2023-12-04 18:33:22 25 4
gpt4 key购买 nike

我试图在 Spring 启动时查询数据库 (http://localhost:8180/products/2),服务器响应:

This application has no explicit mapping for /error, so you are seeing this as a fallback.

Thu Oct 26 01:29:12 COT 2017 There was an unexpected error (type=Internal Server Error, status=500). Missing URI template variable 'productoId' for method parameter of type Long

这个界面

package com.beitech.orders.repository;

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

import com.beitech.orders.model.Product;

public interface ProductJpaRepository extends JpaRepository<Product, Long> {

Product findByProductoId(Long productoId);

@Query(value = "SELECT * FROM PRODUCT WHERE PRODUCTO_ID = ?1", nativeQuery = true)
Product findByproductoId3(Long productoId);

}

这是 Controller :

package com.beitech.orders.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.beitech.orders.model.Product;
import com.beitech.orders.repository.ProductJpaRepository;

@RestController
@RequestMapping("/products")
public class ProductController {

@Autowired
private ProductJpaRepository productJpaRepository;

@GetMapping(value = "/allProducts")
public List<Product> findAll(){
return productJpaRepository.findAll();
}

@GetMapping(value = "/{productId}")
public Product findByProductoId(@PathVariable final Long productoId){
return productJpaRepository.findByProductoId(productoId);
}
}

最佳答案

你定义

@GetMapping(value = "/{productId}")

@PathVariable final Long productoId){

productIdproductoId 之间存在不匹配。如果您希望 productId 绑定(bind)到 Long productoId,那么您必须声明 @PathVariable(name="productId") 或者只是重命名productoIdproductId,反之亦然。

关于spring - REST API 调用 : Missing URI template variable 'productoId' for method parameter of type Long,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46947413/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com