简体   繁体   English

Java Spring REST控制器:请求主体不完整

[英]Java spring REST controller: incomplete request body

I have a problem with my REST Controller. 我的REST控制器有问题。 If I sent a request with a RequestBody (json) some attributes doesn't arrive the controller, although they was sent and defined at model. 如果我发送带有RequestBody(json)的请求,则某些属性不会到达控制器,尽管它们是在模型上发送和定义的。

I could find out that it look like an old version of files will be used from the local java web server. 我发现本地Java Web服务器将使用旧版本的文件。 As I changed the System.out.println Value at Constructor still the old value was outputed. 当我在构造函数中更改System.out.println值时,仍输出旧值。

public RestController_ApiKey_2_0() {
    System.out.println("RestController_ApiKey_2_0 init");
}

I tried the following things bootless: 我尝试了以下无引导的操作:

  1. deleted java web server and did a new installation 删除了Java Web服务器并进行了新安装
  2. cleaned the project and started server again 清理项目并再次启动服务器
  3. clean install of project 干净安装项目

Does anyone have an idea? 有人有主意吗?

Please provide more code, how do you declare a controller, and what params it can take. 请提供更多代码,如何声明控制器,以及可以使用哪些参数。 Also show a sample request. 同时显示示例请求。 Here is an example of a simple controller: 这是一个简单控制器的示例:

A model 一个模型

public class CustomRequestBody {
    private String fieldA;
    private String fieldB;

    public String getFieldA() {
        return fieldA;
    }

    public void setFieldA(final String fieldA) {
        this.fieldA = fieldA;
    }

    public String getFieldB() {
        return fieldB;
    }

    public void setFieldB(final String fieldB) {
        this.fieldB = fieldB;
    }
}

Controller: 控制器:

@Controller
public class MyController {
    @RequestMapping(value = "/some-path", method = RequestMethod.POST)
    @ResponseStatus(HttpStatus.ACCEPTED)
    public ResponseEntity handleSomePath(@RequestBody final CustomRequestBody body, final HttpServletRequest request) {
        // Do the job.
    }  

And request will be: 要求将是:

HTTP POST http://some.server.com/some-path
{
    "fieldA":"first value",
    "fieldB":"second value"
} 

Read more at Spring documentation here 此处阅读更多Spring文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM