Spring:只有一个元素的request-payload

时间:2022-11-29 03:34:44

I want implement a web-service which consumes only one named parameter in request-payload. In curl view it should be smth like: curl -X PATCH myurl.net/my_service -d "{mySingleParameter: 49}"

我想实现一个web服务,它在request-payload中只使用一个命名参数。在curl视图中它应该像sml一样:curl -X PATCH myurl.net/my_service -d“{mySingleParameter:49}”

I'm trying to do it with Spring, but I wondered that to map such payload to my method I must to declare a new class. Like:

我正在尝试使用Spring,但我想知道要将此类有效负载映射到我的方法,我必须声明一个新类。喜欢:

...
public static class PayloadWithSingleParamMSP{
  public Long mySingleParameter;
}

@RequestMapping(value = "my_service", method = RequestMethod.PATCH)
public String myService(@RequestBody PayloadWithSingleParamMSP payload){
  Long valueWhichIReallyNeed = payload.mySingleParameter;
  //do job
  ...
}
...

But is there a way to take value which I really need (mySingleParameter) directly?

但有没有办法直接获取我真正需要的价值(mySingleParameter)?

1 个解决方案

#1


2  

You have couple options:

你有几个选择:

    @RequestMapping(value = "my_service", method = RequestMethod.PATCH)
    public String myService(@RequestBody ObjectNode payload){
        Long valueWhichIReallyNeed = payload.get("mySingleParameter").asLong();
        //do job
       ...
    }

or

@RequestMapping(value = "my_service", method = RequestMethod.PATCH)
public String myService(@RequestBody Map<String, String> payload){
    Long valueWhichIReallyNeed = Long.parseLong(payload.get("mySingleParameter"));
    //do job
    ...
}

or even

@RequestMapping(value = "my_service", method = RequestMethod.PATCH)
public String myService(@RequestBody  Long mySingleParameter){
    Long valueWhichIReallyNeed = mySingleParameter;
    //do job
    //  ...
}

but in this last case your curl will look following:

但在最后一种情况下,你的卷曲将如下所示:

curl -X PATCH myurl.net/my_service -d "49" 

In answers for this question you can find more options: Passing multiple variables in @RequestBody to a Spring MVC controller using Ajax

在这个问题的答案中,您可以找到更多选项:使用Ajax将@RequestBody中的多个变量传递给Spring MVC控制器

#1


2  

You have couple options:

你有几个选择:

    @RequestMapping(value = "my_service", method = RequestMethod.PATCH)
    public String myService(@RequestBody ObjectNode payload){
        Long valueWhichIReallyNeed = payload.get("mySingleParameter").asLong();
        //do job
       ...
    }

or

@RequestMapping(value = "my_service", method = RequestMethod.PATCH)
public String myService(@RequestBody Map<String, String> payload){
    Long valueWhichIReallyNeed = Long.parseLong(payload.get("mySingleParameter"));
    //do job
    ...
}

or even

@RequestMapping(value = "my_service", method = RequestMethod.PATCH)
public String myService(@RequestBody  Long mySingleParameter){
    Long valueWhichIReallyNeed = mySingleParameter;
    //do job
    //  ...
}

but in this last case your curl will look following:

但在最后一种情况下,你的卷曲将如下所示:

curl -X PATCH myurl.net/my_service -d "49" 

In answers for this question you can find more options: Passing multiple variables in @RequestBody to a Spring MVC controller using Ajax

在这个问题的答案中,您可以找到更多选项:使用Ajax将@RequestBody中的多个变量传递给Spring MVC控制器