CatalogItem.java
package com.example.moviecatalogservice;
public class CatalogItem {
private String name;
private String desc;
private int rating;
public CatalogItem(String name, String desc, int rating){
this.name = name;
this.desc = desc;
this.rating = rating;
}
public int getRating(){
return rating;
}
public void setRating(){
this.rating = rating;
}
public String getName(){
return name;
}
public void setName(){
this.name = name;
}
public String getDesc(){
return desc;
}
public void setDesc(){
this.desc = desc;
}
}
MovieCatalogService.java
package com.example.moviecatalogservice;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.an notation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.Collections;
import java.util.List;
@RestController
@RequestMapping("/catalog")
public class MovieCatalogResource {
@RequestMapping("/{userId}")
//public List<CatalogItem> getCatalog(@PathVariable("userId") String userId){
public List<CatalogItem> getCatalog(@PathVariable("userId") String userId){
return Collections.singletonList(
new CatalogItem(name: "transformers", desc:"Test", rating:4)
);
}
}
答案 0 :(得分:1)
更改
new CatalogItem(name: "transformers", desc:"Test", rating:4)
收件人
new CatalogItem("transformers", "Test", 4)
在CatalogItem中必须具有匹配的CatalogItem()构造函数 实体或模型
在change at line no 17
的{{1}}之后,如下图所示
MovieCatalogResource.java
工作示例
Controller.java
@RestController
@RequestMapping("/catalog")
public class MovieCatalogResource {
@RequestMapping("/{userId}")
//public List<CatalogItem> getCatalog(@PathVariable("userId") String userId){
public List<CatalogItem> getCatalog(@PathVariable("userId") String userId){
return Collections.singletonList(
new CatalogItem("transformers", "Test", 4)
);
}
}
User.java
@GetMapping("/{id}")
public List<User> getUser(@PathVariable(name="id") int id)
{
return Collections.singletonList(
new User(1,"username")
);
}
答案 1 :(得分:1)
您为什么这样做:
new CatalogItem(name: "transformers", desc:"Test", rating:4)
代替此:
new CatalogItem("transformers", "Test", 4)
MovieCatalogResource.java的第17行?