I am trying to add "Content-Disposition" header to my api response. This api downloads a file. Here is how I configured the json specification file for openapi-generator (for java spring):
"/download": {
"get": {
"description": "Returns the file",
"operationId": "getFile",
"responses": {
"200": {
"description":"A file",
"headers": {
"content-disposition": {
"description": "Content disposition header",
"schema": {
"type": "string",
"example": "attachment; filename=keyFile.pem"
}
}
},
"content": {
"application/octet-stream": {
"schema": {
"type": "string",
"format": "binary"
}
}
}
},
"400": {
"description": "Delete operation not allowed"
},
"404": {
"description": "File not found"
}
}
}
}
But the code generated by openapi-generator does not add the header to the response. The swagger UI also does not show any header in the response.
The answer here says that adding headers is not supported in spring generator for openapi-generator. Is this true? If yes then, is there an alternative to this?
Can somebody help me with this? I am pretty new to spring-boot and open-api.
Related
We have implemented swagger using OpenAPI 3.
We have a requirement where the external request will be sent with a path "{host}/v1/names" which will then be interpreted at gateway level and forwarded as "{host}/api/v1/names" to the deployed springboot application
In the springboot application we are using rest controller with
#RequestMapping(value="/api/v1/names")
So, when OpenApi Json is generated to be used in swagger, it defines path as "/api/v1/names". However, since external customer would use "/v1/names" to access the API, we want to show "/v1/names" in swagger and OpenApi json
Current JSON looks like this:
{
"openapi": "3.0.1",
"info": {
"title": "External API",
"description": "This set of API is used to serve external clients",
"version": "0.1"
},
"servers": [
{
"url": "https://api.xyz.net/",
"description": "Live"
}
],
"paths": {
"/api/v1/names": {
"get": {
"summary": ".",
"operationId": "getNames",
"parameters": [
{
"name": "pageable",
"in": "query",
"required": true,
"schema": {
"$ref": "#/components/schemas/Pageable"
}
}
],
"responses": {
"200": {
"description": "List of names",
"content": {
"*/*": {
"schema": {
"$ref": "#/components/schemas/Pageable response"
}
}
}
}
}
}
}
}
}
Expected JSON (Different "path" value):
{
"openapi": "3.0.1",
"info": {
"title": "External API",
"description": "This set of API is used to serve external clients",
"version": "0.1"
},
"servers": [
{
"url": "https://api.xyz.net/",
"description": "Live"
}
],
"paths": {
"/v1/names": {
"get": {
"summary": ".",
"operationId": "getNames",
"parameters": [
{
"name": "pageable",
"in": "query",
"required": true,
"schema": {
"$ref": "#/components/schemas/Pageable"
}
}
],
"responses": {
"200": {
"description": "List of names",
"content": {
"*/*": {
"schema": {
"$ref": "#/components/schemas/Pageable response"
}
}
}
}
}
}
}
}
}
I have been trying to figure out any way to do this through some configuration or annotation, but not found anything yet.
Thanks in advance for your suggestions and help.
I'm constructing a message to be sent through my spring boot application.
I was testing out the templates and I have created one where most of the elements are static except a link that needs to be generated by the code and added to the Json.
Currently the Json message looks like this:
{
"blocks": [
{
"type": "context",
"elements": [
{
"type": "image",
"image_url": "https://api.slack.com/img/blocks/bkb_template_images/highpriority.png",
"alt_text": "High Priority"
},
{
"type": "mrkdwn",
"text": "*High Priority*"
}
]
},
{
"type": "divider"
},
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": "*Hercules Platform Status Response failed Messages*"
}
},
{
"type": "divider"
},
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": "*Please click the link to download the file*"
}
},
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": "*<LINK|SOME_LINK>*"
}
}
]
}
I'm not sure how to construct this Json in my spring boot application. Since most of it is static, should I just load this template as a string and append the last link section?
I'm not able to figure out the slack classes in java to build such a message.
In my play framework application, I have registered APIs in route file as:
POST /api/rmt-create-request controllers.Api.CreateRMTRequestForm
On action of controller, I am using following code to access formData submitted with form submit as :
public Result CreateRMTRequestForm()
{
Map<String, String[]> params = request().body().asMultipartFormData().asFormUrlEncoded();
Its working fine as API when I submit the form with forntend application.
I am trying to create APIs documentation with swagger.ui in which within swagger.json file I have written following JSON data.
"paths": {"/api/rmt-create-request": {
"post": {
"tags": [
"RMT APIs"
],
"description" : "Return newly created request data",
"operationId": "create-new-rmt-request",
"consumes": ["application/x-www-form-urlencoded"],
"parameters": [
{
"name": "rootNodeName",
"in": "formData",
"description": "Root node class name for item",
"schema": {
"type": "string"
}
}
],
"responses": {
"200": {
"description": "OK",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/rmt-request-data"
}
}
}
},
"default": {
"$ref": "#/components/responses/default"
}
}
}
},
While inspecting RequestHeader data, its not showing content-Type property with value 'multipart/form-data' as well as formData are not attached, which makes controller to throw null exception.
Can anyone help whats missing in swagger.json file ?
You are mixing OpenAPI 2.0 and 3.0 syntax.
In OpenAPI 3.0, request body (including form data) is defined using the requestBody keyword instead of in: formData parameters.
Also, OAS3 does not use consumes. The media types consumed by the operation are specified inside the requestBody.
"paths": {
"/api/rmt-create-request": {
"post": {
"tags": [
"RMT APIs"
],
"description": "Return newly created request data",
"operationId": "create-new-rmt-request",
"requestBody": {
"content": {
"multipart/form-data": { // or "application/x-www-form-urlencoded" - depending on what you need
"schema": {
"type": "object",
"properties": {
"rootNodeName": {
"type": "string",
"description": "Root node class name for item"
}
}
}
}
}
}
}
}
}
More information: Describing Request Body
The body option is used to control the body of an entity enclosing request (e.g., PUT, POST, PATCH). in PHP it is used like this
$client->request('POST', '/post', ['body' => $stream]);
But I want to use in java like http://crawler.ankiti.com having json
{
"data": [
{
"url": "http:\/\/crawler.ankiti.com\/?module=server&do=test",
"method": "post",
"options": {
"body": "post body test 1"
},
"target": "wo"
},
{
"url": "http:\/\/crawler.ankiti.com\/?module=server&do=test",
"method": "post",
"target": "wo",
"options": {
"body": "post body test 1",
"form_params": {
"name": "abhay",
"surname": "aggarwal"
},
"headers": {
"login": "abhay",
"password": "patparganj405"
}
}
},
{
"url": "http:\/\/crawler.ankiti.com\/?module=server&do=test",
"method": "post",
"target": "wo",
"options": {
"multipart": [
{
"name": "file text",
"path": "\/test\/file.txt"
},
{
"name": "file image",
"path": "\/test\/file.jpg"
}
]
}
}
],
"errors": "",
"meta": ""
}
I want to post a string in a body request which is present in json of this url.
You can use httpServlet and follow a procedure like this.
response.setContentType("application/json");
// Get the printwriter object from response to write the required json object to the output stream
PrintWriter out = response.getWriter();
// Assuming your json object is **jsonObject**, perform the following, it will return your json object
out.print(jsonObject);
out.flush();
Hope this helps
I need to send a HTTP Post to a REST API with the following complex type as parameters. I looked at the documentation of jersey and it helps only to send a key value pair. How can i send a HTTP Post request with the below parameters using jersey.
{
"key": "example key",
"message": {
"html": "<p>Example HTML content</p>",
"text": "Example text content",
"subject": "example subject",
"from_email": "message.from_email#example.com",
"from_name": "Example Name",
"to": [
{
"email": "recipient.email#example.com",
"name": "Recipient Name"
}
],
"headers": {
"Reply-To": "message.reply#example.com"
},
"important": false,
"track_opens": null,
"track_clicks": null,
"auto_text": null,
"auto_html": null,
"inline_css": null,
"url_strip_qs": null,
"preserve_recipients": null,
"view_content_link": null,
"bcc_address": "message.bcc_address#example.com",
"tracking_domain": null,
"signing_domain": null,
"return_path_domain": null,
"merge": true,
"global_merge_vars": [
{
"name": "merge1",
"content": "merge1 content"
}
],
"merge_vars": [
{
"rcpt": "recipient.email#example.com",
"vars": [
{
"name": "merge2",
"content": "merge2 content"
}
]
}
],
"tags": [
"password-resets"
],
"subaccount": "customer-123",
"google_analytics_domains": [
"example.com"
],
"google_analytics_campaign": "message.from_email#example.com",
"metadata": {
"website": "www.example.com"
},
"recipient_metadata": [
{
"rcpt": "recipient.email#example.com",
"values": {
"user_id": 123456
}
}
],
"attachments": [
{
"type": "text/plain",
"name": "myfile.txt",
"content": "ZXhhbXBsZSBmaWxl"
}
],
"images": [
{
"type": "image/png",
"name": "IMAGECID",
"content": "ZXhhbXBsZSBmaWxl"
}
]
},
"async": false,
"ip_pool": "Main Pool",
"send_at": "example send_at"
}
I looked at the other questions of sending HTTP Post using Jersey and all I could find was a way to only send a key\value pairs as parameters and not complex string types like above.
You should look at JAXB, it allows you to "automatically" build "complex parameters" out of "objects". Basically the procedure would be to define a class that represents the data structure you present as request message the REST API resource accepts, then populate it with the data your want to POST and send it. In this question you can find more details on how to exactly do that: Can jersey clients POST a JAXB object to the server using JSON?