如何使用spring和angular 4为POST配置有效的@ResponseBody

时间:2018-02-21 13:17:11

标签: spring angular typescript spring-webflux

我收到了这个错误:

  

core.js:1448 ERROR SyntaxError:JSON输入的意外结束

我认为错误在于回复,我如何正确创建@responseBody 让前方了解后端的对象是否正确创建 输入是(license:"gd345")没有括号

组件文件:

import { Component, OnInit } from '@angular/core';
import { Http, Headers } from '@angular/http';
@Component({
  selector: 'posts',
  templateUrl: './posts.component.html',
  styleUrls: ['./posts.component.css']
})
export class PostsComponent {

  posts: any[];
  letter: any;

  constructor(private http: Http) {
    http.get('http://localhost:8183/api/vehiculespark')
      .subscribe(response => {
        this.posts = response.json();

      });
  }

  createPost(input: HTMLInputElement) {
    debugger;
    let post = { license: input.value };
    this.letter = post;
    const headers = new Headers({ 'Content-Type': 'application/json' });
    this.http.post('http://localhost:8183/api/vehicules', this.letter, { headers: headers })
      .subscribe(response => {

        console.log(response.json())
      });
    debugger;
  }


}

spring Controller:

package com.api.kingspark.parking.controller;

import com.api.kingspark.parking.domain.Tickect;
import com.api.kingspark.parking.domain.Vehicule;
import com.api.kingspark.parking.repositories.TicketRepository;
import com.api.kingspark.parking.repositories.VehiculoRepository;
import com.api.kingspark.parking.services.PorteroServices;
import org.reactivestreams.Publisher;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

import java.util.List;

import static org.springframework.http.MediaType.APPLICATION_JSON_VALUE;
import static org.springframework.http.MediaType.APPLICATION_XML_VALUE;

@RestController
public class ParkingController {


    private PorteroServices porteroServices;
    private final VehiculoRepository vehiculoRepository;
    private  final TicketRepository ticketRepository;


    public ParkingController(PorteroServices porteroServices, VehiculoRepository vehiculoRepository, TicketRepository ticketRepository) {

        this.porteroServices = porteroServices;
        this.vehiculoRepository = vehiculoRepository;
        this.ticketRepository = ticketRepository;
    }
    @CrossOrigin
    @GetMapping("/api/vehiculespark")
    List<Tickect> listIn(){
        return porteroServices.getInVehicules();
    }

    @GetMapping("/api/vehicules")
    Flux<Vehicule> listAll(){
        return vehiculoRepository.findAll();
    }

    @GetMapping("/api/vehicules/{id}")
    Mono<Vehicule> getById(@PathVariable String id){
        return vehiculoRepository.findById(id);
    }

    @CrossOrigin
    @ResponseStatus(HttpStatus.CREATED)
    @PostMapping(value = "/api/vehicules",consumes="application/json",produces={"application/json","application/xml"})
    @ResponseBody
    Mono<Void> createVehicule(@RequestBody Publisher<Vehicule> carstream){
        Vehicule ca= Mono.fromDirect(carstream).cache().block();
        return porteroServices.saveVehicule(ca);
        //return ResponseEntity.created(location).build();
    }

    @PutMapping("/api/vehicules/{id}")
    Mono<Tickect> update(@PathVariable String id, @RequestBody Vehicule vehicule){
        return  ticketRepository.save(porteroServices.drawVehicule(id));
    }
}

0 个答案:

没有答案