DROPWIZARD - basic

XYMON·2023년 7월 18일

Webserver using dropwizard framework.

Getting Started

mvn archetype:generate -DarchetypeGroupId=io.dropwizard.archetypes -DarchetypeArtifactId=java-simple -DarchetypeVersion=[REPLACE WITH A VALID DROPWIZARD VERSION]

You can make a maven-dropwizard project using this command.
And this is example.

The dir structure of that archetype is this.

com.example
- api
- cli
- client
- core
- db
- health
- resources

API

A representation class.
If desired hello world data is this,

{
  "id": 1,
  "content": "Hi!"
}

we can make an api class to represent it.

package com.example.helloworld.api;

import com.fasterxml.jackson.annotation.JsonProperty;

public class Saying {
    private long id;
    private String content;

    public Saying() {
        // Jackson deserialization
    }

    public Saying(long id, String content) {
        this.id = id;
        this.content = content;
    }

    @JsonProperty
    public long getId() {
        return id;
    }

    @JsonProperty
    public String getContent() {
        return content;
    }
}

The 'public Saying()' constructor is needed for Jackson to desrialize a JSON into Java object.
When Jackson create an instance of Java object to map json data, it uses reflection and invokes the constructor. - instantiate empty object, and then populate json data.

Reflection? <- a feature that allows a program to examine and manipulate its own structure, classes, methods, fields, and interfaces at runtime.(dynamically)

Resources

A POJO(Plain Old Java Object) acts like a handler/controller for processing HTTP requests and producing HTTP responses.

package com.example.helloworld.resources;

import jakarta.ws.rs.GET;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.Produces;
import jakarta.ws.rs.core.MediaType;

@Path("/hello")
@Produces(MediaType.APPLICATION_JSON)
public class HelloWorldResource{
    private final String template;
    private final String defaultName;
    private final AtomicLong counter;

    public HelloWorldResource(String template, String defaultName) {
        this.template = template;
        this.defaultName = defaultName;
        this.counter = new AtomicLong();
    }

    @GET
    @Timed
    public Saying sayHello(@QueryParam("name") Optional<String> name) {
        final String value = String.format(template, name.orElse(defaultName));
        return new Saying(counter.incrementAndGet(), value);
    }
    
}

If template is "Hello, %s", then content of response will be "Hello, ~~~(or default name)".
With "@Timed" annot, Dropwizard automatically records the duration and rate of its invocations as a Metrics Timer.
The AtomicLong is used to generate unique id in thread-safe way.(it will work in distributed Server?)

profile
염염

3개의 댓글

comment-user-thumbnail
2023년 7월 18일

글이 많은 도움이 되었습니다, 감사합니다.

답글 달기
comment-user-thumbnail
2023년 7월 18일

잘 봤습니다. 좋은 글 감사합니다.

답글 달기
comment-user-thumbnail
2024년 11월 26일

Escorts Near Hotel The Eros: Our selection of Escorts Near Hotel The Eros promises an unforgettable experience with stunning companions who are well-versed in creating memorable moments tailored to your desires.

답글 달기