在下面的代码中,我尝试使用JackSonMixInAnnotations进行多态类型处理 但是我的代码没有用。
请建议我需要做哪些更改,以更正此代码。
我不想在Animal类中做任何改变。而且,像Dog类我可能有许多扩展Animal类的类。
代码应打印“Animal”(Dog或Elephant)类,具体取决于从客户端传递给MyController类中“test()”的Animal对象的“type”字段。
客户端可以发送JSON,如:
“{\”id \“:\”DOG \“,\”type \“:\”dog \“,\”someAttrOfDog \“:\”someValue \“}”
import org.codehaus.jackson.annotate.JsonSubTypes;
import org.codehaus.jackson.annotate.JsonTypeInfo;
@JsonTypeInfo( use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, property = "type")
/*@JsonSubTypes({// @JsonSubTypes.Type(value = Dog.class, name = "dog")
})*/
public abstract class Animal{
private String id;
private String type;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
}
package com.Animal;
import org.codehaus.jackson.annotate.JsonTypeName;
@JsonTypeName("cat")
public class Cat extends Animal{
String someAttrOfCat;
public String getSomeAttrOfCat() {
return someAttrOfCat;
}
public void setSomeAttrOfCat(String someAttrOfCat) {
this.someAttrOfCat = someAttrOfCat;
}
}
package com.Animal;
import org.codehaus.jackson.annotate.JsonTypeName;
@JsonTypeName("dog")
public class Dog extends Animal{
private String someAttrOfDog;
public String getSomeAttrOfDog() {
return someAttrOfDog;
}
public void setSomeAttrOfDog(String someAttrOfDog) {
this.someAttrOfDog = someAttrOfDog;
}
}
package com.Animal;
import org.codehaus.jackson.annotate.JsonSubTypes;
import org.codehaus.jackson.annotate.JsonSubTypes.Type;
@JsonSubTypes (
{ @Type(value = Dog.class, name = "dog") }
)
public class MixinDog {
}
package com.startUpClasses;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import org.codehaus.jackson.map.ObjectMapper;
import com.Animal.Animal;
import com.Animal.MixinDog;
public class StartApp extends HttpServlet{
public void init() throws ServletException{
System.out.println("---------In StartApp init------------");
ObjectMapper mapper = new ObjectMapper();
mapper.getSerializationConfig().addMixInAnnotations(Animal.class, MixinDog.class);
System.out.println("---------In StartApp init1------------");
mapper.getDeserializationConfig().addMixInAnnotations(Animal.class, MixinDog.class);
System.out.println("---------In StartApp init2------------");
}
}
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;
import com.Animal.Animal;
@Controller
public class MyController {
@RequestMapping( value = "test",method = RequestMethod.POST)
public @ResponseBody String test(@RequestBody Animal animal){
System.out.println("In Controller");
System.out.println(animal.getClass());
return "success";
}
}
AnimalWithJsonType-servlet.xml中
<context:component-scan base-package="com.restcontrollers"/>
<mvc:annotation-driven />
</beans>
的web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>AnimalWithJsonType</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>AnimalWithJsonType</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>0</load-on-startup>
</servlet>
<servlet>
<servlet-name>StartApp</servlet-name>
<servlet-class>com.startUpClasses.StartApp</servlet-class>
<load-on-startup>0</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>AnimalWithJsonType</servlet-name>
<url-pattern>/animals/*</url-pattern>
</servlet-mapping>
</web-app>