JokeController.java
package edu.ucsb.cs156.spring.backenddemo.controllers;
import edu.ucsb.cs156.spring.backenddemo.services.JokeQueryService;
import lombok.extern.slf4j.Slf4j;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
@Tag(name="Jokes info from jokeapi.dev")
@Slf4j
@RestController
@RequestMapping("/api/jokes")
public class JokeController {
ObjectMapper mapper = new ObjectMapper();
@Autowired
JokeQueryService jokeQueryService;
@Operation(summary="Get number of jokes based on a certain category", description ="Joke data uploaded by jokeapi.dev")
@GetMapping("/get")
public ResponseEntity<String> getJokes(
@Parameter(name="category", description="category of the joke", example="Dark") @RequestParam String category,
@Parameter(name="numJokes", description="the number of jokes to return", example="4") @RequestParam int numJokes
) throws JsonProcessingException {
log.info("getJokes: category={} numJokes={}", category, numJokes);
String result = jokeQueryService.getJSON(category, numJokes);
return ResponseEntity.ok().body(result);
}
}