Is it possible to use Spring MVC with Jersey annotations? - java

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

Related

Adding response body to Spring Boot Actuator

I'd like to view the response body in Spring Boot Actuator. I have tried couple of tutorials I found but none is working. The most concrete answer I found was using WebRequestTraceFilter which is no longer supported in Spring Boot 2.X.
Alternatively, if there's any tool like Laravel Telescope for Spring Boot would be great. I'm already using Spring Boot Admin and it's great except for the response body part.
I suppose you are talking about /actuator/httptrace. Spring boot doesn't publish request and response payload by default. Also you need to provide implementation of "InMemoryHttpTraceRepository" bean to expose custom information for tracing. This blog could be helpful.

Parse JsonSchema into io.swagger.v3.oas.models.media.Schema

I have spring boot app that provides openapi. I've used Swagger. I have a file that stores jsonSchema and I wanna to parse it into io.swagger.v3.oas.models.media.Schema to add it into Swagger. I can't find any existing solutions. Does any spring boot library supports this functionality?
Thanks.
If you are using Spring web MVC HATEOS endpoints, then you can directly use the https://springdoc.org/. Example: #RequestMapping, #GetMapping, etc.
Springdoc-openapi comes with ui and open api v3 docs generation.
IF not, and you prefer to use JaxRS2 to configure your RESTful endpoints, then use the <groupId>io.swagger.core.v3</groupId><artifactId>swagger-jaxrs2</artifactId>, and register your resources using JerseyConfig extends ResourceConfig and include io.swagger.v3.integration.sources for jaxrs2 scanning.

What is the equivalent part of Jersey ClientRequestContext and ClientResponseContext in Spring MVC?

I want to migrate my application from Jersey to Spring MVC, What is the equivalent part of Jersey ClientRequestContext and ClientResponseContext in Spring MVC? It is Jersey client side filter, does anyone know about it?
There is no one to one mapping for these classes in Spring. You can achieve this by implementing HandlerInterceptor.
Check if this answer on stackoverflow gets you going.

#ModelAttribute for Jersey similar to Spring

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.

whats the jersey analog for spring (boot) data's #Repository

ive been tasked with a total refactor of legacy code. It's a simple webservice, just an http request, then business logic with possibly a few database calls and a few other microservice calls, then a json response. I am being pushed not to use spring boot because no one else around me has used it before, and I was told jersey does everything spring boot does. I've never used jersey so im trying to find out how to do things that spring boot makes simple (ie repository layer with spring-data, caching, spring-consul, spring-zuul, spring-actuator, spring-circuit-breaker) It looks like jersey does do an analog of spring-security, bean validation, and easy insertion of servlet filters, but not everything spring-boot does. Is there an easy way to wire in a JPA type repository in jersey? I cant find it in the docs at https://jersey.java.net/documentation/latest/index.html.
I think about it this way. There are different layers in your application. You have a service layer, and you have a "REST layer". You access the Spring repositories with the service layer. Then you have the REST layer. With Spring, you have Spring MVC which is its web layer implementation, that you can also use as REST services. There is also Jersey, which is completely independent of Spring, which is a another REST layer options.
That be said, when using Spring MVC as the REST layer, adding the service layer with Spring data is seamless. But Jersey also has integration with Spring, that allows us to use Spring at the service layer inside our Jersey REST services. You check out this post which has some links to example of how this can be done (no hacking, this is supported out of the box). Using this approach, you can just injector your Spring data repositories into your Jersey resource class
interface PetsRepository extends JpaRepository<Pets, Long> {}
#Path("/pets")
class PetsResource {
#Autowired
private PetsRepository repo;
}
Now lets talk about Spring Boot. Spring Boot is just a bootstrapping framework. What it does is allow you to easily bootstrap an application without all the boilerplate configuration you would need without it. When your using Spring Boot for your REST services, you're not actually using Spring Boot itself as the REST service engine. You are only using it to bootstrap Spring MVC and maybe your Spring Data. But Spring MVC is the actual REST service engine.
Now like I said before, Jersey has support for integrating Spring into into it (for the service layer). Because of this support, Spring Boot has also provided a bootstrap configuration to integrate this support seamlessly. So instead of using the manual configuration that you would see in one of the examples linked to above, Spring Boot handles this configuration for us. So we can use Jersey as the REST layer, and Spring beans as the service layer. Check out the links below
See also:
Spring Boot docs for Jersey support

Categories

Resources