Home » Tag Archives: JAX-RS

Tag Archives: JAX-RS

Handle custom exception types in JAX-RS

java-interview-questions-answers

JAX-RS supports handling custom exceptions — thrown in either EJBs or CID beans — to custom HTTP responses. Assuming we have an “exceptional” EJB: @Stateless public class Hello { public String greeting() { if (new Random().nextBoolean()) throw new GreetingException("Could not greet"); return "hello"; } } @ApplicationException public class GreetingException extends RuntimeException { public GreetingException(String message) { super(message); } } The EJB is used in ...

Read More »

Using JAX-RS exceptions for status codes

java-interview-questions-answers

One way to send specific HTTP (error) status codes from a JAX-RS resource is to use the javax.ws.rs.core.Response class with its Builder Pattern-like API. If you want to specify the return type according to the response body, you can still do so and send a different status on errors by throwing a WebApplicationException. @Path("test") public class TestResource { @GET public ...

Read More »

Jersey client dependencies for JAX-RS 2.1

java-interview-questions-answers

Jersey is the reference implementation of JAX-RS 2.1. The following Jersey dependencies are required in order to run a JAX-RS 2.1 client with JSON-P and JSON-B mapping outside of an enterprise container. Jersey client version 2.6 implements the JAX-RS 2.1 API. Following dependencies add the client runtime to a project: <dependency> <groupId>org.glassfish.jersey.core</groupId> <artifactId>jersey-client</artifactId> <version>2.26</version> </dependency> <dependency> <groupId>org.glassfish.jersey.inject</groupId> <artifactId>jersey-hk2</artifactId> <version>2.26</version> </dependency> ...

Read More »

Better late than never: SSE, or Server-Sent Events, are now in JAX-RS

java-interview-questions-answers

Server-Sent Events (or just SSE) is quite useful protocol which allows the servers to push data to the clients over HTTP. This is something our web browsers support for ages but, surprisingly, neglected by JAX-RS specification for quite a long time. Although Jersey had an extension available for SSE media type, the API has never been formalized and as such, ...

Read More »

What are JAX-RS Annotations? (Part 3)

java-interview-questions-answers

Overview of JAX-RS Annotations (Part 3) This is a three-part series looking at the annotation that is used to implement REST endpoints. In part two of JAX-RS annotations you learnt about: The @Path Annotation and @PathParam The @QueryParamter Annotation The @Produces Annotation The @Consumes Annotation In this part, you will learn more about JAX-RS annotations. Are you ready? let’s get started. ...

Read More »

What are JAX-RS Annotations? (Part 2)

java-interview-questions-answers

Overview of JAX-RS Annotations (Part 2) This is a three-part series looking at the annotation that is used to implement REST endpoints. In part one of JAX-RS annotations you learnt about: The @ApplicationPath Annotation The @Path Annotation The @GET HTTP Method Annotation The @POST HTTP Method Annotation The @PUT HTTP Method Annotation The @DELETE HTTP Method Annotation The @OPTIONS HTTP Method Annotation ...

Read More »

What are JAX-RS Annotations?

java-interview-questions-answers

Overview of JAX-RS (Part 1) The JAX-RS API forms an important part of the Java EE platforms commitment to provide standards-driven technology. The ubiquitous nature of the internet and that recent increasing interest in the microservice architecture has put more focus on small scalable autonomous services and their interoperability. The principal methodology used to allow microservice to communicate with each other and the ‘outside ...

Read More »

Keep your promises: contract-based testing for JAX-RS APIs

java-interview-questions-answers

It’s been a while since we talked about testing and applying effective TDD practices, particularly related to REST(ful) web services and APIs. But this topic should have never been forgotten, especially in the world where everyone is doing microservices, whatever it means, implies or takes. To be fair, there are quite a lot of areas where microservice-based architecture shines and ...

Read More »

Laziness at extreme: developing JAX-RS services with Spring Boot

spring-interview-questions-answers

I think it would be fair to state that we, as software developers, are always looking for the ways to write less code which does more stuff, automagically or not. With this regards, Spring Boot project, proud member of the Spring portfolio, disrupted the traditional approaches, dramatically speeding up and simplifying Spring-based applications development. There is a lot to be ...

Read More »