GET /swagger-ui.html fails with HTTP 206 - spring-boot

I'm using spring-boot 1.5.0 RELEASE with swagger 2.6.1. I've added #EnableSwagger2 annotation on my spring boot application and defined a #Bean of type Docket. I've not used #EnableWebMvc annotation anywhere. I've excluded swagger-ui.html from security. When i call /v2/api-docs, i get the correct response JSON. But when i call /swagger-ui.html, it fails with HTTP 206. In chrome, it fails with Failed - No file. In console it shows the following error - Resource interpreted as Document but transferred with MIME type multipart/byteranges: "http://localhost:8080/swagger-ui.html".

Related

Springdoc update from Springfox result in 406 error

I am migrating springfox to spring doc and get 406 error.
Similar like this: https://github.com/springdoc/springdoc-openapi/issues/950
Only the swagger-ui got the issue but the /v3/api-docs can generate the json/yaml file.
After I debug the DispatcherServlet in doDispatch, an exception occur during getHandler.
The exception is:
org.springframework.web.HttpMediaTypeNotAcceptableException: Could not find acceptable representation.
Below is the picture of the debug. If I debug further, the request /swagger-ui/index.html is actually mapped to an endpoint that accept /{param1}/{param2} that generates application/json. So, how to map the swagger path to the resource instead of a controller? Thank you
Spring boot version 1.5.x (spring 4.x). springdoc version 1.6.14.

OAuth/authorization end point is not returning with expected http 302 status, also session id and token information is missing in the response

I am upgrading my jhipster application to the latest jhipster version which is 7.6.0.
Jhipster 7.6.0 introduces latest version of spring boot (2.6.3) and spring framework (5.3.15)
For security configuration class, I am still using the annotations from spring web mvc and I am not using spring web reactive. At this point, I dont want to switch to web reactive.
#EnableWebSecurity
#EnableGlobalMethodSecurity(prePostEnabled = true, securedEnabled = true)
#Import(SecurityProblemSupport.class)
public class SecurityConfiguration extends WebSecurityConfigurerAdapter { }
After compiling, building and starting my application, If I hit the login url, then I get below error on the screen
Login with OAuth 2.0
[invalid_user_info_response] An error occurred while attempting to retrieve the UserInfo Resource: 403 Forbidden: [no body]
Browser's DevTools console shows that OAuth/authorization end point is returning with 200 http status with no values for session id and token, whereas ideally the end point should return with http status 302 along with session id and token.
I suspect, its related to the changes introduced by the version upgrade, because If I switch back to the older branch of the source code (having older version of jhipster like 6.4.1 and spring) , then the login works just fine.
Can anyone suggest what could be the possible reason behind this issue?
I believe that the 'authorize' end point returning 200 response with missing session id and token is surely one of the reasons.
Please guide me on this.
Thanks.
Thanks for the details. I have found the root cause. For the latest version of spring security, oidc: scope needs to be mentioned explicitly. something like this - scope: openid,profile,email. Adding this scope in application yml fixed the issue. –

How to consume Soap Rpc encoded in spring boot?

Hello I managed to generate classes from wsdl using axis 1.4.But I am stuck at invoking services. I tried to used WebServiceTemplate and jaxb2marshaller but there is no request class generated. So I tried created request class manually based on the generated response class. But it is getting any response from server.It says xmlrootelement is not found. So i think jaxb2marshaller is not the correct way to invoke service since it is old rpc encoded soap wsdl. Suggest me the idea to invoke service.
Update:
when using axis 1.4
I found the solution to invoke methods.It is simple 2 line code.
SomeService service=new SomeServiceLocator().getSomeServicePort();
Sample sample=service.fetchSample();

Grails JAX-RS REST multipart file upload

