REST Mock API Server Example

Integration projects require APIs contract and implementation to carry out integration work. Once contract is defined you need API end point from other project/service to finish your work.
If you dont get it on time, you are kind of stuck, what can you do? Create your own API end point and finish your side of implementation by changing your url end point to something local. That local service should be your mock service which gives you desire results same as like actual APIs.

MockServer is one of such library which can allow you to mock your thirdparty or dependent APIs.

Take a look of below easy example.

import org.mockserver.client.server.*;
import org.mockserver.model.*;
import static org.mockserver.integration.ClientAndServer.*;
import static org.mockserver.model.HttpRequest.*;
import static org.mockserver.model.HttpResponse.*;
import static java.util.concurrent.TimeUnit.*;

public class DummyPOSTService {
	public static void main(String[] args) {
		MockServerClient mockServer = startClientAndServer(1100);

                    .withHeader("authorization", "dummy")
                            new Header("Content-Type", "application/json; charset=utf-8"),
                            new Header("Cache-Control", "public, max-age=86400")
                    .withBody("{ 'test': 'abc' }")
                    .withDelay(new Delay(SECONDS, 1))

You need this jar in order to run above class.

<a href="">mockserver-netty-3.10.4-jar-with-dependencies</a>


RestEasy 3.0.x implementation with your existing servlet based web application

Web.xml entry:


Your REST class:



public class UserREST {

	public String doGet(@QueryParam("userId") String userId, @HeaderParam("authorization") String sessionToValidate)
			throws Exception {
			return "SomeString";

Minimum jar files needed from jboss resteasy 3.0.19 bundle:


And call : http(s)://yourhost:port/yourapplication/rest/Users/getUserById?userId=xx
You may choose to pass your own authorization in header, use Postman, but you can ignore it also.