我正在使用 OpenFeign/feign 发送 API 请求:
import feign.Body
import feign.Param
import feign.RequestLine
interface ApiServiceInterface {
@RequestLine("POST /container/{cid}/key/bulk")
@Body("{bulk}")
public void bulk(@Param("cid") Long cid, @Param("bulk") Bulk bulk)
}
@Body("{bulk}")
不起作用,因为它将对象转换为字符串:
[ApiServiceInterface#bulk] ---> POST http://localhost:5050/container/11/key/bulk HTTP/1.1
[ApiServiceInterface#bulk] Content-Length: 45
[ApiServiceInterface#bulk]
[ApiServiceInterface#bulk] com.***.tns.hoth.key.bulk.Bulk@5dd5fb75
[ApiServiceInterface#bulk] ---> END HTTP (45-byte body)
有没有办法自动将对象转换为 JSON 对象?
这就是我构建的方式Feign.Builder
:
def mapper = new ObjectMapper()
mapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"))
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
mapper.setPropertyNamingStrategy(PropertyNamingStrategy.SNAKE_CASE)
mapper.readerForUpdating(this)
return Feign.builder()
.logger(new Logger.JavaLogger().appendToFile("logs/hoth.log"))
.logLevel(Logger.Level.BASIC)
.decoder(new JacksonDecoder(mapper))
.encoder(new JacksonEncoder(mapper))