@RequestBody & @ResponseBody — Concept Check
Questions on @RequestBody & @ResponseBody. @RequestBody deserialises the HTTP body into a Java object using Jackson; @ResponseBody serialises the return value back to JSON or XML.
Questions in this quiz
8 questions on @RequestBody & @ResponseBody — Concept Check. Work through them here, then start the quiz to answer them and see your score.
1.A developer uses @RequestBody without @Valid in a Spring Boot application. What happens if the incoming JSON has invalid data types?
- AThe application throws a MethodArgumentNotValidException.
- BThe application ignores the invalid fields and continues.
- CThe application processes the request with default values.
- DThe application throws a HttpMessageNotReadableException.
2.You notice that extra fields in incoming JSON requests are causing errors. How can you configure Jackson to ignore them?
- AConfigure ObjectMapper to throw exceptions on unknown fields.
- BSet spring.jackson.deserialization.fail-on-unknown-properties=false.
- CEnable MapperFeature.ACCEPT_CASE_INSENSITIVE_ENUMS.
- DUse @JsonIgnore on all unknown fields.
3.A service returns a large dataset as a JSON response, causing memory issues. What should you do to handle this efficiently?
- AUse @ResponseBody to automatically manage large responses.
- BIncrease server memory to handle large responses.
- CUse ResponseBodyEmitter to stream the response.
- DLoad the entire dataset into memory before sending.
4.A developer uses @ResponseBody in a @RestController method. What is the effect of this annotation?
- AIt bypasses HTTP response headers.
- BIt serializes the return value to JSON or XML.
- CIt deserializes the request body to a Java object.
- DIt triggers Bean Validation on the return value.
5.You add @Valid to a @RequestBody parameter in a Spring controller. What happens if validation fails?
- AThe response status is set to 500 Internal Server Error.
- BThe request is processed, but invalid fields are set to null.
- CThe method executes with default values for invalid fields.
- DMethodArgumentNotValidException is thrown before the method executes.
6.A developer configures Jackson to skip null fields in JSON output. What property should they set?
- Aspring.jackson.serialization.indent-output=false
- Bspring.jackson.deserialization.fail-on-unknown-properties=true
- Cspring.jackson.default-property-inclusion=non_null
- Dspring.jackson.date-format=yyyy-MM-dd
7.A method in a Spring Boot application uses @ResponseBody but returns XML instead of JSON. What could be the reason?
- AJackson is not configured to handle JSON.
- BThe client requested XML via Accept header.
- CThe method must explicitly specify JSON format.
- D@ResponseBody only supports XML by default.
8.A developer wants to customize date formats in JSON output. How can they achieve this in Spring Boot?
- AEnable SerializationFeature.WRITE_DATES_AS_TIMESTAMPS.
- BUse @JsonIgnore on date fields.
- CConfigure Jackson to use default date formats.
- DSet spring.jackson.date-format in application.properties.