Spring中如何将JSON数组字段直接恢复为byte[]数组
解决方案:直接在Spring Controller中获取byte[]类型的消息
当然可以直接拿到byte[]类型的消息,不用先转成字符串再处理,这里给你两种最常用的实现方式:
方法一:使用自定义DTO类(推荐)
这种方式最清晰,也符合Spring的最佳实践,让消息转换器自动帮你完成类型转换。
- 创建一个接收请求的DTO类:
public class MessageRequest { private byte[] message; // Getter 和 Setter 方法 public byte[] getMessage() { return message; } public void setMessage(byte[] message) { this.message = message; } }
- 修改你的Controller方法,用这个DTO作为
@RequestBody的参数:
import java.util.Arrays; import java.util.HashMap; import java.util.Map; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.http.HttpHeaders; import org.springframework.http.HttpStatus; import org.springframework.http.MediaType; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestHeader; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.ResponseBody; @RequestMapping(value = "/json", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE) @ResponseBody public ResponseEntity<Map<String, Object>> jsonReceiver(@RequestHeader Map<String, String> headers, @RequestBody MessageRequest request) { byte[] messageBytes = request.getMessage(); LOGGER.info("Message bytes: {}", Arrays.toString(messageBytes)); // 这里可以直接使用byte[]做后续处理,比如转成字符串:new String(messageBytes) Map<String, Object> response = new HashMap<>(); response.put("status-code", "1000"); response.put("success", "true"); HttpHeaders respHeaders = new HttpHeaders(); respHeaders.add("Pragma", "ack"); return new ResponseEntity<>(response, respHeaders, HttpStatus.ACCEPTED); }
原理说明
Spring默认使用的Jackson消息转换器(MappingJackson2HttpMessageConverter)会自动识别JSON中的数字数组(比如[72,69,76,76,79]),并将其转换为Java的byte[]类型,完全不需要你手动处理字符串解析。
方法二:直接使用Map<String, byte[]>(无需DTO)
如果不想额外创建DTO类,也可以直接将@RequestBody的类型指定为Map<String, byte[]>,前提是Jackson的配置支持这种转换(Spring Boot默认已经支持):
@RequestMapping(value = "/json", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE) @ResponseBody public ResponseEntity<Map<String, Object>> jsonReceiver(@RequestHeader Map<String, String> headers, @RequestBody Map<String, byte[]> request) { byte[] messageBytes = request.get("message"); LOGGER.info("Message bytes: {}", Arrays.toString(messageBytes)); // 后续处理逻辑和之前一致 Map<String, Object> response = new HashMap<>(); response.put("status-code", "1000"); response.put("success", "true"); HttpHeaders respHeaders = new HttpHeaders(); respHeaders.add("Pragma", "ack"); return new ResponseEntity<>(response, respHeaders, HttpStatus.ACCEPTED); }
注意事项
这种方式虽然简洁,但如果请求参数结构变复杂,可读性和维护性会不如DTO的方式,所以更推荐第一种方法。
最后验证一下你的curl命令是完全正确的,发送的{"message":[72,69,76,76,79]}正好对应字符串"hello"的ASCII字节数组,两种方法都能正确解析出byte[]。
内容的提问来源于stack exchange,提问作者Ruchira Nawarathna




