gpt4 book ai didi

java - Spring Boot - 出现意外错误(类型=错误请求,状态=400)。缺少必需的请求正文

转载 作者:行者123 更新时间:2023-11-30 06:25:15 25 4
gpt4 key购买 nike

我创建了一个简单的 Spring Boot REST 服务来处理来自 HTML 页面的发布数据。当我发布数据时一切正常,但是当我想检查 @RequestMapping value href 上的数据时,我看到错误消息:

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)

如何查看并获取发送的数据?我的代码:

JS:

$.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 );
}
});

休息 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<>());
}

}

问题dto

package pl.sample.atms.dto;

import javax.persistence.Entity;
import javax.persistence.Id;

@Entity
public class Question {

@Id
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;
}
}

问题库

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);
}

更新

我更改了 Rest Controller 代码:

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;
}

}

我创建了新类(class) - 响应:

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;
}
}

现在我可以看到一些发送的数据,但不幸的是它不是使用 JavaScript JSON 生成和发送的,但是:

{"status":"Done","data":[{"id":null,"title":null}]}

我需要的JSON结构:

"{"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"]}"

我真的不知道我做错了什么,但我觉得我已经接近解决这个问题了。

最佳答案

使用 @PostMapping 注释时,您将方法限制为仅 POST 请求。

这相当于:

@RequestMapping(value = "/questionsPost", method = RequestMethod.POST)

如果你想同时接受POST和GET,我会将其更改为:

@RequestMapping(value = "/questionsPost")

该值表明一个 Post 请求 -> 因此,我个人会将其调整为“/questions”。

更新根据您对要完成的任务的评论,我强烈建议您使用两种单独的方法。如果您正在发布数据或尝试获取数据,Spring 将知道要调用哪个方法。然后相应地调整您的业务逻辑。

一种发布方法:

@RequestMapping(value = "/questions", method = RequestMethod.POST)

一种获取方法:

@RequestMapping(value = "/questions", method = RequestMethod.GET)

关于java - Spring Boot - 出现意外错误(类型=错误请求,状态=400)。缺少必需的请求正文,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47307906/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com