2013-04-12 20 views
4

我使用Jackson JSON 1.9.12和SpringMVC。我用JSON字段創建了一個dto。我想要具有不同JSON字段的相同dto的兩個配置文件,因此我創建了兩個接口,並使用@JsonView註釋標註了字段。傑克遜@JsonView不能按預期工作

class Views { 
    static class Public {} 
    static class ExtendedPublic {} 
    ... 
} 

public class Thing { 
    @JsonView(Views.Public.class) Integer id; 
    @JsonView(Views.ExtendPublic.class) String name; 
} 

在控制器

@RequestMapping(value = "/thing/{id}") 
public void getThing(@PathVariable final String id, HttpServletResponse response) { 
    Thing thing = new Thing(); 
    ObjectMapper objectMapper = new ObjectMapper(); 
    String json = objectMapper.writerWithView(Views.Public.class).writeValueAsString(thing); 
    LOG.debug("JSON: {}", json); 
} 

我希望JSON只包含 「id」 字段,但它總是包含了所有領域。

任何想法?

+1

最後得到全面更新我的回答有一個完整的Maven項目演示了'JSONView'控制器 – andyb

+1

@andyb也許你可以創建一個GitHub的項目 –

回答

1

以下代碼使用Spring 3.2.0和Jackson 1.9.12進行測試,該代碼只是返回{id: 1}而不是擴展{name: "name"},因爲它使用.writerWithView(Views.Public.class)。切換到Views.ExtendPublic.class將導致{"id":1,"name":"name"}

編輯:添加所有項目文件的完整解決方案,使之成爲一個更好的答案比我的其他一個Using @JsonView with Spring MVC

DemoController.java

package com.demo.app; 

import org.springframework.stereotype.Controller; 
import org.springframework.web.bind.annotation.RequestMapping; 
import org.springframework.web.bind.annotation.ResponseBody; 
import org.codehaus.jackson.map.annotate.JsonView; 
import org.codehaus.jackson.map.ObjectMapper; 
import org.codehaus.jackson.map.ObjectWriter; 
import javax.servlet.http.HttpServletResponse; 
import java.io.IOException; 

@Controller 
public class DemoController { 
    private final ObjectMapper objectMapper = new ObjectMapper(); 

    @RequestMapping(value="/jsonOutput") 
    @ResponseBody 
    public String myObject(HttpServletResponse response) throws IOException { 
     ObjectWriter objectWriter = objectMapper.writerWithView(Views.Public.class); 
     return objectWriter.writeValueAsString(new MyObject()); 
    } 

    public static class Views { 
     static class Public {} 
     static class ExtendPublic extends Public {} 
    } 

    public class MyObject { 
     @JsonView(Views.Public.class) Integer id = 1; 
     @JsonView(Views.ExtendPublic.class) String name = "name"; 
    } 
} 

的web.xml

<web-app xmlns="http://java.sun.com/xml/ns/javaee" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" 
    version="3.0"/> 

Initializer.java

package com.demo.app.spring; 

import org.springframework.web.context.support.AnnotationConfigWebApplicationContext; 
import org.springframework.web.WebApplicationInitializer; 
import org.springframework.web.servlet.DispatcherServlet; 
import javax.servlet.*; 

public class Initializer implements WebApplicationInitializer { 
    public void onStartup(ServletContext servletContext) throws ServletException { 
     AnnotationConfigWebApplicationContext mvcContext = new AnnotationConfigWebApplicationContext(); 
     mvcContext.register(AppConfig.class); 

     ServletRegistration.Dynamic dispatcher = servletContext.addServlet("dispatcher", new DispatcherServlet(mvcContext)); 
     dispatcher.setLoadOnStartup(1); 
     dispatcher.addMapping("/*"); 
    } 
} 

AppConfig.java

package com.demo.app.spring; 

import org.springframework.context.annotation.*; 
import org.springframework.web.servlet.config.annotation.EnableWebMvc; 
import org.springframework.web.servlet.view.InternalResourceViewResolver; 

@Configuration 
@EnableWebMvc 
@ComponentScan(basePackages="com.demo.app") 
public class AppConfig { 
} 

的pom.xml

<?xml version="1.0" encoding="UTF-8"?> 
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 
    <modelVersion>4.0.0</modelVersion> 
    <groupId>com.demo</groupId> 
    <artifactId>app</artifactId> 
    <version>1.0.0-SNAPSHOT</version> 
    <packaging>war</packaging> 
    <name>${project.artifactId}</name> 
    <properties> 
     <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 
     <spring.version>3.2.0.RELEASE</spring.version> 
     <jetty.plugin.version>8.1.7.v20120910</jetty.plugin.version> 
    </properties> 
    <dependencies> 
     <dependency> 
      <groupId>org.springframework</groupId> 
      <artifactId>spring-webmvc</artifactId> 
      <version>${spring.version}</version> 
     </dependency> 
     <dependency> 
      <groupId>org.codehaus.jackson</groupId> 
      <artifactId>jackson-core-asl</artifactId> 
      <version>1.9.12</version> 
     </dependency> 
     <dependency> 
      <groupId>org.codehaus.jackson</groupId> 
      <artifactId>jackson-mapper-asl</artifactId> 
      <version>1.9.12</version> 
     </dependency> 
     <dependency> 
      <groupId>javax.servlet</groupId> 
      <artifactId>servlet-api</artifactId> 
      <version>3.0.1</version> 
      <scope>provided</scope> 
     </dependency> 
    </dependencies> 
    <build> 
     <finalName>${project.artifactId}</finalName> 
     <plugins> 
      <plugin> 
       <groupId>org.mortbay.jetty</groupId> 
       <artifactId>jetty-maven-plugin</artifactId> 
       <version>${jetty.plugin.version}</version> 
       <configuration> 
        <webApp> 
         <contextPath>/</contextPath> 
        </webApp> 
       </configuration> 
      </plugin> 
     </plugins> 
    </build> 
</project> 

在下面的結構:

pom.xml 
src 
    |- main 
     |- java 
     | |- com 
     |  |- demo 
     |    |- app 
     |     |- controller/DemoController.java 
     |     |- spring/AppConfig.java, Initializer.java 
     |- webapp 
      |- WEB-INF/web.xml 
+0

它不適合我,我不知道原因。獲得它的獨特方式正確地工作是添加objectMapper.configure(org.codehaus.jackson.map.SerializationConfig.Feature.DEFAULT_VIEW_INCLUSION,false);我想配置spring來使用JsonViews,有可能嗎? – Premier

+0

很奇怪!我在本地爲我工作。如果你在你的項目中使用這個確切的類,你是否在輸出中看到'id'和'name'?除了某些標準的Spring MVC配置外,我沒有其他配置。 – andyb

+0

我已經使用你的代碼和輸出包含ID和名稱 – Premier