I'm using spring-mvc to create servlets like:
#RestController
public class MyServlet {
#GetMapping("/test")
public MyRsp test(MyReq req) {
//...
}
}
Now if the user accesses the root of my app localhost:8080/my-app, it should show a list of GET and POST methods available. At best with possible input parameters, acceptable headers etc.
Question: is that possible with any spring framework, like HATEOAS?
I'd expect a framework to auto detect any #RestController and included methods.
Or would I have to create that overview page myself?
You should must look into this
To integrate it in spring you can refer this
Swagger is one of the best framework to expose RESTful API's.
Swagger 2 is an another option. read the following to know more about swagger and how to set it up.
Setting Up Swagger 2 with a Spring REST API
You can also create swagger definition for your rest apis, which can be used by the clients to generate client classes.
Also the swagger ui can be used to test/invoke your APIs. swagger provides a user interface where you can input all the api inputs such as query params, path params, request body, headers.
Sample Swagger UI
You can check this project Spring Restdocs (github), which allows you to generate ready to use REST documentation. It's officially maintained by Spring Team:
The primary goal of this project is to make it easy to document
RESTful services by combining content that's been hand-written using
Asciidoctor with auto-generated examples produced with the Spring MVC
Test framework. The result is intended to be an easy-to-read user
guide, akin to GitHub's API documentation for example, rather than the
fully automated, dense API documentation produced by tools like
Swagger.
The other option is to use Swagger, it supports bottom-up approach as well:
A bottom-up approach where you have an existing REST API for which you
want to create a Swagger definition. Either you create the definition
manually (using the same Swagger Editor mentioned above), or if you
are using one of the supported frameworks (JAX-RS, node.js, etc), you
can get the Swagger definition generated automatically for you.
Some examples of swagger are mentioned here: 1 2
Related
I have been working with open api ui 3.0.0 and when I give annotations on API endpoints the APIs are shown on swagger endpoint. But interestingly there is a list of schemas that shows DTOs used in the endpoints. I need to remove those endpoints. So I tried setting content = #Content(schema = #Schema(hidden = true) on #ApiResponse.
It seems that the models used in endpoints annotated with these annotations are not shown except for endpoints where a DTO is used in request structure that is shown no matter what and the problem with others is that the response content seems to be shown empty with just response code and descritpion.
Is there any proper way of doing this in spring boot. Most of the standard API documentations doesn't shows this schemas and some of the DTOs are even something that I don't want to show the consumers of swagger documentation.
Try use to one in the Docket, it works!
Dockek()...
.ignoredParameterTypes(ModleName.class)
I am working on a project which compromises JAX-RS API. They have plenty of web services defined and i need to profile it through J Meter. Can we have any tool or Rest URL which provides all the services and its definitions
There is a chance that your application developers have implemented Swagger documentation via i.e. Swagger-Core JAX-RS, if this is the case you should have a special documentation endpoint listing all URLs, methods, examples, etc.
If this is the case you can even use Taurus automation framework which comes with Swagger to YAML converter so you can go use this for building your test plan like:
Swagger -> Taurus YAML -> JMeter JMX
I need produce Rest Api with spring HAL hateoas support and pagination.
What I see is if I use #Restcontroller I need manually write code for _links and paging logic.
If I use RepositoryRestResource we can get generated links and pagination but I don't have control on _link generation and API path, I don't know I can customize or not.
So how can we use #restcontroller and RepositoryRestResource together so that I can use pagination of repository rest resource and API path of rest controller
You can keep using #RepositoryRestResource on your repository, follow the points to achieve what you have mentioned,
Extend your repository with org.springframework.data.repository.PagingAndSortingRepository which is going to provide pagination stuff, you just need to pass page and size in request parameters to get that work.
Customization of API path can be done using #RestResource(path = "your_path_to_api") on top of the method defined in your repository.
The standard way for creating REST resources both in JAX-RS and Spring MVC is to use annotations. Jersey provides an alternative to using annotations, which is called Programmatic API for Building Resources. This can be used to create REST resources dynamically at runtime, for example from user input or configuration. With the annotations approach, in contrast, creating a new REST resource requires the usual develop/build/deploy cycle.
Is there an equivalent API for building REST resources programmatically in Spring MVC? I'm looking for working example code like in the Programmatic Hello World example of the Jersey user guide.
Question is, what's the difference between Spring HATEOAS versus Spring Data Rest ?
I feel both can do the same, and Spring Data Rest (as part of Spring Data) seems a bit more alive.
https://github.com/spring-projects/spring-hateoas
https://github.com/spring-projects/spring-data-rest
When would you use one or the other?
Spring HATEOAS provides common abstractions (representational models, a Link class, API to build links pointing to Spring MVC controllers, etc.) to ease building hypermedia driven REST APIs with Spring MVC in general. Thus, you can use it alongside Spring MVC to manually build those services.
Spring Data REST uses Spring HATEOAS to automatically expose resources for entities managed by Spring Data repositories and leverages hypermedia aspects to do pagination, link entities etc. So it covers the 80% use case for the basic stuff and allows you to selectively add more complex processes using manually implemented controllers later on.
To get a feel for this, feel free to have a look at the Spring RESTBucks sample project. The handling of Order instances is completely done by Spring Data REST (with some minor tweaks to implement business constraints). The entire payment logic is then implemented manually as the process does not fall into the CRUD category as we actually need to implement certain steps and a protocol to complete the order. Again, the code is here, a slide deck with some additional visuals can be found at speakerdeck.com.
HATEOAS stand for Hypermedia as the Engine of Application State and is one of the key ponit of REST. Basically the key point consist to use links on the your resource representation for map the valid transition of the application state. In this case will be the service provider that provide the valid next correct state of the your application reachable through the link. Spring HATEOAS is the Spring projects for help to build the Hymeridia Controls in your Resource. It is a project integrate with Spring MVC and you can think as the Spring MVC extension for building a real RESTFull WS whit a very good support for increase the level of the your service form CRUD (level 2 of maturity in the Richardson model) to an Hypermedia aware (level 3 of maturity in the Richardson model). Spring Data Rest on the other hands is a very nice project that use Spring HATEOAS as basic brick, for give you a repository layer usable as restfull ws. In proctis the project help to reduce the classical boliporlent code for expose the your repository layer as a restfull endpoint. We can say tat was the propouse of the projects very different. With Spring HATEOAS you had a framework usable for any kind of restfull endpoint, with spring data rest you had a spring project that already provide an endpoint and a framework for customize it.
I hope that this reflections can help you to clarify the difference between the two projects and understand better how use one or the other
I chose to use both of them in my project. One layer of controllers was built with Spring Data REST. The other layer of controllers was #RestController's (spring-wevmvc). In this layer I used Spring HATEOAS to create customed page's.
(Process was :
1.creating Pageable Pageable pageable = new PageRequest
2. creating new Page Page<FooDt> page = new PageImpl<FooDt>
3.creating PagedResources PagedResources<Resource<FooDt>> resource = fooAssembler.toResource(page, fooAssembler) after that process using Jackson's ObjectMapper to return json.
The solution that I found for loading to context both technologies - is using two DispatcherServlet's.
Otherwise, Spring Data Rest is taking control and there is no option to use other controllers. ( In that way I had two domains in my app. One for Data Rest and one for webmvc+HATEOS).