I'm using a REST api that requires a few fields to be set. My application should always set some of the fields to the same value. Is it possible to make those values "hard coded" in the interface with the feign definition (or somewhere else)?
My feign declaration looks like the example. Let's say I always want to use the same agent from this application. Is that doable?
@RequestLine("POST /files")
@Headers("Content-Type: application/json")
default FileMetadata addFile(@Param("file_name") String fileName,
@Param("agent") String agent,
@Param("md5") String md5,
@Param("file_type") String fileType) {
return new FileMetadata.Builder().build();
}
You have different alternatives, in the case you are using the Spring Annotations you could use defaultValue
annotation property to determine the default value
@RequestParam(value = "agent", required = false, defaultValue = "AnyAgent") String agent
But in the case you are using the netflix-feign annotations, and seems it is what you are doing, you would need to add in the path, i.e. in the @RequestLine
annotation:
static final String DEFAULT_VALUE_AGENT = "agent";
@RequestLine("POST /files?agent=" + DEFAULT_VALUE_AGENT)
....
But if you are refering to Body POST, you could do it using annotation @Body
:
@RequestLine("POST /files")
@Body("%7B\"file_name\": \"{user_name}\", \"agent\": \"" + DEFAULT_VALUE_AGENT +"\"%7D") //... other params..
....
Based on the Github repository documentation, if you are using Java 8, you could do a default method which calls the other method with a param constant. Just like next
@RequestLine("POST /files")
@Headers("Content-Type: application/json")
FileMetadata addFile(@Param("file_name") String fileName,
@Param("agent") String agent,
@Param("md5") String md5,
@Param("file_type") String fileType);
@RequestLine("POST /files")
@Headers("Content-Type: application/json")
default FileMetadata addFileConstant(@Param("file_name") String fileName,
@Param("md5") String md5,
@Param("file_type") String fileType) {
addFile(fileName, "constant_value", md5, fileType);
}
Personally, I think is more flexible than the other options I proposed before. And the idea works in POST Body as well if they are params.