카테고리 없음

Introduction to REST in Java with JAX-RS

hajinny 2021. 7. 31. 20:01

Hmmm only if Java was called Pava, it would be RIP (REST in Pava). Haha.

 

Here's what we want to do to achieve REST in Java (very similar to Servlet, actually!)

- make our Resources available to HTTP requests 

 

basically, we want to specify what happens with GET (or any HTTP methods) request to certain url endpoint like localhost:8080/services/hello.

 

Make Resource

Resources are equivalent to Servlet, with some differences.

Servlet

 

 

Resrouce

 

The difference here is that unlike in our servlet example, web.xml doesn't need to do any mapping. In fact, it's empty! Resource above defines what happens with GET request to (something)/greetings/hello. The reason why I had '(something)' written is because there's a parent class that maps Resource to top level url.

 

Map resource to url

This is all you need! So we defined what happens with GET request to /services/greetings/hello

 

In fact:

http://localhost:8080/example_05_jax_rs_war_exploded/services/greetings/hello

produces

response from GreetingsResource

That's it!