Swagger UI blank PDF download - java

I'm having a problem with Swagger UI when trying to download a PDF file. Everything works fine outside Swagger UI (using curl or Postman there is no problem), but when I try to download via Swagger UI I get a blank PDF.
I'm using Springfox 2.50 (microservice JHipster application), and the response from my Java Spring method is a HttpEntity<byte[]>.
Edit:
I found similar problem: Swagger UI Download PDF but it does not have any answers.

You likely have one of the following issues with your setup:
1) The #produces on your server (and therefore in the swagger definition) may not be correct. Please make sure you have produces: application/pdf in your operation.
2) Your operation that returns the pdf may have no schema associated with it. For swagger-ui to render a proper download, you need to have a schema. The correct schema would be:
schema:
type: string
format: byte
3) Your server must be returning the correct Content-Type. Please make sure it's application/pdf in the headers
You might want to try the petstore sample against your server as that is the latest build of swagger-ui, the one bundled with SpringFox may be a bit behind.

Related

Generate an Output in an Endpoint with a JSON body as a input

My company has asked me to do a small project in Java Spring and I have never programmed in Spring nor Java before, so I am a bit lost.
I have been asked to make a series of endpoints, seeing the documentation does not seem difficult, where I find the difficulty is that I always receive as input a JSON with several key-value pairs, and with that, I have to search in an Oracle database using Hibernate and return another JSON with other key-value pairs.
Example:
Input:
{
"client_id": 123,
"shop_id": 22,
"id_contract": 233
}
Output:
{ "loan": "70%"
"percentage_point": "80%"
}
My question is, should I do it through a POST method? Is the output a status 200? How do I get from JSON, for example, the client id to map it in the database with hibernate / Spring?
Any advice will be welcome, even if it is simply articles, etc.
Well what you have been asking is not for a specific problem.It is an entire requirement. I can try helping with the steps that you may need to follow.
If you are not done creating a project, I would suggest you to use Spring Boot for your project.To create a spring boot app click here
And if you are using maven or gradle select it and then search for Spring Web,Spring Data JPA in search dependencies and generate a project.
Once you have a project ready and imported it into an IDE (for example Eclipse, IntelliJ Idea, Netbeans or any other Java IDE), and finally all you have to do is to write REST services to implement your requirements.
And to answer your questions.
should I do it through a POST method?
If you were told that the request you will be receiving is in a JSON format, then you need to use POST method.
What is post and why to use post method.check here
Is the output a status 200?
A status 200 in http means SUCCESS. It means when the data sent by the client is valid and the server has processed the request(in your case JSON body) successfully it returns 200.So incase of success your api will return 200.
More on http status codes
How do I get from JSON, for example, the client id to map it in the database with hibernate / Spring?
To understand more on how to create a rest controller, take JSON input, create a POST method, use Spring JPA for making database operations please follow the links below:
create a rest service
spring boot crud Database operations example from scratch
Spring boot Data JPA example
Hope this will help you to put you in right path.
Without code or a more specific post about requirements it's impossible to help. All I can do is provide links to potentially helpful articles. You need to look into the following;
Spring Boot RESTful service
Spring Boot Controller vs RestController
Spring Boot JPA
Spring Boot Consuming/Producing JSON

How to validate server responses against swagger generated java models

I use POJO models, generated from swagger.yaml file in my project (automated API tests). However, I don't use any other stuff that swagger-codegen-maven-plugin generates.
When I fire a request, I just put the server response into the generated POJO and later i can get any data from this and compare to anything I expect to be there.
But the problem is, test are not failing, when, for example, some required field descripbed in swagger is absent in the server response. They fail if only I manually try to get the value of this field from the object.
How to make them fail when something is missing, or when some field has wrong type, or even when something not described in swgger is returned by the server?

Transforming CXF SOAP web-service into a custom HTTP / JSON web-service

