【发布时间】:2017-02-01 21:12:06
【问题描述】:
我正在尝试创建一个以 JSON 形式返回对象列表的端点。
当前对象结构如下:
units: [
{
id: #,
order: #,
name: ""
concepts: [
{
id: #
name: ""
},
...
]
},
...
]
具有 4 个属性的单位列表,其中一个是具有 2 个其他属性的对象列表。这就是我希望得到的结果。
我目前正尝试在我的UnitController 中执行以下操作:
@RequestMapping(method = RequestMethod.GET, produces = "application/json; charset=UTF-8")
public @ResponseBody List<Unit> getUnits() {
return unitService.findAll();
}
但每当我运行应用程序并尝试curl localhost:8080/units 时,我什么都得不到。这可能是因为我在控制器中有另一种方法,比如这个:
@RequestMapping("")
public String index(Map<String, Object> model) {
List<Unit> units = unitService.findAll();
model.put("units", units);
return "units/index";
}
有人可以帮我解决这个问题并告诉我我做错了什么吗?我真的很感激。
编辑
好的,所以我将注释移到了类的顶部
@Controller
@RequestMapping(value = "/units", method = RequestMethod.GET, produces = "application/json; charset=UTF-8")
public class UnitController extends BaseController {
...
}
并尝试了这样的端点:
@RequestMapping(method = RequestMethod.GET, value = "/units.json")
public @ResponseBody List<Unit> getUnits() {
return unitService.findAll();
}
但它curl localhost:8080/units.json 仍然没有给我任何回应。
还忘了提到我的application.properties 文件没有server.contextPath 属性。
【问题讨论】:
标签: java json spring-boot endpoint