如何在PUT方法中只发送一个参数?

huangapple go评论100阅读模式
英文:

How to send just one parameter in put method?

问题

  1. 我正在尝试仅更新对象的一个参数期望如下所示
  2. ``` java
  3. @PutMapping("{id}/nome")
  4. public ResponseEntity<?> putDisciplinaNome(@PathVariable int id, @RequestBody String nome)

并且发送类似以下的 JSON 数据:

  1. {
  2. "nome": "Matematica"
  3. }

但结果却是:

  1. "nome": "{\"nome\":\"Matematica\"}"

我在这里做错了什么?还有其他方法可以发送这个参数吗?

  1. <details>
  2. <summary>英文:</summary>
  3. I&#39;m trying to update just a parameter of the object, by expecting this:
  4. ``` java
  5. @PutMapping(&quot;{id}/nome&quot;)
  6. public ResponseEntity&lt;?&gt; putDisciplinaNome(@PathVariable int id, @RequestBody String nome)

and sending the json like this:

  1. {
  2. &quot;nome&quot;: &quot;Matematica&quot;
  3. }

but the result is this:

  1. &quot;nome&quot;: &quot;{\&quot;nome\&quot;:\&quot;Matematica\&quot;}&quot;

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.

  1. class DisDto {
  2. String nome;
  3. }
  1. @PutMapping("/{id}/nome")
  2. public ResponseEntity<?> putDisciplinaNome(@PathVariable int id, @RequestBody DisDto disDto) {
  3. }
英文:

Create a class for request body contains one field and use class as request body.

  1. class DisDto {
  2. String nome;
  3. }
  1. @PutMapping(&quot;{id}/nome&quot;)
  2. public ResponseEntity&lt;?&gt; putDisciplinaNome(@PathVariable int id, @RequestBody DisDto disDto) {
  3. }

答案2

得分: 0

尝试将您的身体包裹在类似以下的输入类型中。

  1. public class InputNome {
  2. String nome;
  3. //...
  4. }
  5. @PutMapping("{id}/nome")
  6. public ResponseEntity<?> putDisciplinaNome(@PathVariable int id, @RequestBody InputNome body) {
  7. System.out.println(body.nome);
  8. }

这样,当您发送请求时,请求体(假设为 JSON)将类似于以下内容。

  1. {
  2. "nome": "...."
  3. }
英文:

Trying wrapping you're body in an input type like this.

  1. public class InputNome {
  2. String nome;
  3. //...
  4. }
  5. @PutMapping(&quot;{id}/nome&quot;)
  6. public ResponseEntity&lt;?&gt; putDisciplinaNome(@PathVariable int id, @RequestBody InputNome body) {
  7. System.out.println(body.nome);
  8. }

This way when you're sending the request the body (assuming JSON) will be something like this.

  1. {
  2. &quot;nome&quot;: &quot;....&quot;
  3. }

答案3

得分: 0

我想继续以 JSON 格式发送,所以我将字符串转换为 JSONObject,像这样:

  1. public ResponseEntity<?> putDisciplinaNome(@PathVariable int id, @RequestBody String json) {
  2. JSONParser parser = new JSONParser();
  3. JSONObject json2 = null;
  4. try {
  5. json2 = (JSONObject) parser.parse(json);
  6. } catch (ParseException e) {
  7. return new ResponseEntity<>("无法格式化 JSON。", HttpStatus.BAD_REQUEST);
  8. }
  9. Disciplina d = disciplinaService.getDisciplinabyID(id);
  10. if (d == null) {
  11. return new ResponseEntity<>(d, HttpStatus.NOT_FOUND);
  12. } else {
  13. d.setNome(json2.get("nome").toString());
  14. return new ResponseEntity<>(d, HttpStatus.OK);
  15. }
  16. }

现在可以正常工作 如何在PUT方法中只发送一个参数?

英文:

I wanted to keep sending as json, so i cast the string to JSONObject like this:

  1. public ResponseEntity&lt;?&gt; putDisciplinaNome(@PathVariable int id, @RequestBody String json) {
  2. JSONParser parser = new JSONParser();
  3. JSONObject json2 = null;
  4. try {
  5. json2 = (JSONObject) parser.parse(json);
  6. } catch (ParseException e) {
  7. return new ResponseEntity&lt;&gt;(&quot;Json could not be formatted.&quot;, HttpStatus.BAD_REQUEST);
  8. }
  9. Disciplina d = disciplinaService.getDisciplinabyID(id);
  10. if (d == null) {
  11. return new ResponseEntity&lt;&gt;(d, HttpStatus.NOT_FOUND);
  12. } else {
  13. d.setNome(json2.get(&quot;nome&quot;).toString());
  14. return new ResponseEntity&lt;&gt;(d, HttpStatus.OK);
  15. }
  16. }

and now is working 如何在PUT方法中只发送一个参数?

huangapple
  • 本文由 发表于 2020年6月29日 04:14:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/62627753.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定