I'm trying to use an interface as the RequestBody in Feign, but Feign is creating an empty object as the request. Is this not possible or am I doing something wrong here? I could not find anything on that topic so far.
This is a simplified example of what I am trying to do (in reality there are 3 different kinds of requests)
interface BookingClient {
#RequestLine("POST /booking")
#Headers("Content-Type: application/json")
fun createBooking(request: BookingRequest): BookingResponse
}
interface BookingRequest
data class NormalBooking(
val product: String
): BookingRequest
data class DiscountedBooking(
val product: String,
val discountCode: String
): BookingRequest
// Client Configuration
val client = Feign.builder()
.client(feign.okhttp.OkHttpClient())
.errorDecoder(BadEntityErrorDecoder())
.encoder(JacksonEncoder())
.decoder(JacksonDecoder(listOf(KotlinModule(), JavaTimeModule())))
.logger(feign.Logger.JavaLogger())
.logLevel(feign.Logger.Level.FULL)
.target(BookingClient::class.java, mockServer.getUrl())
If I now call createBooking() with either implementation, Feign always serializes
{}
instead of
{
"product": "productA"
}
and
{
"product": "productA",
"discountCode": "discountCode"
}
The problem is parameter type, when we invoke Feign.target(...), Feign start to parse your BookingClient to a Http request template, and body type is BookingRequest, so Feign always serializes "{}"
interface BookingClient {
#RequestLine("POST /booking")
#Headers("Content-Type: application/json")
// BookingRequest should change to NormalBooking or DiscountedBooking
fun createBooking(request: BookingRequest): BookingResponse
}
Related
I haven an endpoint POST /api/marketplace/add that accepts a DTO object as request body. When I send the body below with platformName field set , server accepts request and processes it with no problem. But when I only try to change field platformName to null I get Http 404 error from server. I debugged the request and found out that it even can not reach controller method. I also got no trace from that error. What might be the cause that makes API respond differently to same request?
below
{
"platformName": "Trendyol",
"commissionAmounts": [
{
"amount": 23.45,
"categoryInfos": [
{
"categoryName": "Game"
}
],
"isCategoryBasedPricing": true
}
],
"shipmentAmounts": [
{
"amount": 23.45,
"scaleInfo": {
"order": 0,
"lowerBound": 0,
"upperBound": 0
},
"volumeInfo": {
"order": 0,
"lowerBound": 0,
"upperBound": 0
},
"isVolumeBasedPricing": true
}]
}
EDIT: dto model is
#Generated
public class MarketPlaceDTO {
#JsonProperty("platformName")
private String platformName;
#JsonProperty("commissionAmounts")
#Valid
private List<CommissionInfoDTO> commissionAmounts = new ArrayList<>();
#JsonProperty("shipmentAmounts")
#Valid
private List<ShipmentInfoDTO> shipmentAmounts = new ArrayList<>();
Controller is implementing swagger generated api interface. with postmapping and requestbody annotations.
#RequiredArgsConstructor
#RestController
public class MarketPlaceApiController implements MarketplaceApi {
private final MarketPlaceDAOService marketPlaceDAOService;
#Override
public ResponseEntity<BaseResponseDTO> addMarketPlace(MarketPlaceDTO
marketPlaceDTO) {
BaseResponseDTO dto =
marketPlaceDAOService.addMarketPlace(marketPlaceDTO);
return ResponseEntity.ok(dto);
}
}
Swagger generated api interface
#RequestMapping(
method = RequestMethod.POST,
value = "/marketplace/add",
produces = { "application/json", "application/xml" },
consumes = { "application/json" })
default ResponseEntity<BaseResponseDTO> _addMarketPlace(
#Parameter(name = "MarketPlaceDTO", description = "Add new
marketplace with given request body", required = true) #Valid
#RequestBody MarketPlaceDTO marketPlaceDTO) {
return addMarketPlace(marketPlaceDTO);
}
Response is
{
"timestamp": 1666866382906,
"status": 404,
"error": "Not Found",
"path": "/marketplace/add"
}
Obviously, that you use an endpoint with #RequestBody where body is a DTO.
And on trying to call this endpoint Spring Web first should match that a model in your request payload matches a require object in #RequestBody argument.
Ideally, using DTO as a request model is not a good idea. But I don't see your structure and cannot say if it's a problem or not.
The simple solution in your case is preparation (annotating) your DTO with specific JSON annotations:
#JsonInclude
#JsonIgnoreProperties(ignoreUnknown = true)
public class YourDTO {
private String platformName;
}
and for Controller add class annotation #Validated; for #RequestBody add #Valid annotation.
Recommendation: use request models for incoming objects, and later converters to DTO/entities with ability to response them with filtering (or in complex cases add also response model - usually it's overhead).
My problem was global exception handler component annotated with #ControllerAdvice. I tried to handle validation exceptions and forgot to add #ResponseBody to my handler methods which is in my case probabaly required. That somehow caused server to send http 404 message when any input validation exception was thrown. After I made changes , Exceptions was handled correctly by handler component.
#ControllerAdvice
#ResponseBody // this resolved my issue.
public class MVCExceptionHandler {
#ExceptionHandler(MethodArgumentNotValidException.class)
#ResponseStatus(HttpStatus.BAD_REQUEST)
public BaseErrorResponse
methodArgumentExceptions(MethodArgumentNotValidException e){
return BaseErrorResponse.builder()
.errorMessage(AppError.INVALID_OR_MISSING_USER_INPUT.getErrorMessage())
.errorCode(AppError.INVALID_OR_MISSING_USER_INPUT.getErrorCode())
.errorTime(Date.from(Instant.now())).build();
}
In my app I have one endpoint under /my-endpoint path which supports only post method. It accepts a body that must be compatible with my MyRequest class.
#Validated
data class MyRequest(
#get:JsonProperty("age", required = true)
#field:Size(min = 3, max = 128, message = "age must be between 3 and 128")
val age: String,
#get:JsonProperty("zip_code", required = true)
#field:Pattern(regexp = "\\d{2}-\\d{3}", message = "address.zip_code is invalid. It is expected to match pattern \"\\d{2}-\\d{3}\"")
val zipCode: String
)
And my controller looks like this
#PostMapping("/my-endpoint")
fun myEndpoint(
#Valid #RequestBody request: MyRequest,
): Mono<ResponseEntity<MyResponse>> {
return myService.processRequest(request)
.map { ResponseEntity.ok().body(it) }
}
Each time I receive some request to THIS particular endpoint (I have other endpoints but them should be ignored) - I'd like to publish a message to my pubsub consisting raw request body (as a string) - no matter whether the request body was valid or not.
How to intercept the request to be able to publish the message - still having the endpoint working ?
I think you could implement your own WebFilter. Filter the API path through exchange.getRequest().getPath() using simple if block and get the body through exchange.getRequest().getBody()
#Component
#RequiredArgsConstructor
#Slf4j
public class MyFilter implements WebFilter {
private final MyPublisher myPublisher;
#Override
public Mono<Void> filter(ServerWebExchange exchange, WebFilterChain chain) {
if (pathMatches(exchange.getRequest().getPath()) {
return exchange.getRequest().getBody()
.map(dataBuffer -> {
final String requestBody = dataBuffer.toString(StandardCharsets.UTF_8));
this.myPublisher.publish(requestBody).subscribe();
return exchange;
}).then(chain.filter(exchange));
}
return chain.filter(exchange);
}
}
We can write the query resolver layer as below
#DgsData(parentType = "Query", field = "answersByQuestionUuid")
public List<Answer> answersByQuestionUuid(#InputArgument("questionUuid") UUID questionUuid,
#InputArgument("enhancedContent") boolean enhancedContent,
#InputArgument("templateName") String templateName) {
if (enhancedContent) {
return getStructuredAnswersByQuestionUUID(questionUuid.toString(), templateName);
}
return getAnswersByQuestionUUID(questionUuid);
}
How I can get the HTTP header in the resolver.
In addition to DGS input arguments, you can use the #RequestHeader annotation from the Spring framework to receive HTTP request header values. For example:
public List<Answer> answersByQuestionUuid(#InputArgument("questionUuid") UUID questionUuid,
#RequestHeader("Content-Type") String contentType) {
Here is my spring boot controller:
#RestController
#RequestMapping("/api/v1/geo-media/")
class GeoMediaController {
#PostMapping("create")
fun saveMedias(#RequestHeader("token") token: String,
#RequestParam mediaGroup: WalkMediaGroup):Result<String> {
}
}
Here is the parameter class:
class WalkMediaGroup (
val wid: Long,
val mediaGroup: MediaGroup
)
class MediaGroup(
val node: Node,
val medias: List<Media>
)
class Media (
val type: Int,
val content: String,
val remark: String
)
Then my Retrofit service class:
interface ApiService {
#Headers("Content-Type: application/json")
#POST("geo-media/create")
fun createGeoMedias(
#Body mediaGroup: WalkMediaGroup
): Call<Result<String>>
}
Above is the key parts of my code. I don't know why that I received error.
{
"timestamp":"2019-10-25T11:46:10.247+0000",
"status":400,
"error":"Bad Request",
"message":"Required WalkMediaGroup parameter 'mediaGroup' is not present",
......
}
From Android (or whatever the client it maybe) you're posting JSON body. In order to receive that JSON body parameter in server you've to use Spring Boot #RequestBody() annotation instead of #RequestParam() annotation.
So, in your Spring Boot API code just change the annotation from #RequestParam() to #RequestBody(). That's it!
#PostMapping("create")
fun saveMedias(
#RequestHeader("token") token: String,
/* notice the change here ==> */ #RequestBody mediaGroup: WalkMediaGroup):Result<String> {
}
From past few days i'm trying to implement the Spring cloud stream messaging system using RestController, but it is not happening through the current implementation.
For this sample code i'm going to add RestController
#EnableBinding(Source.class)
#EnableConfigurationProperties(TimeSourceOptionsMetadata.class)
public class TimeSource {
#Autowired
private TimeSourceOptionsMetadata options;
#InboundChannelAdapter(value = Source.OUTPUT)
public String timerMessageSource() {
return new SimpleDateFormat(this.options.getFormat()).format(new Date());
}
}
But the #InboundChannelAdapter cannot accept any parameters from RequestMapping Get Method URL.At the end what i need is to add message to the broker using Restful API Get method from api call. which is the best way to do it?, I couldn't figure out any best process from internet.
spring cloud team already provided a source application that listens for HTTP requests and emits the body as a message payload. If the Content-Type matches text/* or application/json, the payload will be a String, otherwise the payload will be a byte array.
github link
You can go with this or if you want to write it yourself, you can do it like below:
#RestController
#EnableBinding(Source.class)
public class RestSource {
#Autowired
private Source channels;
#RequestMapping(path = "/", method = POST, consumes = {"application/json" })
#ResponseStatus(HttpStatus.ACCEPTED)
public void handleRequest(#RequestBody String body, #RequestHeader(HttpHeaders.CONTENT_TYPE) Object contentType) {
sendMessage(body, contentType);
}
private void sendMessage(Object body, Object contentType) {
channels.output().send(MessageBuilder.createMessage(body,
new MessageHeaders(Collections.singletonMap(MessageHeaders.CONTENT_TYPE, contentType))));
}
}