Trying to get a multipart file upload to work in a Grails 2.5.4 resource.
I'd really like to use Spring's out of the box multipart features, but I cannot find an example for my particular case.
I am calling the API from Postman. Body with form-data and a single PDF file attached in a file field. No other headers!
First try
#POST
#Path("/pdf")
Response createPdf(MultipartFile file)
Gives back 415 Unsuported Media Type in Postman, and this on the server:
[...] ERROR container.ContainerRequest - A message body reader for Java class org.springframework.web.multipart.MultipartHttpServletRequest [...]
The registered message body readers compatible with the MIME media type are:
*/* ->
com.sun.jersey.core.impl.provider.entity.FormProvider
com.sun.jersey.core.impl.provider.entity.StringProvider
com.sun.jersey.core.impl.provider.entity.ByteArrayProvider
com.sun.jersey.core.impl.provider.entity.FileProvider
com.sun.jersey.core.impl.provider.entity.InputStreamProvider
com.sun.jersey.core.impl.provider.entity.DataSourceProvider
com.sun.jersey.core.impl.provider.entity.XMLJAXBElementProvider$General
com.sun.jersey.core.impl.provider.entity.ReaderProvider
com.sun.jersey.core.impl.provider.entity.DocumentProvider
com.sun.jersey.core.impl.provider.entity.SourceProvider$StreamSourceReader
com.sun.jersey.core.impl.provider.entity.SourceProvider$SAXSourceReader
com.sun.jersey.core.impl.provider.entity.SourceProvider$DOMSourceReader
com.sun.jersey.json.impl.provider.entity.JSONJAXBElementProvider$General
com.sun.jersey.core.impl.provider.entity.XMLRootElementProvider$General
com.sun.jersey.core.impl.provider.entity.XMLListElementProvider$General
com.sun.jersey.core.impl.provider.entity.XMLRootObjectProvider$General
com.sun.jersey.core.impl.provider.entity.EntityHolderReader
com.sun.jersey.json.impl.provider.entity.JSONRootElementProvider$General
com.sun.jersey.json.impl.provider.entity.JSONListElementProvider$General
Adding #Consumes(MediaType.MULTIPART_FORM_DATA) does not help.
I've tried other params such as MultipartHttpServletRequest but there's still no message body reader.
Second try
#POST
#Path("/pdf")
#Consumes(MediaType.MULTIPART_FORM_DATA)
Response createPdf(#FormDataParam("file") InputStream uploadedInputStream,
#FormDataParam("file") FormDataContentDisposition fileDetail)
Gives back a 500 error, and this on the server:
SEVERE: Missing dependency for method public javax.ws.rs.core.Response com.carfax.VehicleReceiptPhotoResource.createPdf(java.io.InputStream,org.glassfish.jersey.media.multipart.FormDataContentDisposition) at parameter at index 0
SEVERE: Missing dependency for method public javax.ws.rs.core.Response createPdf(java.io.InputStream,org.glassfish.jersey.media.multipart.FormDataContentDisposition) at parameter at index 1
SEVERE: Method, public javax.ws.rs.core.Response createPdf(java.io.InputStream,org.glassfish.jersey.media.multipart.FormDataContentDisposition), annotated with POST of resource, class com.carfax.VehicleReceiptPhotoResource, is not recognized as valid resource method.
having these dependencies
compile group: 'com.sun.jersey', name: 'jersey-bundle', version: '1.19.4'
compile group: 'com.sun.jersey', name: 'jersey-json', version: '1.19.4'
compile "javax.ws.rs:javax.ws.rs-api:2.1#jar"
compile ('org.glassfish.jersey.media:jersey-media-multipart:2.27') {
exclude group:'javax.ws.rs'
}
compile group: 'org.jvnet.mimepull', name: 'mimepull', version: '1.9.11'
Third try
#POST
#Path("/pdf")
Response createPdf(#FormDataParam("file") InputStream fileInputStream)
With the same jersey dependencies as in the second attempt, this works, but it's not good enough for me. I'd like to upload multiple files at once, and get some extra json info on the side.
What I want
Some clarification on how many methods of multi file upload are there in Grails
What are the different libraries achieving this
To do this in a Grails REST resource, not in a controller
Some links I went through
Jersey Exception : SEVERE: A message body reader for Java class
https://groups.google.com/forum/#!topic/dropwizard-user/moYQOtxXnRU
Maven jersey-multipart missing dependency for javax.ws.rs.core.Response
https://philsturgeon.uk/api/2016/01/04/http-rest-api-file-uploads/
https://www.javatpoint.com/jax-rs-file-upload-example
JAX-RS Multipart with com.sun.jersey
*as per Paul Samsotha's comment
You're trying to mix Jersey 2.x multipart support with a Jersey 1.x application. This isn't going to work. You need to use the 1.x multipart. Get rid of the 2.x one.

Force Spring to always report exceptions as JSON

How do I force Spring to always convert uncaught exception to JSON instead of HTML page?
This is what I get when request is made from Chrome's REST client plugin:
{
timestamp: 1425041457798
status: 404
error: "Not Found"
exception: "com.some.my.Exception"
message: "/rrr does not exist"
path: "/test/rrr"
}
But this is what I get when I access it from browser or from Jersey API (you see parsed HTML):
Whitelabel Error PageThis application has no explicit mapping for /error, so you are seeing this as a fallback.Fri Feb 27 13:37:27 CET 2015There was an unexpected error (type=Not Found, status=404).No message available
I know this can be done somehow by setting the request headers but I want JSON response to be the only variant.
First we need add some exception resolver for exception that trows inside controllers. I prefer extend ResponseEntityExceptionHandler and add own method, but there is a great article on spring.io: http://spring.io/blog/2013/11/01/exception-handling-in-spring-mvc
After that if you still get following page you can override org.springframework.boot.autoconfigure.web.ErrorController. There are an example: BasicErrorController. But this works only if you use spring boot application with embedded container. For example, if you will create war file form spring boot project and will deploy under tomcat, you will get standard tomcat error page.
So this mean that ErrorController is not common solution.
In my case i throws exceptions inside filters, that why /error page shown. So solution will be write own filter that converts exceptions to JSON representation. This solution should work for every container/server, and you can get more information about exception.

Resources