【发布时间】:2020-01-12 11:04:17
【问题描述】:
重定向只返回一个简单的字符串。它无法识别重定向。
响应只打印“redirect:/getUser/”
你能帮助我吗?
已经谢谢了。
the return should be the list of user with the new user added
@Slf4j
@RestController
public class UserController {
@Autowired
UserService userService;
@GetMapping(value = "/getUser", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public List<UserDTO> getUsers() throws Exception {
List<UserDTO> oListUsers = new ArrayList<UserDTO>();
try {
oListUsers = userService.findAll();
} catch (Exception ex) {
log.error("Error getUsers. Cause: " + ex.getMessage());
throw new Exception("Data can not be retrieved. Cause: " + ex.getMessage());
}
return oListUsers;
}
@PostMapping("/createUser")
public String createUser(@Valid @RequestBody UserDTO newUser) throws Exception {
try {
userService.save(newUser);
} catch (Exception ex) {
log.error("Error createUser. Cause: " + ex.getMessage());
throw new Exception("Data can not be save. Cause: " + ex.getMessage());
}
return "redirect:/getUser/";
}
}
【问题讨论】:
-
createUser 方法未遵循 REST 。我建议它进行相应的更改并返回 ResponseEntity
标签: java spring-boot spring-mvc