在通过ajax(Spring)传递的json中获取null

问题描述 投票:0回答:1

我在客户端创建一个“音乐家”对象并使用 AJAX 将其发送到服务器:

$("#add_musician_button").click(function() {
    var musician = {
        name: $("#musician_name").val(),
        photo: $("#musician_photo").val(),
        startYear: $("#start_year").val(),
        stopYear: $("#stop_year").val(),
        genres: genresList,
        countries: countriesList, 
        type: $("#musician_type_select").val(),
        frontman: $("#band_frontmen").val(),
        fullName: $("#solo_full_name").val(),
        birthYear: $("#solo_birth_year").val(),
        deathYear: $("#solo_death_year").val(),
        description: $("#description").val(),
        history: $("#history").val()
    }

    console.log(musician);

    $.ajax({
    type: "POST",
    url: "/save_musician",
    data: JSON.stringify(musician),
    contentType: "application/json",
    success: function(response) {
        alert('Musician "' + response.name + '" successfully created!');
    },
    error: function(error) {
        alert("An error occurred while creating the musician!")
    }
    });      

在服务器上,我接受。为此,我创建了一个特殊的类“musicianJson”,在其中重复 JSON 对象。

class MusicianJson {
    String name;
    String photo;
    String startYear;
    String stopYear;
    Set<String> genres;
    Set<String> countries;
    String type;
    String frontman;
    String fullName;
    String birthYear;
    String deathYear;
    String description;
    String history;
}

@RestController
public class AddMusicianRestController {

    @PostMapping("/save_musician")
    public ResponseEntity<String> AddMusician(@RequestBody MusicianJson musicianJson) {
        System.out.println("Musician name = " + musicianJson.name);
        return ResponseEntity.ok("Musician received");
    };
}

为什么我们需要“musicianJson”类?这是因为音乐家实体与“国家”和“流派”等实体相关联,因此我必须首先获取这些对象(根据用户输入)并将它们添加到新的“音乐家”对象中。这就是我写不下去的原因

public Musicians AddMusician(@RequestBody Musician musician)
因此,在服务器控制台中我收到消息:
Musician name = null
。为什么?

json ajax spring spring-boot spring-mvc
1个回答
0
投票

解决方案 - 为 MusicianJson 类添加 getter 和 setter:

class MusicianJson {
    String name;
    String photo;
    String startYear;
    String stopYear;
    Set<String> genres;
    Set<String> countries;
    String type;
    String frontman;
    String fullName;
    String birthYear;
    String deathYear;
    String description;
    String history;

    public String getName() {
        return name;
    }
    public void setName( String name ) {
        this.name = name;
    }
    public String getPhoto() {
        return photo;
    }
    public void setPhoto( String photo) {
        this.photo = photo;
    }
    public String getStartYear() {
        return  startYear;
    }
    public void setStartYear( String startYear ) {
        this.startYear = startYear;
    }
    public String getStopYear() {
        return stopYear;
    }
    public void setStopYear() {
        this.stopYear = stopYear;
    }
    public Set<String> getGenres() {
        return genres;
    }
    public void setGenres( Set<String> genres ) {
        this.genres = genres;
    }
    public Set<String> getCountries() {
        return countries;
    }
    public void setCountries ( Set<String> countries )
    {
        this.countries = countries;
    }
    public String getType() {
        return type;
    }
    public void setType( String type ) {
        this.type = type;
    }
    public String getFrontman() {
        return frontman;
    }
    public void setFrontman( String frontman ) {
        this.frontman = frontman;
    }
    public String getFullName() {
        return fullName;
    }
    public void setFullName( String fullName ) {
        this.fullName = fullName;
    }
    public String getBirthYear() {
        return birthYear;
    }
    public void setBirthYear( String birthYear ) {
        this.birthYear = birthYear;
    }
    public String getDeathYear() {
        return deathYear;
    }
    public void setDeathYear( String deathYear ) {
        this.deathYear = deathYear;
    }
    public String getDescription() {
        return description;
    }
    public void setDescription( String description ) {
        this.description = description;
    }
    public String getHistory() {
        return history;
    }
    public void setHistory( String history ) {
        this.history = history;
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.