JSocketをTomcatサーバーに正しく送信しているように見えるAngularJS Webアプリケーションがありますが、サーバーはnull値を受け取ります。 という表記があるので、私のプロパティ名は既に小文字で、this questionは役に立たなかったので、This questionは役に立ちませんでした。 編集:コメントに指摘されているように、私は実際にこの記法を持っていません。私はそれを間違って読んでいた。ここでSpring POSTがnull値を持つオブジェクトを受け取ります
は、問題のサーバーメソッドです:
var uri = 'http://localhost:8080/catalog/api/kind/';
function create(name) {
var deferred = $q.defer();
var data = {
'name': name
};
$http({
method: 'POST',
url: uri + 'add',
data: angular.toJson(data),
headers: {
'Content-Type': 'application/json; charset=UTF-8'
}
}).then(function(response) {
deferred.resolve(response.data);
}).catch(function(error) {
console.error('Error while adding kind');
deferred.reject(error);
});
return deferred.promise;
}
:ここ
@Entity
public class Kind {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private int id;
private String name;
@OneToMany(mappedBy="kind")
private Set<Card> cards;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Set<Card> getCards() {
return cards;
}
public void setCards(Set<Card> cards) {
this.cards = cards;
}
}
が要求を行う方法であって、ここで
@PostMapping(path="/kind/add")
public @ResponseBody String addNewKind(Kind kind) throws Exception {
if (kind.getName() == null) {
throw new Exception("Name not found.");
}
kindRepository.save(kind);
return "Saved";
}
それが受信することを期待種類のオブジェクトがあります
これはサーバーに送信されたJSONです。
{"name":"Something"}
N.B.私はid
またはcards
パラメータを送信せずに離れることができると仮定しました。これはid
が自動生成され、cards
が空である可能性があるためです。以下のJSONを送信するときしかし、私は同じヌル結果を得る:
Accept: application/json, text/plain, */*
Accept-Encoding: gzip, deflate
Accept-Language: en-US,en;q=0.5
Connection: keep-alive
Content-Length: 20
Content-Type: application/json; charset=UTF-8
Cookie: __distillery=08c4ef1_752470a4-…ID=lr9remoqoi0ht13v1urq5lq8r7
Host: localhost:8080
Referer: http://localhost:8080/catalog/
User-Agent: Mozilla/5.0 (Windows NT 10.0; …) Gecko/20100101 Firefox/57.0
そして、これは私がデバッグでkind
オブジェクト引数を表示したときに、サーバーが受け取るものです:
{"id":0,"name":"Something","cards":[]}
これらは、リクエストヘッダですモード:
kind= Kind
cards= null
id= 0
name= null
_ "私はすでに@RequestBody表記を持っているので" _ - どこ?私は '@ ResponseBody'しか見ることができません。 – helospark
@helosparkあなたは正しいです、どのように恥ずかしい!私は '@ RequestBody'を見て、' @ ResponseBody'を読んでいました。 – catholiccomposer