如何在API中返回单例列表? 我正在学习使用Eclipse IDE和Spring Boog框架创建java API。因此,我面临一个我无法解决的语法问题。以下是我的代码供您参考: package first.microservice.moviecatalogservice.resources; import java.util.Collections; import java.util.List; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import first.microservice.moviecatalogservice.models.CatalogItem; @RestController @RequestMapping("/catalog") public class MovieCatalogResource { @RequestMapping("/{user_id}") public List<CatalogItem> getCatalog(@PathVariable("user_id") String user_id) { return Collections.singletonList( <CatalogItem> new CatalogItem(name: "DonJon", desc: "Test", rating: 4) ); } }
我希望输入URL模式以显示要在浏览器上显示的catalogitem的硬编码值 我在以下行中遇到错误:如何在API中返回单例列表? 我正在学习使用Eclipse IDE和Spring Boog框架创建java API。因此,我面临一个我无法解决的语法问题。以下是我的代码供您参考: package first.microservice.moviecatalogservice.resources; import java.util.Collections; import java.util.List; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import first.microservice.moviecatalogservice.models.CatalogItem; @RestController @RequestMapping("/catalog") public class MovieCatalogResource { @RequestMapping("/{user_id}") public List<CatalogItem> getCatalog(@PathVariable("user_id") String user_id) { return Collections.singletonList( <CatalogItem> new CatalogItem(name: "DonJon", desc: "Test", rating: 4) ); } },java,api,spring-boot,Java,Api,Spring Boot,我希望输入URL模式以显示要在浏览器上显示的catalogitem的硬编码值 我在以下行中遇到错误: return Collections.singletonList( <CatalogItem> new CatalogItem(name: "DonJon", desc: "Test", rating: 4) ); return Collections.sing
return Collections.singletonList(
<CatalogItem> new CatalogItem(name: "DonJon", desc: "Test", rating: 4)
);
return Collections.singletonList(
新目录项(名称:“DonJon”,描述:“测试”,评级:4)
);
错误说明:
The method singletonList(T) in the type Collections is not applicable for the arguments (CatalogItem)
Multiple markers at this line
- Syntax error on token "<", invalid Expression
- Syntax error on token ":", invalid AssignmentOperator
- name cannot be resolved to a variable
- Syntax error on token ":", invalid AssignmentOperator
- desc cannot be resolved to a variable
- Syntax error on token ":", invalid AssignmentOperator
- rating cannot be resolved to a variable
类型集合中的方法singletonList(T)不适用于参数(CatalogItem)
这条线上有多个标记
-标记“AFAIK Java不支持命名参数的语法错误。因此,此行
<CatalogItem> new CatalogItem(name: "DonJon", desc: "Test", rating: 4)
它应该可以工作我希望您的getCatalog
方法中存在编译时错误。请更改返回语句,如下所示:
public List<CatalogItem> getCatalog(@PathVariable("user_id") String user_id)
{
return Collections.singletonList(new CatalogItem("DonJon", "Test", 4));
}
public List getCatalog(@PathVariable(“user\u id”)字符串user\u id)
{
返回集合。单音列表(新的CatalogItem(“DonJon”,“Test”,4));
}
在Eclipse中
单击Windows>首选项,然后选择左侧的Maven
选中“启动时下载存储库索引更新”框。
- 或者,选中下载工件源和下载工件JavaDoc。
单击“确定”。警告将不再显示
重新启动Eclipse
您面临的确切问题是什么?您是否遇到一些编译时错误。请共享您的日志/错误以更好地理解它。您从浏览器调用的URL是什么?此行有多个标记-令牌上的语法错误“我在CatalogItem名称下得到一条红线,并且如上所述得到以下错误。是的,刚刚注意到。谢谢。是的,你明白了,他面临着同样的问题:)这一行有多个标记-令牌上的语法错误“有上述问题^@PelochoYep,刚刚注意到。谢谢。
new CatalogItem("DonJon", "Test", 4)
public List<CatalogItem> getCatalog(@PathVariable("user_id") String user_id)
{
return Collections.singletonList(new CatalogItem("DonJon", "Test", 4));
}