【问题标题】:How to automatically map JSON to Java Class in Spring Controller如何在 Spring Controller 中自动将 JSON 映射到 Java 类
【发布时间】:2014-02-10 15:27:27
【问题描述】:

我有以下代码:

public class MyClass {

    String xxx;
    String yyy;

    public String getXxx() {
        return xxx;
    }
    public void setXxx(String xxx) {
        this.xxx = xxx;
    }
    public String getYyy() {
        return yyy;
    }
    public void setYyy(String yyy) {
        this.yyy = yyy;
    }
    public MyClass(String xxx, String yyy) {
        super();
        this.xxx = xxx;
        this.yyy = yyy;
    }

    @Override
    public String toString() {
        return "MyClass [xxx=" + xxx + ", yyy=" + yyy + "]";
    }
}

我也实现了服务:

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;


@Controller
class MyService {

    @RequestMapping(value = "/abc", method = RequestMethod.POST, produces = "application/json")

    public @ResponseBody
    String add(@RequestBody String myClass, HttpServletRequest request,
        HttpServletResponse response) {

        return "Test";
    }

}

当我使用带有 JSON 的 DEV HTTP CLIENT 进行 HTTP 调用时:

{"xxx":"abc", "yyy":"abc"}

我看到一个错误:

错误 406 NOT_ACCEPTABLE

是否可以这样做,或者我必须对 JSON 进行编码并创建一个 Java 对象?

【问题讨论】:

  • 您的类路径中是否有可用的 Jackson 库 (jackson.codehaus.org)?当它们存在时,这应该是无缝的(具有相对普通的配置)。
  • 不,不是,但我不确定这是否是一个解决方案
  • 你一直在使用字符串而不是你的类。 @ResponseBody 通常应该返回代表 JSON 响应的 POJO。此外,显式设置 produces 值是多余的(并防止 Spring 返回 XML,以防万一)。

标签: java json spring rest http-status-code-415


【解决方案1】:

尝试在 @RequestMapping

中添加 produces="application/json"
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;


@Controller
class MyService {

@RequestMapping(value = "/abc", method = RequestMethod.POST,produces = "application/json" )
    public @ResponseBody String myMethod( @RequestBody String _json,HttpServletRequest request,
        HttpServletResponse response) {
        return _json;
    }   
}

【讨论】:

  • 还是不行。也许我从 DEV HTTP CLIENT 发送的数据有问题...
  • 添加到 myMethod 构造函数请求和响应,并更改字符串中 myClass 的类型。然后解析myClass中的字符串。
  • myMethod(@RequestBody String myClass,HttpServletRequest 请求, HttpServletResponse 响应)
  • 我做到了,现在我得到了:错误 406 NOT_ACCEPTABLE
  • 在我的测试 web 应用程序工作中,尝试没有请求和响应,然后返回 myClass 字符串。
【解决方案2】:

您可以在控制器中使用 google GSON 来完成此操作。

这里是例子:

            Gson gson = new Gson();
            JsonElement s = gson.toJsonTree(device, Devices.class);
            return s.toString();

不要忘记@ResponseBody

希望对你有帮助

【讨论】:

    猜你喜欢
    • 2012-12-30
    • 2018-11-19
    • 1970-01-01
    • 2018-06-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-13
    相关资源
    最近更新 更多