Jetty handler swing handler differences

Jetty handler swing handler differences

How to add servlet handlers for jetty server ?

Jetty handler :  Add a class extending HttpServlet as handler for a path.

.addHandler(“p1”,
c1.class)

Swing handler:

ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
context.setContextPath(“/”);
context.addServlet(new ServletHolder(new ServletContainer(new MSLifeCycleApplication())), “/*”);

Pass context as an argument to setHandler

register(InvalidRequestExceptionMapper.class);
register(ValidationExceptionMapper.class);
register(new AbstractBinder() {
@Override
protected void configure() {
bind(msLifeCycleHandler).to(MSLifeCycleHandler.class);
}
});

/*
* Specify where resource classes are located. These are the classes that constitute the API.
*/
packages(true, “com.microservice.api”);
All classes in above package path constitutes the API

Get data from swing api:

@POST
@Path(“/e”)
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)

public Response api1(String requestJson, public Response api1(String requestJson,                                  @Context HttpHeaders httpHeaders)

{

String h1 = httpHeaders.getHeaderString(“h1”);

switch (h1) {

case “1”: {

if (h1 != null) {

if (validateJsonSyntax(requestJson)) {

ObjectMapper objectMapper = new ObjectMapper();                                try {

Object1 obj = objectMapper.readValue(requestJson, json1.class);        return Response.status(Response.Status.ACCEPTED).build();                                }

catch (Exception e) {

return Response.status(Response.Status.BAD_REQUEST).build();                                }

}

}
}
break;

default:            {            }            break;
}        return Response.status(Response.Status.BAD_REQUEST).build();

}

Get data from HttpServet handlers:
public class c1 extends HttpServlet {

@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

//super.doPost(request, response);

String h1 = request.getHeader(“h1”);
String h2 = request.getHeader(“h2”);

switch (h1) {

case “one”: {

if (h2 != null) {
InputStream inStrReqBody = request.getInputStream();
if (inStrReqBody != null) {
String strReqBody = IOUtils.toString(inStrReqBody,
request.getCharacterEncoding());
if (validateJsonSyntax(strReqBody)) {
ObjectMapper objectMapper = new ObjectMapper();
try {
Object1 obj1 = objectMapper.readValue(strReqBody,Object1.class);
response.setStatus(HttpServletResponse.SC_ACCEPTED);
} catch (Exception e) {

response.setStatus(HttpServletResponse.SC_BAD_REQUEST);

}
}
}
}
}

}
}

Calling swing api:

Response response = client.target(“http://localhost:8082”)
.path(“/e”)
.request(MediaType.APPLICATION_JSON)
.header(“h1”, “”)
.header(“h2”, “”)
.header(“h3”, “”)
.post(Entity.entity(request, MediaType.APPLICATION_JSON));

Leave a Reply

Your email address will not be published. Required fields are marked *