为什么Spring MVC报告“找不到类型的返回值的转换器:class org.json.JSONObject”? [英] Why does Spring MVC report "No converter found for return value of type: class org.json.JSONObject"?

查看:2952
本文介绍了为什么Spring MVC报告“找不到类型的返回值的转换器:class org.json.JSONObject”?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想返回由两个字符串组成的JSON,并且不知道如何实现它。这是我的代码:

I want to return a JSON made of two strings and don't know how to implement it. Here is my code:

 @PostMapping
public ResponseEntity<> createUser(@RequestBody User user ) {

    JSONObject responseJson = new JSONObject();

    if (userService.userExists(user)) {

        responseJson.put("status", "User with that username already exists.");

        return new ResponseEntity<>(responseJson, HttpStatus.BAD_REQUEST);
    }

    responseJson.put("status", "User created.");

    return new ResponseEntity<>(responseJson, HttpStatus.CREATED);
}

我有 Json»20160810 com.fasterxml.jackson.core 我仍然得到 java.lang.IllegalArgumentException:No converter找到类型的返回值:class org.json.JSONObject 为什么Jackson不会自动转换我的JSON?这是一个标准的,只是简单的关键:价值。也许有更好的方法来创建使用jackson.core中的一些类的简单JSON,所以我不必在我的项目中包含Json lib并且jackson会自动转换它们吗?

I've got Json » 20160810 and com.fasterxml.jackson.core in my pom.xml and I'm still getting java.lang.IllegalArgumentException: No converter found for return value of type: class org.json.JSONObject Why doesn't Jackson automatically converts my JSON? It's a standard one, just simple key:value. Maybe there is better way to create simple JSONs using some class in jackson.core so I don't have to inlude Json lib in my project and jackson automatically converts them?

推荐答案

我不知道你为什么要使用两个JSON解析库。而不是创建 JSONObject ,创建Jackson的等价物 ObjectNode

I don't know why you're using two JSON parsing libraries. Instead of creating a JSONObject, create Jackson's equivalent ObjectNode.

假设您可以访问Spring MVC堆栈使用的 ObjectMapper

Assuming you have access to the ObjectMapper used by your Spring MVC stack

@Autowired
private ObjectMapper objectMapper;

用它来创建和填充 ObjectNode

ObjectNode jsonObject = mapper.createObjectNode();
jsonObject.put("status", "User with that username already exists.");
// don't forget to change return type to support this
return new ResponseEntity<>(jsonObject, HttpStatus.BAD_REQUEST);

由于这是杰克逊类型,杰克逊知道如何序列化它。

Since this is a Jackson type, Jackson knows how to serialize it.

它不知道如何序列化 JSONObject 。以下一些解释来自我的回答此处

It doesn't know how to serialize JSONObject. Some of the following explanation comes from my answer here.

基本上,Spring MVC使用 HandlerMethodReturnValueHandler 实现处理 @RequestMapping 返回的值( @PostMapping )带注释的方法。对于 ResponseEntity ,该实现是 HttpEntityMethodProcessor

Essentially, Spring MVC uses HandlerMethodReturnValueHandler implementations to handle values returned by @RequestMapping (@PostMapping) annotated methods. For ResponseEntity, that implementation is HttpEntityMethodProcessor.

此实现只是循环遍历 HttpMessageConverter 实例,检查实例是否可以序列化 ResponseEntity 的正文,如果可以,则使用它。

This implementation simply loops through a collection of HttpMessageConverter instances, checks if an instance can serialize the body of the ResponseEntity, and uses it if it can.

不幸的是,Jackson的 HttpMessageConverter 实现, MappingJackson2HttpMessageConverter ,使用一个 ObjectMapper 来序列化这些对象, ObjectMapper 无法序列化 JSONObject 因为它无法发现类中的任何属性(即bean getter)。

Unfortunately, Jackson's HttpMessageConverter implementation, MappingJackson2HttpMessageConverter, uses an ObjectMapper to serialize these objects and ObjectMapper cannot serialize JSONObject because it can't discover any properties in the class (ie. bean getters).

Jackson的 HttpMessageConverter 可以'这样做,所有其他默认注册的都不能。这就是Spring MVC报告无转换器的原因。

Jackson's HttpMessageConverter can't do it and neither can all the other ones that are registered by default. That's why Spring MVC reports "no converter".

org.springframework.web.util.NestedServletException: Request processing failed; nested exception is java.lang.IllegalArgumentException: No converter found for return value of type: class org.json.JSONObject

另一种解决方案是将 JSONObject 序列化为 String 并将其传递给 ResponseEntity 。显然,您需要更改返回类型以支持 String 。在这种情况下,Spring MVC将使用 StringHttpMessageConverter 。但是,您需要自己指定 application / json 内容类型,因为它不会添加它。例如,

An alternative solution is to serialize the JSONObject yourself to a String and pass that to the ResponseEntity. Obviously you'll want to change your return type to support String. In this case, Spring MVC will use a StringHttpMessageConverter. However, you'll want to specify the application/json content type yourself because it doesn't add it. For example,

HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
return new ResponseEntity<>(responseJson.toString(), headers, HttpStatus.BAD_REQUEST);

这篇关于为什么Spring MVC报告“找不到类型的返回值的转换器:class org.json.JSONObject”?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