0% found this document useful (0 votes)
2 views48 pages

JAVA UNIT 5

The document contains multiple Java classes demonstrating different aspects of dependency injection in a Car engine context, as well as examples of making HTTP requests (GET, POST, PUT, DELETE) to a placeholder API. It illustrates how to inject dependencies using both setter and constructor methods in the Car class. Additionally, it shows how to handle API calls using HttpURLConnection to perform various operations on JSON data.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views48 pages

JAVA UNIT 5

The document contains multiple Java classes demonstrating different aspects of dependency injection in a Car engine context, as well as examples of making HTTP requests (GET, POST, PUT, DELETE) to a placeholder API. It illustrates how to inject dependencies using both setter and constructor methods in the Car class. Additionally, it shows how to handle API calls using HttpURLConnection to perform various operations on JSON data.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 48

class Engine {

void start() {
System.out.println("Engine started");
}
}

class Car {
Engine engine;
void setEngine(Engine engine) { // Setter injection
this.engine = engine;
}
void drive() {
engine.start();
System.out.println("Car is running");
}
}

public class Main {


public static void main(String[] args) {
Engine e = new Engine();
Car c = new Car();
c.setEngine(e); // Injecting dependency
c.drive();
}
}
class Engine {
void start() {
System.out.println("Engine started");
}
}

class Car {
Engine engine;
Car(Engine engine) { // Constructor injection
this.engine = engine;
}
void drive() {
engine.start();
System.out.println("Car is running");
}
}

public class Main {


public static void main(String[] args) {
Engine e = new Engine();
Car c = new Car(e); // Injecting dependency
c.drive();
}
}
import java.io.*;
import java.net.*;

public class GetAPIDemo {


public static void main(String[] args) throws Exception {
URL url = new URL("https://jsonplaceholder.typicode.com/posts/1");
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setRequestMethod("GET");

BufferedReader in = new BufferedReader(


new InputStreamReader(con.getInputStream()));
String line;
while ((line = in.readLine()) != null) {
System.out.println(line);
}
in.close();
con.disconnect();
}
}
import java.io.*;
import java.net.*;

public class PostAPIDemo {


public static void main(String[] args) throws Exception {
URL url = new URL("https://jsonplaceholder.typicode.com/posts");
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setRequestMethod("POST");
con.setRequestProperty("Content-Type", "application/json; utf-8");
con.setDoOutput(true);

String json = "{\"title\":\"Hello\",\"body\":\"World\",\"userId\":1}";

try (OutputStream os = con.getOutputStream()) {


os.write(json.getBytes("utf-8"));
}

BufferedReader br = new BufferedReader(new InputStreamReader(con.getInputStream(), "utf-8"));


String line;
while ((line = br.readLine()) != null) {
System.out.println(line);
}
br.close();
con.disconnect();
}
}
import java.io.*;
import java.net.*;

public class PutAPIExample {


public static void main(String[] args) throws Exception {
URL url = new URL("https://jsonplaceholder.typicode.com/posts/1");
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setRequestMethod("PUT");
con.setRequestProperty("Content-Type", "application/json; utf-8");
con.setDoOutput(true);

String json = "{\"id\":1,\"title\":\"updated\",\"body\":\"data\",\"userId\":1}";

try (OutputStream os = con.getOutputStream()) {


os.write(json.getBytes("utf-8"));
}

BufferedReader br = new BufferedReader(new InputStreamReader(con.getInputStream(), "utf-8"));


String line;
while ((line = br.readLine()) != null) {
System.out.println(line);
}
br.close();
con.disconnect();
}
}
import java.net.HttpURLConnection;
import java.net.URL;

public class DeleteAPIExample {


public static void main(String[] args) throws Exception {
URL url = new URL("https://jsonplaceholder.typicode.com/posts/1");
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setRequestMethod("DELETE");

int responseCode = con.getResponseCode();


System.out.println("DELETE Response Code: " + responseCode);
con.disconnect();
}
}

You might also like