Chapter 11 - Structured Data and Responses in REST

0.0(0)
studied byStudied by 2 people
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
Card Sorting

1/38

encourage image

There's no tags or description

Looks like no tags are added yet.

Study Analytics
Name
Mastery
Learn
Test
Matching
Spaced

No study sessions yet.

39 Terms

1
New cards

What is the default data format for structured data in RESTful services?

JSON (JavaScript Object Notation).

2
New cards

Why is JSON preferred over XML in REST?

It's simpler, human-readable, widely supported, and doesn't require extra configuration in Spring.

3
New cards

REST vs SOAP for structured data

4
New cards

How can you manually create a JSON object in Java?

Using JsonObjectBuilder from javax.json:

java

Json.createObjectBuilder().add("Fruit", "Lemon").build();

5
New cards

What's a better alternative to manually building JSON in Spring?

Returning Java objects and letting Spring (with Jackson) auto-convert them to JSON.

6
New cards

What does Spring use to automatically convert Java objects to JSON?

Jackson (a JSON serialization library).

7
New cards

What must a Java class have for Spring to serialize it to JSON?

Getter methods (getX()) for each field you want to include.

8
New cards

Do setter methods matter for JSON serialization?

No, they only matter for deserialization (receiving JSON).

9
New cards

Can immutable classes (with constructors and final fields) be serialized?

Yes, as long as they have getters and a constructor.

10
New cards

Can Java Records be used for JSON data classes?

Yes! Records are supported out of the box by Jackson.

11
New cards

What does produces=MediaType.APPLICATION_JSON_VALUE do?

Explicitly declares that the method returns JSON.

12
New cards

What happens if you set produces=MediaType.APPLICATION_XML_VALUE without configuration?

You get a 500 Internal Server Error.

13
New cards

How do you receive JSON data in a POST request?

Use @PostMapping(..., consumes=MediaType.APPLICATION_JSON_VALUE) with @RequestBody

14
New cards

What library is used in Java clients to convert between JSON and Java objects?

Jackson, using ObjectMapper.

15
New cards

What Maven dependency do you need to add Jackson to your project?

<p></p>
16
New cards

What class is used to build HTTP requests in Java?

HttpRequest from java.net.http.

17
New cards

How do you deserialize a list of objects from JSON using Jackson?

knowt flashcard image
18
New cards

How do you send a POST request with JSON in the body?

Convert the object with objectMapper.writeValueAsString(obj) and use BodyPublishers.ofString(...).

19
New cards

What class lets you customize HTTP status codes and headers in Spring?

ResponseEntity<T>

20
New cards

How do you return a 200 OK response with data?

return ResponseEntity.ok(data);

21
New cards

How do you return a 404 Not Found response?

return ResponseEntity.notFound().build();

22
New cards

How do you return a 409 Conflict response?

return ResponseEntity.status(HttpStatus.CONFLICT).build();

23
New cards

How do you return a 204 No Content response?

return ResponseEntity.noContent().build();

24
New cards

Can you add headers to a ResponseEntity?

Yes, using .header("key", "value").

25
New cards

How do you check the HTTP status code of a response in Java?

response.statusCode()

26
New cards

What is Optional<T> used for in Java?

To represent a value that may or may not be present (instead of using null).

27
New cards

How do you create a non-empty Optional?

Optional.of(data)

28
New cards

How do you create an empty Optional?

Optional.empty()

29
New cards

How do you create an Optional that might be null?

Optional.ofNullable(data)

30
New cards

How do you use a lambda to process an Optional if it’s present?

myOptional.ifPresent(data -> doSomething(data));

31
New cards

How do you handle both present and empty cases in an Optional?

knowt flashcard image
32
New cards

Why not use Optional for everything (e.g. addStar)?

For binary success/failure cases, returning a boolean is clearer.

33
New cards

What is OpenAPI?

A standard for describing REST APIs in a machine-readable format.

34
New cards

What dependency do you need to add to enable OpenAPI in Spring Boot?

knowt flashcard image
35
New cards

What URL gives you the OpenAPI JSON spec for your API?

36
New cards

What URL shows the Swagger UI web interface?

37
New cards

What can Swagger UI be used for?

Browsing, documenting, and testing API endpoints interactively.

38
New cards

Can you import OpenAPI specs into Postman?

Yes — Postman supports OpenAPI v3 import.

39
New cards

Does SoapUI support OpenAPI v3?

No — only the paid version does; free version supports OpenAPI v2 only.