色婷婷狠狠18禁久久YY,CHINESE性内射高清国产,国产女人18毛片水真多1,国产AV在线观看

java 后臺返回json

呂致盈1年前8瀏覽0評論

Java后臺返回JSON是一種常見的編程方式,它可以將Java后臺處理的數據轉換為JSON格式,并返回給前端頁面進行展示。這種方式可以使數據傳輸更加高效,并且有效地節省了網絡開銷。

@RestController
@RequestMapping("/user")
public class UserController {
@Autowired
private UserService userService;
@GetMapping
public Listusers() {
return userService.findAll();
}
@GetMapping("/{id}")
public User user(@PathVariable Long id) {
return userService.findById(id);
}
@PostMapping
public ResponseEntitycreate(@RequestBody User user) {
userService.save(user);
return new ResponseEntity("User has been created!", HttpStatus.CREATED);
}
@PutMapping("/{id}")
public ResponseEntityupdate(@PathVariable Long id, @RequestBody User updatedUser) {
User user = userService.findById(id);
if (user == null) {
return new ResponseEntity("User not found!", HttpStatus.NOT_FOUND);
}
userService.update(updatedUser);
return new ResponseEntity("User has been updated!", HttpStatus.OK);
}
@DeleteMapping("/{id}")
public ResponseEntitydelete(@PathVariable Long id) {
User user = userService.findById(id);
if (user == null) {
return new ResponseEntity("User not found!", HttpStatus.NOT_FOUND);
}
userService.delete(id);
return new ResponseEntity("User has been deleted!", HttpStatus.OK);
}
}

上述代碼展示了一個用戶管理的后臺控制器,其中使用了@RestController注解使其支持JSON格式的數據返回。使用@GetMapping、@PostMapping、@PutMapping和@DeleteMapping注解進行GET、POST、PUT和DELETE請求方法的映射,并使用@ResponseBody注解將返回的 Java 對象轉換為 JSON 字符串。