I have created a SOAP web-service, running in a tomcat container, using CXF and a WSDL contract approach. The Java code was generated from the WSDL using the maven plugin "cxf-codegen-plugin".
Now, I would like to also expose the web-service as a custom HTTP / JSON web-service by mapping the XML data into JSON data. But I haven't been successful yet and I would appreciate any help to achieve that.
I found that old documentation https://cxf.apache.org/docs/json-support.html on CXF's website (which has also been recently duplicated and not corrected here https://cwiki.apache.org/confluence/display/CXF20DOC/JSON+Support). I got it running with a small fix : sf.setBindingId(HTTPBinding.HTTP_BINDING); instead of sf.setBindingId(HttpBindingFactory.HTTP_BINDING_ID); since HttpBindingFactory does not exist anymore in CXF 3.x versions.
The first issue with that solution is that it depends on jettison-json whose last release was published in July 2016. The project looks dead. I could not find any suitable replacement. I only found StAXON which is worst : last release in February 2014.
Is there any other library that is well maintained and that implements StAX ?
The second issue is that the example uses a server bean while I already have a container. For the SOAP service, I simply use the public static Endpoint publish(String address, Object implementor) function.
How can I use both the Tomcat container and the JSON/XML conversion ?
The third issue is that I could not send a valid JSON request so far. The absence of documentation does not help. Still, I managed to get as far as sending a valid JSON equivalent of the SOAP header content. But I could not manage to guess how to make CXF also reads the equivalent of the SOAP body. The Java interface of the web-service is public Response get(Header header, Body body); and I keep getting errors : org.apache.cxf.interceptor.Fault: wrong number of arguments while invoking public abstract Response ServicePort.get(Header,Body) with params [Header#88f6973[user=bbcc2545-27ea-43cd-a1fe-c4a194258e0f]]. Obviously the body is missing.
Is there anyone who knows the right JSON syntax to send a request to a CXF HTTP / JSON web-service that relies on jettison-json ? (Or where could one find a comprehensive documentation ?)

How to save fetched html content to database in apache nutch?

I'm using 1.8 version of apache nutch. I want to save crawled HTML content to postgre database to do this, I modify FetcherThread.java class as below.
case ProtocolStatus.SUCCESS: // got a page
pstatus = output(fit.url, fit.datum, content, status,
CrawlDatum.STATUS_FETCH_SUCCESS, fit.outlinkDepth);
updateStatus(content.getContent().length);
/*Added My code Here*/
But I want to use plug-in system instead of directly modifying FetcherThread class. To use plug-in system which extension points I need to use?
You could write a custom plugin and implement an extension of org.apache.nutch.indexer.IndexWriter to send the documents to
Postgres as part of the indexing step. You'll need to index the raw content which requires NUTCH-2032 - this is in Nutch 1.11 so you will need to upgrade your version of Nutch.
Alternatively you could write a custom MapReduce job which would take a segments as input, read the content and send it to your DB in the reduce step.

debugging in struts application

i'm new to struts... I have a struts form. i need to analyze the data posted to an action class. i just want to output the data to a page and stop further execution so as to test of it is working correctly... how is it done..?
There are many ways to debug a struts application:
Configuration Plugin: You can use configuration plugin provided by struts. To use this plugin include struts2-config-browser-plugin-x.x.x.x.jar jar in your classpath and then you can use
">Launch the configuration browser
anywhere in your application to view a lot of detail about the action class.
The second way is to use Debugging Interceptor by using property debug=true in struts.xml.
See the below link for more details:
http://struts.apache.org/release/2.2.x/docs/debugging-struts.html
The easiest way to quickly and thoroughly inspect http requests and responses and post data is to use either:
http-fox with firefox (download)
chromes' network inspector (pre-installed with chrome - ctrl-shift-i - Network - Click on the appropriate post request and the body of the post will be displayed)
If neither of these very easy tools appeal, you'll have to find the appropriate hook to attach your debugger. I've not used struts since 2002 so cann't advise.
The best method is to use System.out.print and get the result in the tomcat console... i had said i need on server side not client side.

Categories