Java根据REST调用显示/隐藏对象字段

Java根据REST调用显示/隐藏对象字段,java,json,xml,spring,jackson,Java,Json,Xml,Spring,Jackson,是否可以根据REST调用配置Item类以显示和隐藏特定字段? 例如,当调用XmlController时,我想从User类隐藏colorId(并显示categoryId),反之亦然 项目类别 XML控制器 是的,可以使用和方法ObjectMapper#writerWithView 您只需要为两个控制器配置不同的ObjectMapper,就可以了 Jackson JSON视图的一个例子是,我们注意到,ownerName只能在内部访问,不能公开访问 公共类项目{ @JsonView(Views.Pub

是否可以根据REST调用配置Item类以显示和隐藏特定字段? 例如,当调用
XmlController
时,我想从
User
类隐藏
colorId
(并显示
categoryId
),反之亦然

项目类别 XML控制器
是的,可以使用和方法
ObjectMapper#writerWithView

您只需要为两个控制器配置不同的
ObjectMapper
,就可以了

Jackson JSON视图的一个例子是,我们注意到,
ownerName
只能在内部访问,不能公开访问

公共类项目{
@JsonView(Views.Public.class)
公共int id;
@JsonView(Views.Public.class)
公共字符串itemName;
@JsonView(Views.Internal.class)
公共字符串所有者名称;
}

我通过创建以下视图解决了这个问题:

public class View
{
    public static class Parent
    {
    }

    public static class Json extends Parent
    {
    }

    public static class Xml extends Parent
    {
    }
}
通过此配置,可以将类设置为:

@Getter
@Setter
@NoArgsConstructor
@JsonView(View.Parent.class)
public class Item
{
   private Long id;
   @JsonView(View.Xml.class)  // <-- Show field in XML REST call and hide in JSON REST call
   private Long categoryId;    
   @JsonView(View.Json.class) // <-- Show field in JSON REST call and hide in XML REST call
   private Long colorId;    
   private Long groupId;
   @JacksonXmlProperty(localName = "item")
   @JacksonXmlElementWrapper(localName = "groupItems")
   private List<GroupedItem> item;
}

谢谢你的提示!我用同样的概念,以不同的方式解决了这个问题,因为我使用的是Spring。使用以下视图,我可以分离每个REST调用的属性:`public class AcsView{public static class Acs{}public static class Json extends Acs{}public static class Xml extends Acs{}}`Super@Stefan,我很高兴您能解决它。出于好奇,你是怎么做到的?也许你可以回答自己的问题并接受它,这样每个人都知道它已经得到了回答。问题:)
@RestController
@RequestMapping(
    path = "/xml/",
    produces = MediaType.APPLICATION_XML_VALUE)
public class XmlController
{
    @Autowired
    private Service service;

    @RequestMapping(path = "{categoryId}")
    public Item getArticles(@PathVariable("categoryId") Long categoryId)
    {
        return service.getByCategory(categoryId); // returns XML without "colorId"
    }
}
public class View
{
    public static class Parent
    {
    }

    public static class Json extends Parent
    {
    }

    public static class Xml extends Parent
    {
    }
}
@Getter
@Setter
@NoArgsConstructor
@JsonView(View.Parent.class)
public class Item
{
   private Long id;
   @JsonView(View.Xml.class)  // <-- Show field in XML REST call and hide in JSON REST call
   private Long categoryId;    
   @JsonView(View.Json.class) // <-- Show field in JSON REST call and hide in XML REST call
   private Long colorId;    
   private Long groupId;
   @JacksonXmlProperty(localName = "item")
   @JacksonXmlElementWrapper(localName = "groupItems")
   private List<GroupedItem> item;
}
@JsonView(View.Json.class) // or @JsonView(View.Xml.class) in other case
@RequestMapping(path = "{colorId}")
public Item getArticles(@PathVariable("colorId") Long colorId)
{
    return service.getByColor(colorId); // returns JSON without "categoryId"
}