#ModelAttribute for Jersey similar to Spring - java

I wonder whether jersey has something like Spring MVC can offer to pass several query parameters inside a one. When we need to group several params in Spring MVC, we can use #ModelAttribute and have these query params in URL listed as ?name=SomeName&city=SomeCity ... Then we just create a POJO that contains these fields. Does jersey have similar functionality? Using a json is unacceptable for my case cause I'm modifying an old API.
Thanks!

You could do that with #BeanParam starting with JAX-RS 2.0. See a sample here.

Related

How to self describe a REST api with Spring?

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

Is it possible to use Spring MVC with Jersey annotations?

I am wondering, if it's possible to use Spring Web MVC with Jersey annotations, as I'm having a massive problem with Spring not being able to parse part variables the way Jersey can and I need to migrate some Jersey code to Spring MVC.
I have the following code in Jersey:
#PUT
#Path("{storageId}/{repositoryId}/{path:.*}")
#PreAuthorize("hasAuthority('ARTIFACTS_DEPLOY')")
public Response upload(#PathParam("storageId") String storageId,
#PathParam("repositoryId") String repositoryId,
#PathParam("path") String path,
InputStream is)
{
...
}
I would like to convert this over to Spring MVC, but, like I've explained in this SO post, I'm experiencing issues with parsing the path variables.
Hence, my wondering (and a separate question): is it possible to use the same Jersey annotations with Spring MVC?
it's possible to use Spring Web MVC with Jersey annotations, try make a little sample in Spring Initializer there you can check the option jersey, see how spring build this project and try in yours

When to use RequestContextFilter with Jersey?

In a bunch of the tutorials and code samples of Spring Boot and Jersey that I've seen, the following line appears:
register(RequestContextFilter.class)
What is this really used for? I don't see anything unusual in those samples, and if I remove it from my (simple) application, nothing seems to break.
RequestContextFilter's javadoc says
This filter is mainly for use with third-party servlets, e.g. the JSF
FacesServlet. Within Spring's own web support, DispatcherServlet's
processing is perfectly sufficient.
I haven't seen third party servlets in those examples.
In one of them I read
org.glassfish.jersey.server.spring.scope.RequestContextFilter, which
is a Spring filter that provides a bridge between JAX-RS and Spring
request attributes
What would be an example of a Spring request attribute?
What is some typical use case, besides needing a third party servlet?

HttpClient vs Spring Rest Template?

What's the best way to make a REST call?
Should I use Apache Http Client or Should I use Spring Rest Template.
On what basis I can decide which one I should go for?
I need to make a call to this url-
http://localhost:8080/service/Service/v1/get/USERID=10000/profile.ACCOUNT.SERVICE"
And after getting the response back, I just need to see whether that response contains any particular string or not.
Spring RestTemplate follows the pattern for all the *Template classes within the core Spring framework and the various sub-frameworks: JdbcTemplate, HibernateTemplate, WebServiceTemplate etc etc.
The idea of all of these Template classes is to reduce the boilerplate code (exception handling, repetitive stuff and concentrate on your business logic). I would definitely use it over the simple HttpClient.
To get the class you'll need the spring-web dependency.

springframework controller from standalone java code

We have Spring MVC application. One module requires to call the Spring Controller from standalone java app.
Can I do that?
Dead easy:
new java.net.URL("http://localhost:8080/path/to/your/controller").openStream();
Just like you would do it in the browser. If you want to call the Java code directly, do not publish your controllers. Instead, extract business logic and provide it as a library.
Yes.
It's a POJO, especially if you use Spring 3.x. The newest versions don't even extend an interface or base class.
I'd call it through its http interface as it's a Spring controller. You could use a clientside http request and use the response. I'm guessing the method you wish to call does not resolve to a view, if that's the case then just use something like the RestTemplate class that comes with Spring 3.
Not sure if it would be a good idea to call it directly as Spring MVC projects are usually hidden away inside servlet wars.

Categories

Resources