Java jersey spring mvc

How to integrate Jersey in a Spring MVC application

I have recently started to build a public REST API with Java for Podcastpedia.org and for the JAX-RS implementation I have chosen Jersey, as I find it “natural” and powerful – you can find out more about it by following the Tutorial – REST API design and implementation in Java with Jersey and Spring. Because Podcastpedia.org is a web application powered by Spring MVC, I wanted to integrate both frameworks in podcastpedia-web, to take advantage of the backend service functionality already present in the project. Anyway this short post will present the steps I had to take to make the integration between the two frameworks work.

Framework versions

Project dependencies

The Jersey Spring extension must be present in your project’s classpath. If you are using Maven add it to the pom.xml file of your project:

  org.glassfish.jersey.ext jersey-spring3 $  org.springframework spring-core  org.springframework spring-web  org.springframework spring-beans    org.glassfish.jersey.media jersey-media-json-jackson $  com.fasterxml.jackson.jaxrs jackson-jaxrs-base  com.fasterxml.jackson.core jackson-annotations  com.fasterxml.jackson.jaxrs jackson-jaxrs-json-provider   

Note: I have explicitly excluded the Spring core and the Jackson implementation libraries as they have been already imported in the project with preferred versions.

Web.xml configuration

In the web.xml , in addition to the Spring MVC servlet configuration I added the jersey-servlet configuration, that will map all requests starting with /api/ :

 Spring MVC Dispatcher Servlet org.springframework.web.servlet.DispatcherServlet contextConfigLocation classpath:spring/application-context.xml  1  Spring MVC Dispatcher Servlet /  jersey-serlvet org.glassfish.jersey.servlet.ServletContainer javax.ws.rs.Application org.podcastpedia.web.api.JaxRsApplication  2  jersey-serlvet /api/*  

Well, that’s pretty much it… If you have any questions drop me a line or comment in the discussion below.

In the coming post I will present some of the results of this integration, by showing how to call one method of the REST public API with jQuery, to dynamically load recent episodes of a podcast, so stay tuned.

Podcastpedia image

Adrian Matei

Creator of Podcastpedia.org and Codepedia.org, computer science engineer, husband, father, curious and passionate about science, computers, software, education, economics, social equity, philosophy — but these are just outside labels and not that important, deep inside we are all just consciousness, right?

Источник

REST API with Jersey and Spring

announcement - icon

The Kubernetes ecosystem is huge and quite complex, so it’s easy to forget about costs when trying out all of the exciting tools.

Читайте также:  Factory method to create object in java

To avoid overspending on your Kubernetes cluster, definitely have a look at the free K8s cost monitoring tool from the automation platform CAST AI. You can view your costs in real time, allocate them, calculate burn rates for projects, spot anomalies or spikes, and get insightful reports you can share with your team.

Connect your cluster and start monitoring your K8s costs right away:

We rely on other people’s code in our own work. Every day.

It might be the language you’re writing in, the framework you’re building on, or some esoteric piece of software that does one thing so well you never found the need to implement it yourself.

The problem is, of course, when things fall apart in production — debugging the implementation of a 3rd party library you have no intimate knowledge of is, to say the least, tricky.

Lightrun is a new kind of debugger.

It’s one geared specifically towards real-life production environments. Using Lightrun, you can drill down into running applications, including 3rd party dependencies, with real-time logs, snapshots, and metrics.

Learn more in this quick, 5-minute Lightrun tutorial:

announcement - icon

Slow MySQL query performance is all too common. Of course it is. A good way to go is, naturally, a dedicated profiler that actually understands the ins and outs of MySQL.

The Jet Profiler was built for MySQL only, so it can do things like real-time query performance, focus on most used tables or most frequent queries, quickly identify performance issues and basically help you optimize your queries.

Critically, it has very minimal impact on your server’s performance, with most of the profiling work done separately — so it needs no server changes, agents or separate services.

Basically, you install the desktop application, connect to your MySQL server, hit the record button, and you’ll have results within minutes:

announcement - icon

DbSchema is a super-flexible database designer, which can take you from designing the DB with your team all the way to safely deploying the schema.

The way it does all of that is by using a design model, a database-independent image of the schema, which can be shared in a team using GIT and compared or deployed on to any database.

And, of course, it can be heavily visual, allowing you to interact with the database using diagrams, visually compose queries, explore the data, generate random data, import data or build HTML5 database reports.

announcement - icon

The Kubernetes ecosystem is huge and quite complex, so it’s easy to forget about costs when trying out all of the exciting tools.

Читайте также:  Символ html точка запятая

To avoid overspending on your Kubernetes cluster, definitely have a look at the free K8s cost monitoring tool from the automation platform CAST AI. You can view your costs in real time, allocate them, calculate burn rates for projects, spot anomalies or spikes, and get insightful reports you can share with your team.

Connect your cluster and start monitoring your K8s costs right away:

Get started with Spring 5 and Spring Boot 2, through the reference Learn Spring course:

Get started with Spring 5 and Spring Boot 2, through the reference Learn Spring course:

We’re looking for a new Java technical editor to help review new articles for the site.

1. Overview

Jersey is an open source framework for developing RESTful Web Services. It serves as a reference implementation of JAX-RS.

In this article, we’ll explore the creation of a RESTful Web Service using Jersey 2. Also, we’ll use Spring’s Dependency Injection (DI) with Java configuration.

2. Maven Dependencies

Let’s begin by adding dependencies to the pom.xml:

 org.glassfish.jersey.containers jersey-container-servlet 2.26  org.glassfish.jersey.media jersey-media-json-jackson 2.26 

Also, for Spring integration we have to add the jersey-spring4 dependency:

 org.glassfish.jersey.ext jersey-spring4 2.26 

The latest version of these dependencies is available at jersey-container-servlet, jersey-media-json-jackson and jersey-spring4.

3. Web Configuration

Next, we need to set up a web project to do Servlet configuration. For this, we’ll use Spring’s WebApplicationInitializer:

@Order(Ordered.HIGHEST_PRECEDENCE) public class ApplicationInitializer implements WebApplicationInitializer < @Override public void onStartup(ServletContext servletContext) throws ServletException < AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext(); servletContext.addListener(new ContextLoaderListener(context)); servletContext.setInitParameter( "contextConfigLocation", "com.baeldung.server"); >>

Here, we’re adding the @Order(Ordered.HIGHEST_PRECEDENCE) annotation to ensure that our initializer is executed before the Jersey-Spring default initializer.

4. A Service Using Jersey JAX-RS

4.1. Resource Representation Class

Let’s use a sample resource representation class:

@XmlRootElement public class Employee < private int id; private String firstName; // standard getters and setters >

Note that JAXB annotations like @XmlRootElement are required only if XML support is needed (in addition to JSON).

4.2. Service Implementation

Let’s now look at how we can use JAX-RS annotations to create RESTful web services:

@Path("/employees") public class EmployeeResource < @Autowired private EmployeeRepository employeeRepository; @GET @Path("/") @Produces(< MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML >) public Employee getEmployee(@PathParam("id") int id) < return employeeRepository.getEmployee(id); >@POST @Consumes(< MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML >) public Response addEmployee( Employee employee, @Context UriInfo uriInfo) < employeeRepository.addEmployee(new Employee(employee.getId(), employee.getFirstName(), employee.getLastName(), employee.getAge())); return Response.status(Response.Status.CREATED.getStatusCode()) .header( "Location", String.format("%s/%s",uriInfo.getAbsolutePath().toString(), employee.getId())).build(); >>

The @Path annotation provides the relative URI path to the service. We can also embed variables within the URI syntax, as the variable shows. Then, the variables will be substituted at runtime. To obtain, the value of the variable we can use the @PathParam annotation.

@GET, @PUT, @POST, @DELETE and @HEAD define the HTTP method of the request, which will be processed by annotated methods.

The @Produces annotation defines the endpoint’s response type (MIME media type). In our example, we’ve configured it to return either JSON or XML depending on the value of HTTP header Accept (application/json or application/xml).

On the other hand, the @Consumes annotation defines the MIME media types that the service can consume. In our example, the service can consume either JSON or XML depending on the HTTP header Content-Type (application/json or application/xml).

The @Context annotation is used to inject information into a class field, bean property or method parameter. In our example, we’re using it to inject UriInfo. We can also use it to inject ServletConfig, ServletContext, HttpServletRequest and HttpServletResponse.

5. Using ExceptionMapper

ExceptionMapper allows us to intercept the exceptions and return appropriate HTTP response code to the client. In the following example, HTTP response code 404 is returned if EmployeeNotFound exception is thrown:

@Provider public class NotFoundExceptionHandler implements ExceptionMapper  < public Response toResponse(EmployeeNotFound ex) < return Response.status(Response.Status.NOT_FOUND).build(); >>

6. Managing Resource Classes

Finally, let’s wire up all service implementation classes and exception mappers against an application path:

@ApplicationPath("/resources") public class RestConfig extends Application < public Set> getClasses() < return new HashSet>( Arrays.asList( EmployeeResource.class, NotFoundExceptionHandler.class, AlreadyExistsExceptionHandler.class)); > >

7. API Testing

Let’s now test the APIs with some live tests:

public class JerseyApiLiveTest < private static final String SERVICE_URL = "http://localhost:8082/spring-jersey/resources/employees"; @Test public void givenGetAllEmployees_whenCorrectRequest_thenResponseCodeSuccess() throws ClientProtocolException, IOException < HttpUriRequest request = new HttpGet(SERVICE_URL); HttpResponse httpResponse = HttpClientBuilder .create() .build() .execute(request); assertEquals(httpResponse .getStatusLine() .getStatusCode(), HttpStatus.SC_OK); >>

8. Conclusion

In this article, we’ve introduced the Jersey framework and developed a simple API. We’ve used Spring for Dependency Injection features. We have also seen the use of ExceptionMapper.

As always, the full source code is available in this Github project.

announcement - icon

Slow MySQL query performance is all too common. Of course it is. A good way to go is, naturally, a dedicated profiler that actually understands the ins and outs of MySQL.

The Jet Profiler was built for MySQL only, so it can do things like real-time query performance, focus on most used tables or most frequent queries, quickly identify performance issues and basically help you optimize your queries.

Critically, it has very minimal impact on your server’s performance, with most of the profiling work done separately — so it needs no server changes, agents or separate services.

Basically, you install the desktop application, connect to your MySQL server, hit the record button, and you’ll have results within minutes:

Источник

Оцените статью