springboot返回值国际化处理

一:启动类内配置

二:国际化文件

三,拦截器

package com.otcbi;

import com.ot
  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Spring Boot的控制器方法可以使用不同的返回值类型。以下是一些常见的返回值类型: 1. 字符串:可以直接返回一个字符串,它将作为响应的主体内容返回给客户端。 ```java @GetMapping("/hello") public String hello() { return "Hello, World!"; } ``` 2. 对象:可以返回一个Java对象,Spring Boot将自动将其转换为JSON格式返回给客户端。 ```java @GetMapping("/user/{id}") public User getUser(@PathVariable Long id) { User user = userService.getUserById(id); return user; } ``` 3. ResponseEntity:可以使用ResponseEntity类来自定义响应的状态码、头部信息和主体内容。 ```java @GetMapping("/user/{id}") public ResponseEntity<User> getUser(@PathVariable Long id) { User user = userService.getUserById(id); if (user != null) { return ResponseEntity.ok(user); } else { return ResponseEntity.notFound().build(); } } ``` 4. ModelAndView:可以返回一个ModelAndView对象,用于控制视图的渲染和模型数据的传递。 ```java @GetMapping("/home") public ModelAndView home() { ModelAndView modelAndView = new ModelAndView("home"); modelAndView.addObject("message", "Welcome to my website!"); return modelAndView; } ``` 5. 其他类型:还可以返回其他类型,如InputStream、File等,用于直接返回文件或流给客户端。 ```java @GetMapping("/file") public ResponseEntity<Resource> getFile() throws IOException { Resource file = fileService.getFile(); return ResponseEntity.ok() .header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + file.getFilename() + "\"") .body(file); } ``` 这只是一些常见的返回值类型,实际上还有很多其他的返回值类型可以使用。根据实际需求选择合适的返回值类型即可。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值