代码之家  ›  专栏  ›  技术社区  ›  jon hanson

Akka HTTP Java客户端示例发布JSON消息

  •  0
  • jon hanson  · 技术社区  · 7 年前

    HttpRequest req =
        HttpRequest.POST("/user")
            .withEntity(HttpEntities.create(
                ContentTypes.APPLICATION_JSON,
                "{\"some\": json}"
            ));
    

    1 回复  |  直到 7 年前
        1
  •  2
  •   Bartosz Kiebdaj    7 年前

    class RequestProducer {
        private ObjectMapper objectMapper;
    
        public RequestProducer(ObjectMapper objectMapper) {
           this.objectMapper = objectMapper;
        }
    
        HttpRequest post(SomePojo somePojo) {
            return HttpRequest.POST("/user")
                .withEntity(HttpEntities.create(
                  ContentTypes.APPLICATION_JSON,
                  objMapper.writeValueAsString(somePojo)));        
        }
    }