Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 10da69b

Browse files
🎉 Begin
0 parents commit 10da69b

File tree

3 files changed

+76
-0
lines changed

3 files changed

+76
-0
lines changed

‎Server.java

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import java.io.*;
2+
import java.net.*;
3+
import com.sun.net.httpserver.*;
4+
5+
public class Server {
6+
public static void main(String[] args) throws Exception {
7+
var port = new InetSocketAddress(8000);
8+
9+
var server = HttpServer.create(port, 0);
10+
11+
server.createContext("/randomNumber", new RandomNumberHandler());
12+
13+
server.setExecutor(null);
14+
15+
server.start();
16+
}
17+
18+
static class RandomNumberHandler implements HttpHandler {
19+
@Override
20+
public void handle(HttpExchange request) throws IOException {
21+
var randomNumber = (int) (Math.random() * 100);
22+
23+
var response = Integer.toString(randomNumber);
24+
25+
var responseLength = response.getBytes().length;
26+
27+
request.sendResponseHeaders(200, responseLength);
28+
29+
var body = request.getResponseBody();
30+
31+
body.write(response.getBytes());
32+
33+
body.close();
34+
}
35+
}
36+
}

‎license

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2022 Eliaz Bobadilla <eliaz.bobadilla@gmail.com>
4+
(https://ultirequiem.com)
5+
6+
Permission is hereby granted, free of charge, to any person obtaining a copy of
7+
this software and associated documentation files (the "Software"), to deal in
8+
the Software without restriction, including without limitation the rights to
9+
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
10+
the Software, and to permit persons to whom the Software is furnished to do so,
11+
subject to the following conditions:
12+
13+
The above copyright notice and this permission notice shall be included in all
14+
copies or substantial portions of the Software.
15+
16+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
18+
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
19+
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
20+
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21+
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

‎readme.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Java SE Simple HTTP Rest API
2+
3+
[Based on this Stack Overflow Question](https://stackoverflow.com/questions/3732109)
4+
5+
## Support
6+
7+
Open an Issue, I will check it a soon as possible 👀
8+
9+
If you want to hurry me up a bit
10+
[send me a tweet](https://twitter.com/UltiRequiem) 😆
11+
12+
Consider [supporting me on Patreon](https://patreon.com/UltiRequiem) if you like
13+
my work 🙏
14+
15+
Don't forget to start the repo ⭐
16+
17+
## Licence
18+
19+
Licensed under the MIT License 📄

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /