【发布时间】:2017-10-26 06:38:38
【问题描述】:
我试图在 Spring Boot 中对数据库 (http://localhost:8180/products/2) 进行查询,服务器响应:
此应用程序没有显式映射 /error,因此您将其视为后备。
Thu Oct 26 01:29:12 COT 2017 出现意外错误 (类型=内部服务器错误,状态=500)。缺少 URI 模板 Long 类型方法参数的变量“productoId”
这个界面
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);
}
这是控制器:
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);
}
}
【问题讨论】:
-
你能分享更多的堆栈跟踪层吗?
-
另外,你为什么使用 final Long productoId?
-
可能问题在于您为路径变量和方法参数使用了不同的名称(productId 和 productoId)。尝试通过指定变量名称来更改注解
@PathVariable:public Product findByProductoId(@PathVariable(name="productId") final Long productoId)。但我不能肯定。
标签: spring rest spring-boot