convert JSON Type to Byte array format in java

前端 未结 4 779
萌比男神i
萌比男神i 2021-02-05 02:25

I have a problem when I want to sending data using byte format in UDP protocol, the problem is when I try to create a data with type json object, I can\'t get the byte format of

4条回答
  •  佛祖请我去吃肉
    2021-02-05 02:51

    Use utility class from ObjectMapper of jackson-databind project, ie objectMapper.writeValueAsBytes(dto) returns byte[]

    @Autowired
    private ObjectMapper objectMapper;
    
    ContractFilterDTO filter = new ContractFilterDTO();
        mockMvc.perform(post("/api/customer/{ico}", "44077866")
                .contentType(MediaType.APPLICATION_JSON_UTF8_VALUE)
                .content(objectMapper.writeValueAsBytes(filter)))...
    

    Maven dependency:

    
    
        com.fasterxml.jackson.core
        jackson-databind
        2.8.8.1
    
    

提交回复
热议问题