Java - Rest

> Procedural Languages > Java

1 - About

Advertising

3 - Specification

  • Jax Rs is an annotated Rest specification and jersey is the most well known implementation

4 - Test

When testing a Rest API (ie when writing a unit test), there is two approach:

  • you create a context object to emulate an incoming request
  • or you start an embedded server and use a client to make http request

Example:

ServerClientTest.java
public class ServerClientTest {
 
    private HttpServer server;
    private WebTarget target;
 
    @Before
    public void setUp() throws Exception {
        server = Main.startServer();
 
        Client c = ClientBuilder.newClient();
        target = c.target(Main.BASE_URI);
    }
 
    @After
    public void tearDown() throws Exception {
        server.stop();
    }
 
    /**
     * Test to see that the message "Got it!" is sent in the response.
     */
    @Test
    public void testGetIt() {
        String responseMsg = target.path("myresource").request().get(String.class);
        assertEquals("Got it!", responseMsg);
    }
}

4.1 - Javdoc

lang/java/rest.txt · Last modified: 2019/11/13 18:33 by gerardnico