I annotated my various controller methods with swagger Annotations but now I want to be able to retrieve the info for a given method programmatically, e.g. I want to retrieve the APIOperation value and notes from within Java. Is that possible without me checking through all the Swagger Annotations?
Related
I have a REST API defined in a swagger.yaml. Inside there are all the fields with their specifications (eg: length, pattern, etc.). Is there a way in java to validate these fields according to their specifications without having to do it manually?
In this way I should go and set the annotation manually above each
field, I was looking for a library that would automate everything by
reading the rules on the swagger
Yes, the OpenApi tool generator is able to do that.
https://openapi-generator.tech/docs/generators/jaxrs-spec
If the option "useBeanValidation" is enabled (default value), then the javax.validation annotations will be generated in your jaxrs bean.
If you are trying to validate request bodies in controller level, you should check javax validation constrains https://docs.oracle.com/javaee/7/api/javax/validation/constraints/package-summary.html
There are plenty of annotations such as #NotNull, #Pattern #Max etc. which will do the job
I have a POJO model that contains lots of fields. When we generate OpenApi Swagger docs for external users, we only want to expose a subset of the fields. Is there a way to do that? We don't want to change the POJO to add #ApiModelProperty for each field.
My team is evaluating adopting OpenAPI Generator for our project, and the ability to declaratively keep Spring MVC mappings in sync with the spec is attractive. However, much of Spring MVC's value comes from its ability to automatically resolve request information and provide it as strongly typed method parameters, and in the examples I've seen the generated Java interfaces only reflect the request attributes as directly listed in the spec. For example, userId is a string in a path mapping, and in my controller I want to inject that as a UUID or even a User (with DomainClassConverter). Additionally, I may want access to the HttpSession.
Is there any way (short of rewriting the templates) to customize the method parameters so that I can get the mappings autogenerated but add attributes for my own requirements?
I'm using Spring Data JPA and Spring Data Rest to expose my JPA entities as a webservice. My entities have several hundred attributes, and oneTo(very)Many relationships with other entities that have hundreds of attributes as well.
I'd like to be able for the user to pass in a query parameter that allows them to get back only the fields of the the JSON response body they are concerned with. In the past I've worked with IBM's Rational Team Concert REST API, and they allow the caller to pass in XPath query to select the fields they want returned. Their XPath integration also allowed the caller to apply filtering logic on the individual fields. For example, here is what was possible with that API:
workitem/workItem[creator/name='Bob Sacremento' or owner/name='Bob Sacremento']/(id|summary)
^The above query will return the id and the summary fields of all workItem elements whose owner or creator has a name equal to Bob Sacremento.
workitem/workItem/(id|summary|comments[creator/name='Bob Sacremento']/content)
^The above query will return the id, the summary and the comment contents fields of all workItem elements. But it will only show the comments that were created by Bob Sacremento.
I'd like to do something similar with my API. Before I wrote my own controllers to do so, I figured I'd ask if there is a framework that provides this functionality out of the box. It seems Spring Data REST does not.
You can use Spring Data REST to define known projections, this can be used to create endpoints which expose specific projections on your underlying models and/or to allow invokers to request known (pre defined) projections via the URI e.g. foo/bar/bas?projection=summary.
More detais in the Spring Data REST docs.
However, I'm not aware of a library which will integrate with Spring Data REST, JPA and allow callers to specify projections (in the form of a SQL-esque SELECT statement for example) and then apply those projections on the fly on your behalf.
I suspect you'll need to define a query parameter such as select=a,b,c in your controllers and then use the supplied value to apply a projection via Spring Data JPA.
GraphQl provides this functionality.
I've following problem, there's a regular spring model (let's call it "A") with some validations-related annotations. Next, there's a command object (regular POJO class that defines some field, one of them is object of type A). The command object implements Validator interface, to make binding and validation work in controller methods.
Question is, how to make use of annotations-configured validations inside the command object (given it implements Validator interface, hence it has supports() and validate() methods).
What I'm trying to achive is to have basic validations on model that is reused and mixed with some heavier business-logic validations in other parts of the system.
I have had the exact same problem. I wanted to use automatic annotation validation for "simple things" and then pass the complex validation logic to my custom spring Validator. But whenever I set the controller validator, all of hibernate's validation stopped working, as described at the end of this tutorial:
http://www.captaindebug.com/2011/07/applying-custom-spring-validator-to.html#.VQR0OI7F-gd
This technique should be used when you need to do ALL your
controller’s validation yourself, and you can’t or don’t want to make
use of the Hibernate’s reference implementation of a JSR 303
validator. From this, you’ll guess that you can’t mix your own custom
Spring validator with Hibernate’s JSR 303 validator. For example,
adding the built-in annotations to the Address command object will
have no effect:
You should forget about old style Spring Validator and delete "setInitBinder()" as described in this related question:
Spring MVC validator annotation + custom validation
You should then only rely on hibernate validation and JSR303.
To add a complex validation to your class (model), say you want to check two date fields - use a custom annotation constraint on class level as described in the link below.
https://docs.jboss.org/hibernate/validator/5.1/reference/en-US/html/validator-customconstraints.html#section-class-level-constraints
Hope this helps.
Best Regards,
Alexander
Once look at this may this help you
Using both JSR-303 and Traditional Bean Validation?. There i have given one example for custom validation for model using custom annotation.