50,526
社区成员
发帖
与我相关
我的任务
分享
@RequestMapping(value = "/findAllStudent", method = RequestMethod.GET)
@ResponseBody
public String findAllStudent() {
List<Student> students = studentService.findAll();
String result = "";
for (Student s : students
) {
result = JSON.toJSONString(s) + result;
}
return result;
}
@RestController
@RequestMapping("")
public class MainController {
@GetMapping("302")
public ResponseEntity Test302(){
HttpHeaders headers = new HttpHeaders();
headers.add("Location", "http://www.baidu.com/");
return new ResponseEntity( "", headers, HttpStatus.TEMPORARY_REDIRECT);
}
但是,api一般是返回json,给前端的ajax调用,你有时返回json,有时返回302跳转,给前端同学造成不必要的工作和逻辑处理。
response.addHeader("location", "https://www.baidu.com/");
response.setStatus(302);