将Http更新为HttpClient后出现错误400

时间:2020-06-04 16:08:00

标签: angular spring-boot angular-httpclient updating http-status-code-400

嗨,我正在将我的angular 2应用程序更新为angular 6,后来又更新为9,但是我的请求中出现错误400 / net :: ERR_CONNECTION_REFUSED 这是以前的代码在angular 2中使用

const mergeAuthToken = (options: RequestOptionsArgs) => {
    let newOptions = new RequestOptions({}).merge(options);
    let newHeaders = new Headers(newOptions.headers);
    newHeaders.set('Authorization','Bearer '+ localStorage.getItem('jwt'));
    newHeaders.set('accept', 'application/json');
    newHeaders.set('content-type', 'application/json');
    newOptions.headers = newHeaders;
    return newOptions;
};

@Injectable()
export class AuthHttpService {

    constructor(public http: Http) {
    }

    get(url: string, options?: RequestOptionsArgs): Observable<Response> {
        return this.http.get(url, mergeAuthToken(options));
    }

    post(url: string, body: string, options?: RequestOptionsArgs): Observable<Response> {
        return this.http.post(url, body, mergeAuthToken(options));
    }
}

我已将其更新为以下代码,但出现错误400则无法正常工作

const mergeHeaders = (Reqoptions : any) =>{
    let httpHeaders = new HttpHeaders()
            .set('Authorization','Bearer '+ localStorage.getItem('jwt'))
            .set('accept', 'application/json')
            .set('content-type', 'application/json');
    if(Reqoptions){
        httpHeaders.set('params' ,Reqoptions);
    }                
    console.log(httpHeaders);
    return httpHeaders;
}


@Injectable()
export class AuthHttpService {

    constructor(public http: HttpClient) {
    }

    get(url: string, options?: any): Observable<any> {
        return this.http.get(url, {headers :mergeHeaders(options) });
    }

    post(url: string, body: any, options?: any): Observable<any> {
        return this.http.post(url, JSON.stringify(body), {headers :mergeHeaders(options) });
    }
}

这就是我的称呼

findDecopagesByCriteria(decopageCriteria: DecopageCriteria): Observable<Array<Decopage>> {
        return this.authHttp.post(this.settings.server.url + `/decoupage/listByCriteria`, decopageCriteria)
            .pipe(map((res => res)));
    }

localStorage.getItem('jwt');返回null

这是我的被调用函数的Spring Boot代码:

    @RequestMapping(value = "/decoupage/listByCriteria", method = RequestMethod.POST)
    public @ResponseBody
    ResponseEntity<List<DecoupageDto>> getDecoupagesByCriteria(@RequestBody DecoupageCriteria decoupageCriteria) throws Exception {

        List<DecoupageDto> list = decoupageService.findDecoupagesByCriteria(decoupageCriteria);

        if (Utils.isNoEmpty(decoupageCriteria.getIncludes()) || Utils.isNoEmpty(decoupageCriteria.getExcludes()));
            list = CollectionUtils.emptyIfNull(list).stream().map(decoupage -> new DecoupageDto().mappedCustomDto(decoupage, decoupageCriteria.getIncludes(), decoupageCriteria.getExcludes())).collect(Collectors.toList());

        if (list == null || list.isEmpty())
            return new ResponseEntity<List<DecoupageDto>>(HttpStatus.NO_CONTENT);

        return new ResponseEntity<List<DecoupageDto>>(list, HttpStatus.OK);

    }

完整网络错误:

{
  "timestamp" : 1591289699672,
  "status" : 400,
  "error" : "Bad Request",
  "exception" : "org.springframework.http.converter.HttpMessageNotReadableException",
  "message" : "Could not read document: Can not construct instance of ma.enova.odt.dao.criteria.DecoupageCriteria: no String-argument constructor/factory method to deserialize from String value ('{\"libelleIndicateur\":\"\"}')\n at [Source: java.io.PushbackInputStream@256d246f; line: 1, column: 1]; nested exception is com.fasterxml.jackson.databind.JsonMappingException: Can not construct instance of ma.enova.odt.dao.criteria.DecoupageCriteria: no String-argument constructor/factory method to deserialize from String value ('{\"libelleIndicateur\":\"\"}')\n at [Source: java.io.PushbackInputStream@256d246f; line: 1, column: 1]",
  "path" : "/ODTBACK/decoupage/listByCriteria"
}

1 个答案:

答案 0 :(得分:0)

您是否也已将HttpClientModule导入到AppComponent中?