Skip to content

Instantly share code, notes, and snippets.

@mayankchoubey
Created July 13, 2021 22:09
Show Gist options
  • Save mayankchoubey/9a990421f33d9d7519397f73770ddb65 to your computer and use it in GitHub Desktop.
Save mayankchoubey/9a990421f33d9d7519397f73770ddb65 to your computer and use it in GitHub Desktop.
Deno native HTTP v/s Spring Boot HTTP
package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.http.ResponseEntity;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.RestController;
@SpringBootApplication
@RestController
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
@PostMapping("/")
public ReqName handleRequest(@RequestBody ReqName rBody) {
return rBody;
}
}
class ReqName{
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
const listener = Deno.listen({ port: 3000 });
for await(const conn of listener)
handleNewConnection(conn);
async function handleNewConnection(conn: Deno.Conn) {
for await(const { request, respondWith } of Deno.serveHttp(conn)) {
const reqBody=await request.json();
if(reqBody) {
respondWith(new Response(JSON.stringify({ name: reqBody.name }), {
headers: { 'content-type': 'application/json'}
}));
} else
respondWith(new Response(undefined, {status: 400}));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment