Make a sample Java service which takes the following json from an endpoint as input, parses it and puts it into the database.
Input: The input will have a fixed format which will be posted as a content body to the endpoint.
Output: The output should be a number of rows that are filled in the database or error in case of failures.
Sample input :
{
"quiz": {
"sport": {
"q1": {
"question": "Which one is correct team name in NBA?",
"options": [
"New York Bulls",
"Los Angeles Kings",
"Golden State Warriros",
"Huston Rocket"
],
"answer": "Huston Rocket"
}
},
"maths": {
"q1": {
"question": "5 + 7 = ?",
"options": [
"10",
"11",
"12",
"13"
],
"answer": "12"
},
"q2": {
"question": "12 - 8 = ?",
"options": [
"1",
"2",
"3",
"4"
],
"answer": "4"
}
}
}
}
I've created a basic Rest service with springboot that takes in question, options and answer as input, and also displays it in the same way.
Ex. input :
{
"question": "khis is the question part",
"options": [
"option 1",
"option 2",
"option 3",
"option 4"
],
"answer": "option 1"
}
I have created a springboot project and created the following java classes: 1. QuestionSet.java
package com.example.demo.questionset;
import java.io.Serializable;
import com.fasterxml.jackson.annotation.JsonRootName;
public class QuestionSet {
private String question;
private String options[];
private String answer;
public QuestionSet() {
}
public QuestionSet(String question, String[] options, String answer) {
super();
this.question = question;
this.options = options;
this.answer = answer;
}
public String getQuestion() {
return question;
}
public String[] getOptions() {
return options;
}
public String getAnswer() {
return answer;
}
public void setQuestion(String question) {
this.question = question;
}
public void setOptions(String[] options) {
this.options = options;
}
public void setAnswer(String answer) {
this.answer = answer;
}
}
`
QuestionSetService.java
package com.example.demo.questionset; import java.util.ArrayList; import java.util.Arrays; import java.util.List; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.stereotype.Service; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; import com.fasterxml.jackson.annotation.JsonProperty; import com.fasterxml.jackson.databind.ObjectWriter; @Service public class QuestionSetService { String nameString[] = {"kabir", "ram", "shyam", "varun"};
QuestionSetController.java
package com.example.demo.questionset;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import com.fasterxml.jackson.annotation.JsonGetter;
@RestController
public class QuestionSetController {
@Autowired
QuestionSetService questionSetService;
@RequestMapping("/questionsets")
public List<QuestionSet> getAllQuestionSets(){
return questionSetService.getAllQuestionSets();
}
@RequestMapping("/questionsets/{question}")
public QuestionSet getQuestionSetByQuestion(@PathVariable String question)
{
return questionSetService.getQuestionSetByQuestion(question);
}
@PostMapping("/questionsets")
public void postController(
@RequestBody QuestionSet questionSet) {
questionSetService.addQuestionSet(questionSet);
}
}
private List<QuestionSet> questionSets = null;
public List<QuestionSet> getAllQuestionSets() {
return questionSets;
}
public QuestionSet getQuestionSetByQuestion(String question) {
return questionSets.stream().filter(t ->
t.getQuestion().equals(question)).findFirst().get();
}
public ResponseEntity addQuestionSet( QuestionSet questionSet) {
questionSets.add(questionSet);
return ResponseEntity.ok(HttpStatus.OK);
}
}
I can take in simple json input in the form of questionset but the required json input is much more complex. I understand that I need to make wrapper classes and use objectmapper, but I don't know how.