Spring boot with SpringFox Swagger UI Generates all Verbs - spring-boot

I enabled API documentation using Swagger 2.6.1 in my spring boot app. The page(swagger-ui.html) loads fine but controller documentation contains all the verbs(PUT, GET, PATCH, POST, etc) even if my controller only has a GET operation. How can I disable the other verbs in the UI doc?

This happens when you have mapping like this in your controller
#RequestMapping(value = "/productDetails")
Springfox cannot identify what is the requestMethod hence it provides all mapping.(Eventhough the default is GET)
If you change this to
#RequestMapping(value = "/productDetails", method = RequestMethod.GET)
Then you will see only GET mapping and not others.
If you use newer versions of Sprinboot, you can use #GetMapping or #PostMapping instead of #RequestMapping

Related

#RequestHeader in spring not accepting fake headers

I am using
#RequestHeader(value = "channel") String channel
in Spring application controller to get headers and it works fine for normal headers, however, in some cases, I need to trap a request using filter and use HttpServletRequestWrapper to add some extra headers. The new headers added by overriding getHeader method are not being accepted by #RequestHeader annotation and throws error. However, if I manually get headers using
HttpServletRequest.getHeader("channel")
the new headers work fine. Is there any bug in #RequestHeader implementation? If so, is there any work-around so that I wouldn't have to change the same thing in 50+ APIs.
When resolving #RequestHeader, Spring is using getHeaders(), not getHeader(). You need to override this one.

How to send POST requests to a Spring Boot REST API using Postman form-data?

I'm trying a very simple Spring Boot + MySql app using STS: https://github.com/acenelio/person01
Posting a new Person using raw body works fine. However, when trying to post using form-data, I get either a 400 HttpMessageNotReadableException or 415 HttpMediaTypeNotSupportedException error, depending on which Content-type I set on Headers.
I provided some Postman printscreens here: https://sites.google.com/site/stackoverflownelioalves/arqs
What am I missing? I'm kinda new on Postman and Spring Boot. Thanks.
You can use the #RequestBody annotation:
#RequestMapping("/addInvestorProfile")
public #ResponseBody Boolean addInvestorProfile(#RequestBody() InvestorProfile profile
, #RequestParam(value = "sessionId") String sessionId)
{
.... your content
return true;
}
I am not sure which version of your spring boot. First of all, almost all spring mvc or spring boot controller method will accept all request content-type when you are not add consumer param to #RequestMapping annotation. So, maybe there is some wrong usage of postman.And I am work with spring boot & postman well.
try to compare postman behavior by copy request to curl
Usually post a request use curl looks like:
curl -XPOST UrlOfYourSpringBootEndPoint -d'body of your post'
more detail of curl here.

Spring MVC interceptor

I need inputs related to Spring MVC, I have a URL to which a client will send a post request with an xml as the pay load. I plan to have a controller method which maps to the requested url, I want that xml to be validated/converted to an object using jaxb before controller method is executed. And also, the controller method should have only the object as the parameter to its methods and no httprequest etc.
So, how do I achieve this? Will interceptor be helpful? If yes, how will it be done?
I plan to use Spring 3.
Simply use #RequestBody in conjunction with #Valid on a method argument and that is all you need.
public void myRequestHandlingMethod(#Valid #RequestBody YourJaxbObject jaxbObject) { … }
I strongly suggest you take a look at the Spring reference guide

Available paths listing for Spring actions

I have an application which exposes RESTful actions using spring annotations and Spring MVC.
It looks like
#RequestMapping(value = "/example/{someId}",
method = RequestMethod.GET, consumes=MediaType.APPLICATION_JSON_VALUE,
produces=MediaType.APPLICATION_JSON_VALUE)
#ResponseBody
public void isRegisteredToThread(#PathVariable long someId, HttpServletResponse response) {
[something]
}
What I want is an automatically generated listing of all URL's, methods and available parameters - possibly within a WSDL. Is there a plugin or is it somehwere available?
WSDL is not done for rest, it's used for SOAP.
You might use WADL, but I really do not suggest it.
In my project I always use swagger (there is a release for spring). You may find more info here https://github.com/martypitt/swagger-springmvc and here http://blog.zenika.com/index.php?post/2013/07/11/Documenting-a-REST-API-with-Swagger-and-Spring-MVC
Give it a try.
As an alternative, if you don't need something web-based, you may try rest-shell (https://github.com/spring-projects/rest-shell)
You could take a look at the source code of RequestMappingEndpoint provided by the Spring Boot and see how Spring Boot reports the mappings.
Looking through that code one can see that the mappings (both handler and method mappings) can easily be obtained from the applicationContext
(using
applicationContext.getBeansOfType(AbstractUrlHandlerMapping.class)
and
applicationContext.getBeansOfType(AbstractHandlerMethodMapping.class)
respectively). After you have obtained the mapping you can process them anyway you like.
You might want to create a library that could include in all your projects that processes the mapping your organizations desired form

HTTP Status 405 - Request method 'POST' not supported when I upgrade from Spring 3.0.5.RELEASE to Spring 3.1.0.M1

I encounter the error:
HTTP Status 405 - Request method 'POST' not supported
when I upgrade from Spring 3.0.5.RELEASE to Spring 3.1.0.M1. There is no other change in my code, what can be the reason? The code is something like:
#RequestMapping(method = RequestMethod.POST)
public String create(#Valid ConcreteUser concreteUser, BindingResult bindingResult, Model uiModel, HttpServletRequest request)
When I switch back to Spring 3.0.5.RELEASE, it is working again. However I still desire to upgrade my Spring library to 3.1.0.M1 since I may want to use its new Cacheable feature etc. How to achieve the upgrade without any problem?
Sounds like a spring bug (if you use google you will find some problems like that with 3.1 pre release version).
But first of all, use the actual 3.1 version. At the moment it is 3.1.RC2 (http://www.springsource.org/node/3317) -- Hopefully the bug is gone.

Resources