英文:
How to send just one parameter in put method?
问题
我正在尝试仅更新对象的一个参数,期望如下所示:
``` java
@PutMapping("{id}/nome")
public ResponseEntity<?> putDisciplinaNome(@PathVariable int id, @RequestBody String nome)
并且发送类似以下的 JSON 数据:
{
"nome": "Matematica"
}
但结果却是:
"nome": "{\"nome\":\"Matematica\"}"
我在这里做错了什么?还有其他方法可以发送这个参数吗?
<details>
<summary>英文:</summary>
I'm trying to update just a parameter of the object, by expecting this:
``` java
@PutMapping("{id}/nome")
public ResponseEntity<?> putDisciplinaNome(@PathVariable int id, @RequestBody String nome)
and sending the json like this:
{
"nome": "Matematica"
}
but the result is this:
"nome": "{\"nome\":\"Matematica\"}"
what am i doing wrong here? there's another way of sending this parameter?
答案1
得分: 1
Create a class for request body contains one field and use class as request body.
class DisDto {
String nome;
}
@PutMapping("/{id}/nome")
public ResponseEntity<?> putDisciplinaNome(@PathVariable int id, @RequestBody DisDto disDto) {
}
英文:
Create a class for request body contains one field and use class as request body.
class DisDto {
String nome;
}
@PutMapping("{id}/nome")
public ResponseEntity<?> putDisciplinaNome(@PathVariable int id, @RequestBody DisDto disDto) {
}
答案2
得分: 0
尝试将您的身体包裹在类似以下的输入类型中。
public class InputNome {
String nome;
//...
}
@PutMapping("{id}/nome")
public ResponseEntity<?> putDisciplinaNome(@PathVariable int id, @RequestBody InputNome body) {
System.out.println(body.nome);
}
这样,当您发送请求时,请求体(假设为 JSON)将类似于以下内容。
{
"nome": "...."
}
英文:
Trying wrapping you're body in an input type like this.
public class InputNome {
String nome;
//...
}
@PutMapping("{id}/nome")
public ResponseEntity<?> putDisciplinaNome(@PathVariable int id, @RequestBody InputNome body) {
System.out.println(body.nome);
}
This way when you're sending the request the body (assuming JSON) will be something like this.
{
"nome": "...."
}
答案3
得分: 0
我想继续以 JSON 格式发送,所以我将字符串转换为 JSONObject,像这样:
public ResponseEntity<?> putDisciplinaNome(@PathVariable int id, @RequestBody String json) {
JSONParser parser = new JSONParser();
JSONObject json2 = null;
try {
json2 = (JSONObject) parser.parse(json);
} catch (ParseException e) {
return new ResponseEntity<>("无法格式化 JSON。", HttpStatus.BAD_REQUEST);
}
Disciplina d = disciplinaService.getDisciplinabyID(id);
if (d == null) {
return new ResponseEntity<>(d, HttpStatus.NOT_FOUND);
} else {
d.setNome(json2.get("nome").toString());
return new ResponseEntity<>(d, HttpStatus.OK);
}
}
现在可以正常工作
英文:
I wanted to keep sending as json, so i cast the string to JSONObject like this:
public ResponseEntity<?> putDisciplinaNome(@PathVariable int id, @RequestBody String json) {
JSONParser parser = new JSONParser();
JSONObject json2 = null;
try {
json2 = (JSONObject) parser.parse(json);
} catch (ParseException e) {
return new ResponseEntity<>("Json could not be formatted.", HttpStatus.BAD_REQUEST);
}
Disciplina d = disciplinaService.getDisciplinabyID(id);
if (d == null) {
return new ResponseEntity<>(d, HttpStatus.NOT_FOUND);
} else {
d.setNome(json2.get("nome").toString());
return new ResponseEntity<>(d, HttpStatus.OK);
}
}
and now is working
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论