-1

I'm using OpenFeign/feign to send API requests:

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}") is not working, because it converts the object into a string:

[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)

Is there any way to automatically convert objects into JSON objects?

This is how I build the 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))
MonkeyMonkey
  • 826
  • 1
  • 6
  • 19

2 Answers2

0

From the documentation of OpenFeign when we use

@Body("{bulk}")
public void bulk(@Param("cid") Long cid, @Param("bulk") Bulk bulk)

Bulk object toString() is used to map the parameter value. If you want to convert the Bulk object to necessary json string, just do the following

public void bulk(@Param("cid") Long cid, @RequestBody Bulk bulk)

You don't have to put @Body annotation. Hope this helps.

laaptu
  • 2,963
  • 5
  • 30
  • 49
-1

You can try

def feignJson = JsonSlurper.toJson(feignObject)

and then, optionally

def pretyJsonOutput = JsonOutput.prettyPrint(feignJson)

check details: http://groovy-lang.org/json.html

yeugeniuss
  • 180
  • 1
  • 7