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

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

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&#39;m trying to update just a parameter of the object, by expecting this:

``` java
@PutMapping(&quot;{id}/nome&quot;)
public ResponseEntity&lt;?&gt; putDisciplinaNome(@PathVariable int id, @RequestBody String nome)

and sending the json like this:

{
    &quot;nome&quot;: &quot;Matematica&quot;
}

but the result is this:

&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.

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(&quot;{id}/nome&quot;)
public ResponseEntity&lt;?&gt; 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(&quot;{id}/nome&quot;)
public ResponseEntity&lt;?&gt; 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.

{
    &quot;nome&quot;: &quot;....&quot;
}

答案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);
    }
}

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

英文:

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

public ResponseEntity&lt;?&gt; 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&lt;&gt;(&quot;Json could not be formatted.&quot;, HttpStatus.BAD_REQUEST);
         }
        Disciplina d = disciplinaService.getDisciplinabyID(id);
        if (d == null) {
            return new ResponseEntity&lt;&gt;(d, HttpStatus.NOT_FOUND);
        } else {
              d.setNome(json2.get(&quot;nome&quot;).toString());
              return new ResponseEntity&lt;&gt;(d, HttpStatus.OK);
        }
}

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:

确定