看了各种博客,相同的层非要取个各种名字,真的是给我整麻了,最后总结了一下,就定为这种格式了。
积分资源:
https://download.youkuaiyun.com/download/qq_60567426/88405619?spm=1001.2014.3001.5501
在Postman的测试效果:
当然这返回的最终是列表形式,如果要跟前端结合,这是不合理的,要转为json串才行,Springboot想要最终得到json串格式,就需要将结果类型定为HashMap。
!--我这里是List类型 --!
如果想使用HashMap返回json串,就用以下函数:
package com.example.demo.Controller;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import com.example.demo.Entity.Questionnaire;
import com.example.demo.Service.QuestionnaireService;
@RestController
@RequestMapping("/questionnaire")
public class QuestionnaireController {
@Autowired
private QuestionnaireService questionservice;
@ResponseBody
@GetMapping("/getAllquestionnaire")
public Map<String,Object> getAllquestionnaire() {
Map<String,Object> map=new HashMap<>();
List<Questionnaire> questionnaires =questionservice.getAllquestionnaires();
map.put("问卷信息",questionnaires);
return map;
}
}
Postman的测试效果:
成功设置为一个键值对的形式。