我可以使Controller端点接受其他json对象(我​​在Java端具有匹配的类)吗? [英] Can I make the Controller endpoint accept different json object (I have the matched class in java side)?

查看:66
本文介绍了我可以使Controller端点接受其他json对象(我​​在Java端具有匹配的类)吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Java端有不同的Java类.

I have different java class in Java side .

public class Prov{
  private String providerId = "";
  private String upin = "";
  ...
  //with getter and setter
}

public class Proc{
  private String procId = "";
  private String description = "";
  ...
  //with getter and setter
}

@Controller
public class Controller{

  @RequestMapping(value = "service/reports/prov", method = RequestMethod.POST)
  @ResponseBody
  public String searchProv(@ResponseBody Prov prov){
  return "prov"
  }

  @RequestMapping(value = "service/reports/proc", method = RequestMethod.POST)
  @ResponseBody
  public String searchProv(@ResponseBody Proc proc){
  return "proc"
  }
}

现在我在Controller中定义了两个端点,我想知道是否可以将不同的json对象传递给同一端点.

Now I have two endpoints defined in Controller , I wonder if it is possible to passing different json object to the same endpoint.

我尝试使用像C这样的继承类.Proc扩展了C,Prov扩展了C.将控制器更改为

I tried using a inheritance class like C. Proc extends C and Prov extends C. change the Controller to

@Controller
public class Controller{

  @RequestMapping(value = "service/reports", method = RequestMethod.POST)
  @ResponseBody
  public String searchProv(@ResponseBody C c){
    if(c instanceof Prov){
      return "prov"
    }        
    if(c instanceof Proc){
      return "proc"
    }
  return "error"
  }

但是它不起作用.它获取ClassCastingException.我认为这可能是由javascript引起的,没有类继承.(如果我错了,请告诉我.如果您可以解释原因,那将是不错的选择)我认为通用类可能会有所帮助.但是我不太熟悉.或任何其他方式来做到这一点.谢谢.

But It's not working. It gets ClassCastingException. I think that maybe caused by javascript don't have class inheritance.(Please tell me if I'm wrong .if you can explain the reason, that would be nice) I think maybe generic class might help. But I'm not quite familiar with it. Or any other way to do it. Thank you.

推荐答案

您需要做的第一件事是确保您的客户端在JSON中发送额外的数据,以便Jackson知道如何反序列化该对象.对于下面的示例,我使用两个JSON对其进行了测试:

First thing you need to do is make sure that your client sends inside the JSON extra data so that Jackson knows how to deserialize that object. For the examples below, I tested it with two JSONs:

{
  "type": "cat",
  "name": "Skippy"
}

{
  "type": "dog",
  "name": "Spike"
}

首先,您使用 Jackson Polymorfic映射您的类反序列化.就我而言,我使用的是称为 type 的属性来决定将其反序列化到哪个类.以下是我的基类 Animal :

First you map yor classes using Jackson Polymorfic Deserialization. In my case I'm using an attribute called type to decide what class it will be deserialized to. The following is my base class Animal:

@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, property = "type")
@JsonSubTypes({ @Type(value = Cat.class, name = "cat"), @Type(value = Dog.class, name = "dog") })
public class Animal {

  private String name;
  public String getName() {
    return name;
  }
  public void setName(String name) {
    this.name = name;
  }
}

我的班级 Dog 和`Cat:

public class Dog extends Animal {
  public void bark() {
    System.out.println("Au!");
  }
}

public class Cat extends Animal {
  public void miau() {
    System.out.println("Miau!");
  }
}

在您的控制器中,您可以执行通常的操作:

In your controller you can do what you did normally:

@Controller
public class ExampleController {

  @RequestMapping(value = "animal", method = RequestMethod.POST)
  @ResponseBody
  public String searchProv(@RequestBody Animal animal) {
    if (animal instanceof Dog) {
      ((Dog) animal).bark();
      return "dog";
    }
    if (animal instanceof Cat) {
      ((Cat) animal).miau();
      return "cat";
    }
    return "error";
  }
}

这篇关于我可以使Controller端点接受其他json对象(我​​在Java端具有匹配的类)吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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