Q&A for Work
Setup a private space for you and your coworkers to ask questions and share information.
Learn more about Teams
I created a simple Spring Boot REST service to handle posting data from my HTML page. When I POST data everything is ok, but when I want to check data on @RequestMapping value href I see error message:
Whitelabel Error Page
This application has no explicit mapping for /error, so you are seeing this as a fallback.
Wed Nov 15 13:33:46 CET 2017
There was an unexpected error (type=Bad Request, status=400).
Required request body is missing: public org.springframework.http.ResponseEntity> pl.sample.atms.web.rest.QuestionRestController.getQuestions5(pl.sample.atms.dto.Question)
How Can I check and get sent data? My code:
$.ajax({
url: 'http://localhost:8080/api/questionsPost',
dataType: 'json',
type: 'post',
contentType: 'application/json',
data: JSON.stringify(scoreObj),
success: function( data, textStatus, jQxhr ){
console.log(data, textStatus, jQxhr)
error: function( jqXhr, textStatus, errorThrown ){
console.log( jqXhr, textStatus, errorThrown );
Rest controller:
package pl.sample.atms.web.rest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import pl.sample.atms.dao.QuestionRepository;
import pl.sample.atms.dto.Question;
import java.util.ArrayList;
import java.util.List;
@RestController
@RequestMapping(value = "/api", method = {RequestMethod.GET, RequestMethod.POST})
public class QuestionRestController {
@Autowired
private QuestionRepository questionRepository;
@PostMapping("/questionsPost")
public ResponseEntity<List<Question>> getQuestions5(@RequestBody Question myQuestion) {
System.out.println(ResponseEntity.ok(new ArrayList<>()));
System.out.println(myQuestion);
return ResponseEntity.ok(new ArrayList<>());
Question dto
package pl.sample.atms.dto;
import javax.persistence.Entity;
import javax.persistence.Id;
@Entity
public class Question {
private Long id;
private String title;
public Question() {
public Long getId() {
return id;
public void setId(Long id) {
this.id = id;
public String getTitle() {
return title;
public void setTitle(String title) {
this.title = title;
Question repository
package pl.sample.atms.dao;
import org.springframework.data.repository.CrudRepository;
import pl.sample.atms.dto.Question;
import java.util.List;
public interface QuestionRepository extends CrudRepository<Question, Long> {
List<Question> findByTitle(String title);
UPDATE
I changed Rest controller code:
package pl.sample.atms.web.rest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import pl.sample.atms.dao.QuestionRepository;
import pl.sample.atms.dao.UserInfoRepository;
import pl.sample.atms.dto.Question;
import pl.sample.atms.dto.QuestionAnswer;
import pl.sample.atms.dto.Response;
import pl.sample.atms.dto.UserInfo;
import java.util.ArrayList;
import java.util.List;
import static org.apache.commons.lang3.StringUtils.isBlank;
@RestController
@RequestMapping("/api")
public class QuestionRestController {
@Autowired
private QuestionRepository questionRepository;
@Autowired
private UserInfoRepository userInfoRepository;
private List<Question> question = new ArrayList<Question>();
@GetMapping(value = "/questionsGet")
public Response getResource() {
Response response = new Response("Done", question);
return response;
@PostMapping(value = "/questionsPost")
public Response postCustomer(@RequestBody Question oneQuestion) {
question.add(oneQuestion);
Response response = new Response("Done", oneQuestion);
return response;
And I created new class - response:
package pl.sample.atms.dto;
public class Response {
private String status;
private Object data;
public Response(){
public Response(String status, Object data){
this.status = status;
this.data = data;
public String getStatus() {
return status;
public void setStatus(String status) {
this.status = status;
public Object getData() {
return data;
public void setData(Object data) {
this.data = data;
Now I am able to see some sent data, but unfortunatelly it isn't generated and sent using JavaScript JSON, but this:
{"status":"Done","data":[{"id":null,"title":null}]}
Structure of JSON which I need:
"{"N":["4","4","5","4","2"],"E":["4","4","5","3","1"],"O":["3","3","4","2","3"],"U":["2","2","2","2","4"],"S":["3","2","2","3","3"]}"
I really don't know what I am doing wrong but I feel that I am close to solve this.
–
–
–
–
You're restricting your method to just POST requests when using the @PostMapping annotation.
This is equivalent to:
@RequestMapping(value = "/questionsPost", method = RequestMethod.POST)
If you want to accept both POST and GET, I would change it to:
@RequestMapping(value = "/questionsPost")
The value suggests a Post request -> so, personally I would adjust that to just "/questions".
UPDATE
Per your comment on what you're attempting to accomplish, I would highly recommend having two separate methods. Spring will know which method to call based on if you're posting data, or attempting to get data. And then adjust your business logic accordingly.
one method for Post:
@RequestMapping(value = "/questions", method = RequestMethod.POST)
one method for Get:
@RequestMapping(value = "/questions", method = RequestMethod.GET)
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
site design / logo © 2019 Stack Exchange Inc; user contributions licensed under cc by-sa 3.0
with attribution required.
rev 2019.8.28.34724