Creating a web service involves using frameworks or libraries specific to the programming language. Here are simple examples of creating a web service in C#, Java (using Spring Boot), and Golang. These examples demonstrate a basic HTTP server serving a “Hello, World!” message. For a real-world application, you would typically define routes, handle requests, and interact with databases or external services.
C# (.NET Core) Web Service:
using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Http; using Microsoft.Extensions.DependencyInjection; class Program { static void Main() { var host = new WebHostBuilder() .UseKestrel() .ConfigureServices(services => services.AddMvc()) .Configure(app => app.UseMvcWithDefaultRoute()) .Build(); host.Run(); } } public class Startup { public void Configure(IApplicationBuilder app) { app.UseMvc(); } } public class HomeController { public string Index() { return "Hello, World! from C# Web Service"; } }
Java (Spring Boot) Web Service:
Create a new file named Application.java
:
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; @SpringBootApplication public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } } @RestController class HelloWorldController { @GetMapping("/") public String helloWorld() { return "Hello, World! from Java Web Service"; } }
Golang (Go) Web Service:
package main import ( "fmt" "net/http" ) func handler(w http.ResponseWriter, r *http.Request) { fmt.Fprintf(w, "Hello, World! from Golang Web Service") } func main() { http.HandleFunc("/", handler) http.ListenAndServe(":8080", nil) }
These examples use different frameworks (ASP.NET Core for C#, Spring Boot for Java, and the standard library for Golang) to create a simple web service. Run these programs, and you will have a web service running at http://localhost:8080
for Golang and Java, and http://localhost:5000
for C#. You can modify and extend these examples based on your specific requirements.