Java 为什么在Spring中传递参数名对RestTemplate是必需的

Java 为什么在Spring中传递参数名对RestTemplate是必需的,java,spring,rest,spring-boot,resttemplate,Java,Spring,Rest,Spring Boot,Resttemplate,我实现了api https://theysaidso.com/api/#qod 使用弹簧支撑模板。我的问题是,如果我像下面这样创建url,它就会工作。但如果我从括号中删除param名称,它不会删除并返回错误。有什么想法吗?谢谢 这项工作: QuoteResponse quoteResponse= this.restTemplate.getForObject("http://quotes.rest/qod.json?category= {param}", QuoteResponse.c

我实现了api

https://theysaidso.com/api/#qod 
使用弹簧支撑模板。我的问题是,如果我像下面这样创建url,它就会工作。但如果我从括号中删除param名称,它不会删除并返回错误。有什么想法吗?谢谢

这项工作:

QuoteResponse quoteResponse=    
this.restTemplate.getForObject("http://quotes.rest/qod.json?category=
{param}", QuoteResponse.class, category);
这并不重要

QuoteResponse quoteResponse=    
this.restTemplate.getForObject("http://quotes.rest/qod.json?category={}", 
QuoteResponse.class, category);
我想这两种情况都会转化为以下内容(将值传递作为类别字符串的灵感)

"http://quotes.rest/qod.json?category=inspire"
更新(添加更多代码):控制器

@Autowired
QuoteService quoteService;

@RequestMapping(value="/ws/quote/daily", produces=MediaType.APPLICATION_JSON_VALUE,method=RequestMethod.GET)
public ResponseEntity<Quote> getDailyQuote(@RequestParam(required=false) String category){
    Quote quote = quoteService.getDaily(category);
    if(quote==null)
        return new ResponseEntity<Quote>(HttpStatus.INTERNAL_SERVER_ERROR);
    return new ResponseEntity<Quote>(quote,HttpStatus.OK);

}
当您发出这样的请求时,这意味着您正在将PathVariable传递到控制器中,该控制器由控制器参数中的
@PathVariable
注释处理

如果控制器上有
@PathVariable
,则需要路径变量来完成工作

restTemplate.getForObject("http://quotes.rest/qod.json?category={}", 
QuoteResponse.class, category); 

当您发出这样的请求时,您没有发送任何需要的PathVariable请求,因此不起作用,并抛出粘贴控制器代码帮助我们了解这一点。@Lovababu补充说,因为您有“required=false”,spring不会强迫您传递查询参数。类别={any u name},这里是{any u name}只是RestTemplate的一个占位符,它在进行实际rest调用之前用提供的uriVariable替换此占位符。@谢谢!我假定您的意思是RequestParameter而不是PathVariable,因为据我所知,path变量应该是/quotes.rest/qod.json/{cat}和//quotes.rest/qod.json?category={cat}是针对请求参数的。但对于这一个问题,RequestParam应该有一个名称,对吗?而它适用于传递的任何名称,即.restTemplate.getForObject(“{param}”,quoterResponse.class,category)..和。这也适用于。this.restTemplate.getForObject({param12345},quoterResponse.class,category)
this.restTemplate.getForObject("http://quotes.rest/qod.json?category=
{param}", QuoteResponse.class, category); 
restTemplate.getForObject("http://quotes.rest/qod.json?category={}", 
QuoteResponse.class, category);