HttpErrorResponse&Spring Bad Request:缺少必需的请求主体

时间:2018-12-17 04:28:18

标签: javascript java angular http spring-mvc

我遇到了一个错误的请求。

服务器端错误:

Required request body is missing error

客户端错误:

HttpErrorResponse {headers: HttpHeaders, status: 400, statusText: "Bad Request",
                   url: "http://localhost:9000/api/create-risk", ok: false, …}

我已经使用内置的swagger组件测试了API,并收到201响应,其中包含正在创建的正确对象和预期响应。我感觉这是我的疏忽,但是我已经看了几个小时的代码了,似乎无法找到断开连接。任何帮助将不胜感激。

API:

    @PostMapping(value = "/create-risk", produces = 
    MediaType.APPLICATION_JSON_VALUE)
    public Risk createRisk(
    @RequestBody Risk risk
) {
    log.debug("REST request to save Risk : {}", risk);
    Risk createdRisk = new Risk();
    createdRisk.setPocFirstName(risk.getPocFirstName());
    riskManagementRepository.save(createdRisk);
    return createdRisk;
}

Angular RiskModel

export interface IRiskModel {
id?: any;
login?: string;
firstName?: string;
lastName?: string;
email?: string;
activated?: boolean;
langKey?: string;
authorities?: any[];
createdBy?: string;
createdDate?: Date;
lastModifiedBy?: string;
lastModifiedDate?: Date;
password?: string;

}

export class RiskModel implements IRiskModel {
constructor(
    public id?: any,
    public firstName?: string,
) {
    this.id = id ? id : null;
    this.firstName = firstName ? firstName : null;
}

}

风险组件HTML

<div class="jumbotron">
<div class="container">
    <div class="row">
        <div class="col-md-6 offset-md-3">
            <form name="form">
                <div class="form-group">
                    <input type="text" [(ngModel)]="risk.firstName" placeholder="First Name" name="firstName" class="form-control" id="firstName">
                </div>
                <div>
                    <button (click)="saveRisk()" class="btn btn-success">Save</button>
                </div>
            </form>
        </div>
    </div>
</div>

创建风险TS

import { Component, OnInit } from '@angular/core';
import { ActivatedRoute } from '@angular/router';
import {RiskModel,RiskManagementService} from 'app/core';


@Component({
selector: 'jhi-create-risk',
templateUrl: './create_risk.component.html',
styleUrls: ['create_risk.scss']
})
export class CreateRiskComponent implements OnInit {
risk: RiskModel;
inProduction: boolean;
swaggerEnabled: boolean;

constructor(
    private riskManagementService: RiskManagementService,
    private route: ActivatedRoute) {}

ngOnInit() {
    this.route.data.subscribe(({ risk }) => {
        this.risk = risk.body ? risk.body : risk});
};

saveRisk() {
    this.riskManagementService.createRisk(this.risk).subscribe(response 
=> response);
}

}

风险服务TS

import { Injectable } from '@angular/core';
import { HttpClient, HttpResponse } from '@angular/common/http';
import { Observable } from 'rxjs';
import { SERVER_API_URL } from 'app/app.constants';
import {IRiskModel} from 'src/main/webapp/app/core';

@Injectable({ providedIn: 'root' })
export class RiskManagementService {
public resourceUrl = SERVER_API_URL + 'api/create-risk';

constructor(private http: HttpClient) {}

createRisk(risk: IRiskModel): Observable<HttpResponse<IRiskModel>> {
    return this.http.post<IRiskModel>(this.resourceUrl, risk, {observe: 
    'response'});
}

}

0 个答案:

没有答案