Java/Spring

Hello Spring

WebDevLee 2022. 2. 10. 15:39

자바의 스프링 프레임워크와 HTTP요청을 이용하는 방법을 정리하였습니다.

 

 


< What is Spring? >

Spring is an open-source Java framework that is useful, among other things, for building RESTful web applications. Like any web framework, Spring comes with an established set of code conventions and patterns, such as predefined templates, databases, and functions.

Spring contains templates for many different kinds of applications, or “Spring Projects”, (including Spring Cloud, Spring Web Services, Spring Security, etc.).

Spring Boot is one of the easiest ways to build Spring web projects, enabling applications to run with minimal configuration.

 

The way a Spring web application actually work(usually) :

Web Server : Spring web Server

 

 


< Making GET Requests with a Web Browser >

GET Request : retrieve an existing resource from a server.

Whenever a user navigates to a website or clicks on a URL link, for example, they are initiating a GET request.

 

Example : working with an online system that is keeping track of dog patient intakes for a pet clinic. 

1. A user types http://www.mypetclinic.com/dogs/ into the address bar, the browser

GET http://www.mypetclinic.com/dogs/

2. The server, upon receiving this GET request, should return a response that looks something like this:

 

{
  "dogs": [
    {
      "id": 1,
      "name": "Bella",
      "breed": "Golden Retriever"
    },
    {
      "id": 2,
      "name": "Max",
      "breed": "Bulldog"
    },
    {
      "id": 3,
      "name": "Ruby",
      "breed": "Poodle"
    },
  ]
}

 

 


<  >

Alternatively, we can make GET requests using Curl.

Curl, short for Client for URLs, is a command line tool that allows us to transfer data to and from a server.

 

  • Curl is also useful for testing HTTP request methods from your local command line.

 

Example : test the HTTP endpoint http://www.mypetclinic.com/dogs/ in CLI

curl http://www.mypetclinic.com/dogs/

: Uses the GET request as the default method to retrieve the dogs resource from that URL; the dogs resource is displayed in the terminal rather than the browser.

 

 


< Making POST Requests with a Web Browser >

POST : create new resources

A common way to append data to an application is by submitting an HTML form

 

 

Example : Add a new restaurant

 

 


< Making POST Requests with Curl >

Example : Add a new restaurant

curl -X POST -d "{\"name\":\"Pizza Hut\", \"line1\":\"123 Main St.\",
\"city\":\"San Diego\", \"state\":\"CA\", \"zipCode\":\"90029\"}" -H
"Content-Type: application/json" http://localhost:4001/restaurants/
  • -X POST(short for --request) tells the server that the client is making a POST request, where -X is the curl parameter specifying the type of request method to use.
  • -d (short for --data) indicates to the server that the client is sending new data to an existing application.
  • The content inside "{ }" is the data that the client wants to send (as indicated by the preceding -d)
    We use the {\"key1\":\"value1\", \"key2\":\"value2\"} syntax
    : name, line1, city, state, zipCode  => the names of the keys
    : Pizza Hut, 123 Main st. , San Diego, CA, 90029 => the names of the values

  • -H(short for --header) "Content-Type: application/json" specifies that we are sending data in JSON format.(H means Header)
  • http://localhost:4001/restaurants/ tells the server where to send this new data