【问题标题】:REST API call: Missing URI template variable 'productoId' for method parameter of type LongREST API 调用:Long 类型的方法参数缺少 URI 模板变量“productoId”
【发布时间】: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?
  • 可能问题在于您为路径变量和方法参数使用了不同的名称(productIdproductoId)。尝试通过指定变量名称来更改注解@PathVariablepublic Product findByProductoId(@PathVariable(name="productId") final Long productoId)。但我不能肯定。

标签: spring rest spring-boot


【解决方案1】:

你定义了

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

@PathVariable final Long productoId){

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

【讨论】:

  • 我累了,我没有看到这种不匹配。非常感谢。
猜你喜欢
  • 2018-06-19
  • 2019-05-29
  • 1970-01-01
  • 2013-11-17
  • 1970-01-01
  • 2018-03-24
  • 2015-02-25
  • 2021-12-13
  • 1970-01-01
相关资源
最近更新 更多