Light Webservice

Sonatype helps open source projects to set up Maven repositories on https://oss.sonatype.org/

License

License

GroupId

GroupId

com.moebiusgames
ArtifactId

ArtifactId

light-ws
Last Version

Last Version

1.3
Release Date

Release Date

Type

Type

jar
Description

Description

Light Webservice
Sonatype helps open source projects to set up Maven repositories on https://oss.sonatype.org/
Source Code Management

Source Code Management

https://github.com/entrusc/light-ws

Download light-ws

How to add to project

<!-- https://jarcasting.com/artifacts/com.moebiusgames/light-ws/ -->
<dependency>
    <groupId>com.moebiusgames</groupId>
    <artifactId>light-ws</artifactId>
    <version>1.3</version>
</dependency>
// https://jarcasting.com/artifacts/com.moebiusgames/light-ws/
implementation 'com.moebiusgames:light-ws:1.3'
// https://jarcasting.com/artifacts/com.moebiusgames/light-ws/
implementation ("com.moebiusgames:light-ws:1.3")
'com.moebiusgames:light-ws:jar:1.3'
<dependency org="com.moebiusgames" name="light-ws" rev="1.3">
  <artifact name="light-ws" type="jar" />
</dependency>
@Grapes(
@Grab(group='com.moebiusgames', module='light-ws', version='1.3')
)
libraryDependencies += "com.moebiusgames" % "light-ws" % "1.3"
[com.moebiusgames/light-ws "1.3"]

Dependencies

compile (3)

Group / Artifact Type Version
org.eclipse.jetty : jetty-server jar 9.4.18.v20190429
com.fasterxml.jackson.core : jackson-databind jar 2.9.10.1
com.fasterxml.jackson.datatype : jackson-datatype-jsr310 jar 2.9.9

test (3)

Group / Artifact Type Version
junit : junit jar 4.12
org.hamcrest : hamcrest-core jar 1.3
org.springframework : spring-web jar 4.3.3.RELEASE

Project Modules

There are no modules declared in this project.

Light-WS

Sometimes a full blown spring framework is not an option, and neither is an application server (e.g. when resources are limited). This library is a lightweight REST webservice implementation. It is based on jetty and is capable of starting a webservice on a low-end device like the Raspberry Pi in a few seconds (compared to minutes for spring-boot).

license

Light-WS is licensed under MIT license and can therefore be used in any project, even for commercial ones.

usage

for Gradle

compile 'com.moebiusgames:light-ws:1.3'

for Maven

<dependency>
    <groupId>com.moebiusgames</groupId>
    <artifactId>light-ws</artifactId>
    <version>1.3</version>
</dependency>

example

@WebService("/ws")
public class MyWebService {

    @GetMapping("/get/some/info/{id}")
    public String getSomeInfo(@GetParameter("id") String id) {
        return "Hello " + id;
    }

    @GetMapping("/get/some/otherthing/{id}")
    @ResultMimeType("text/html; charset=utf-8") //without this the default mime type is text/plain
    public Response<Item> getSomeInfo(@GetParameter("id") int id) {
        Response<String> response = new Response("Hello " + id);
        if (id < 100) {
            return new Response(null, HttpServletResponse.SC_NOT_FOUND);
        }
        return new Response(new Item(id), HttpServletResponse.SC_OK);
    }

    @PostMapping("/post/sth")
    public void getSomePost(@PostParameter MyObject obj) {
        // (expects the post parameter to be type application/json)
        // do sth. with obj ...
    }

    @PostMapping("/upload")
    @ResultMimeType("text/html; charset=utf-8") //without this the default mime type is text/plain
    public String uploadFile(@PostParameter UploadedFile uploadedFile) {
        // use uploadedFile.openInputStream() to open the uploaded file for reading
        return "<html><h1>Uploaded successfully</h1></html>";
    }

}

Then you can start the webserver like this:

Server server = new Server(8080);
server.setHandler(new HandlerList(
        new WebServiceHandler<>(new MyWebService())
        //...
));
server.start();

Now you should be able to open http://localhost:8080/ws/get/some/info/123 and get a nice greeting. And you should also be able to post a JSON to /ws/post/sth.

Note that you can also mix get(path) parameters and post like this:

@PostMapping("/post/sth/{name}")
public void getSomePost(@PostParameter MyObject obj,
                @GetParameter("name") String name) {
    // (expects the post parameter to be type application/json)
    // do sth. with obj ...
}

build

mvn clean package

Versions

Version
1.3
1.2
1.1
1.0