有了这段代码
@RequestMapping(value = "/bar/foo", method = RequestMethod.GET)
public ResponseEntity<foo> foo() {
Foo model;
...
return ResponseEntity.ok(model);
}
}
Run Code Online (Sandbox Code Playgroud)
我得到以下异常
java.lang.IllegalArgumentException: No converter found for return value of type
Run Code Online (Sandbox Code Playgroud)
我的猜测是,由于杰克逊失踪,该对象无法转换为JSON.我不明白为什么,因为我认为杰克逊是用春季靴子建造的.
然后我试图将Jackson添加到pom.xml但我仍然有同样的错误
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.4.3</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.4.3</version>
</dependency>
Run Code Online (Sandbox Code Playgroud)
我是否必须更改任何弹簧启动属性才能使其正常工作?
谢谢
我正在使用Spring 4.x进行Spring Rest Api项目
这个作品:
Controller.java
@PostMapping("newTransaction")
TransactionRequestModel insertNewTransaction(@RequestBody TransactionRequestModel model){
//do something
}
Run Code Online (Sandbox Code Playgroud)
TransactionRequestModel.java
public class TransactionRequestModel {
private int id;
private List<KeyValue> keyValueList;
public TransactionRequestModel(){}
//default constructor
//getter-setter
}
Run Code Online (Sandbox Code Playgroud)
KeyValue.java
public class KeyValue {
String key;
String value;
//default constructor
//setter-getter
}
Run Code Online (Sandbox Code Playgroud)
请求身体Json
{
"id": 1
"keyValueList": [
{
"key": "dummy",
"value": "dummy"
}
]
}
Run Code Online (Sandbox Code Playgroud)
使用jackson的Spring消息转换器工作正常.
这不会:
当我将TransactionRequestModel.java更改为以下(并删除KeyValue.java)时
public class TransactionRequestModel {
public class KeyValue {
String key;
String value;
//default constructor
//setter-getter …Run Code Online (Sandbox Code Playgroud)