0

我正在使用本教程https://spring.io/guides/gs/rest-service/

我想传递给 Web 服务 API 方法参数而不是字符串:

@RestController
public class ApiClass {

   @RequestMapping("/service")
   public int service(@RequestParam(value="paramIn") CustomClass paramIn) {
      if (paramIn.value != 0) return 1;
      else return 0;
  }

}

但是当我尝试它时,我得到了这个错误:

HTTP 状态 500 - 无法将类型“java.lang.String”的值转换为所需类型“CustomClass”;嵌套异常是 java.lang.IllegalStateException:无法将类型 [java.lang.String] 的值转换为所需类型 [CustomClass]:找不到匹配的编辑器或转换策略`

纳克斯,

4

1 回答 1

2

通常的方法是使用POSTorPUT方法并用@RequestBody. 例如:

 @RequestMapping(value = "/service", 
                 method = RequestMethod.POST, 
                 consumes = MediaType.APPLICATION_JSON_VALUE)
 public int service(@RequestBody CustomClass paramIn) {

     // do something with the paramIn

 }

如果您将实例POST的 JSON 表示形式CustomClass发送到端点,/serviceSpring 将对其进行反序列化并将其作为参数传递给您的控制器。

于 2015-08-01T20:13:58.163 回答