15

我正在尝试隐藏域类中的属性,但它一直出现在输出的 JSON 中。我正在使用 Jackson 2.0 和 Spring 3.1.1

/users/1 的输出:

{"id":1,"password":null,"email":"someone@somewhere.com","firstName":"John","lastName":"Smith"}

我的域类:

@Entity
public class User {
    private String mPassword;
    ... 
    @JsonIgnore
    public String getPassword() {
        return mPassword;
    }

    public void setPassword(String password) {
        mPassword = password;
    }
    ...
}

我的springmvc配置:

...
<mvc:annotation-driven/>
<bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
    <property name="favorPathExtension" value="true" />
    <property name="mediaTypes">
        <map>
            <entry key="json" value="application/json"/>
        </map>
    </property>
    <property name="defaultContentType" value="application/json"/>
    <property name="defaultViews">
        <list>
            <bean class="org.springframework.web.servlet.view.json.MappingJacksonJsonView" />
        </list>
    </property>
</bean>
...

我的控制器:

@Controller
@RequestMapping("/users")
public class UserController {
    private UserService mUserService;

    public UserController(){}

    @Inject
    public void setUserController(UserService userService){
        mUserService=userService;
    }

    @RequestMapping(value="/{id}", method=RequestMethod.GET)
    public void getUser(@PathVariable("id") long id, Model model){
        model.addAttribute(mUserService.getUser(id));
    }
}
4

5 回答 5

11

对 Jackson 2 的支持已添加到 Spring 3.1.2(从 Spring 3.2 - SPR-9507向后移植)。只需将您的项目从 Spring 3.1.1 升级到 Spring 3.1.2,Jackson 2 就可以使用您已有的配置。

于 2012-07-15T21:17:47.717 回答
10

问题是 Spring 不适用于 Jackson 2.0。这已通过以下方式解决:

<bean id="jacksonMessageConverter"
          class="own.implementation.of.MappingJacksonHttpMessageConverter"/>

<bean class="org.springframework.web.servlet.mvc
             .annotation.AnnotationMethodHandlerAdapter">
        <property name="messageConverters">
            <list>
                <ref bean="jacksonMessageConverter"/>
            </list>
        </property>
        <property name="requireSession" value="false"/>
    </bean>

而own.implementation.of.MappingJacksonHttpMessageConverter就是基于这个:

http://www.jarvana.com/jarvana/view/org/springframework/spring-web/3.0.0.RELEASE/spring-web-3.0.0.RELEASE-sources.jar!/org/springframework/http/converter /json/MappingJacksonHttpMessageConverter.java?format=ok

但是使用来自 Jackson 2.0 的 ObjectMapper 和其他 Jackson 类而不是 Jackson 1.*

于 2012-04-11T18:01:46.983 回答
8

如果您使用的是 Spring 4 版本,则必须执行以下操作,更新依赖项

<dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-core</artifactId>
        <version>2.3.3</version>
    </dependency>

<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>2.3.3</version>
</dependency>

将旧的 codehaus 注释导入替换为以下内容,如果不这样做,您的旧注释(如 JsonIgnoreProperties)将不适用于新版本的 Jackson

import com.fasterxml.jackson.annotation.*;

从下面删除 mediaTypes,它已被弃用。

内容协商视图解析器

, 不需要,将从激活框架jar中提取,Jackson视图应该使用

映射Jackson2JsonView

<beans:property name="viewResolvers">
    <beans:list>
        <beans:bean
            class="org.springframework.web.servlet.view.BeanNameViewResolver" />
        <beans:bean
            class="org.springframework.web.servlet.view.InternalResourceViewResolver">
            <beans:property name="prefix" value="/WEB-INF/views/" />
            <beans:property name="suffix" value=".jsp" />
        </beans:bean>
        <beans:bean
            class="org.springframework.web.servlet.view.tiles3.TilesViewResolver">
            <beans:property name="viewClass"
                value="org.springframework.web.servlet.view.tiles3.TilesView"></beans:property>
            <beans:property name="order" value="0"></beans:property>
        </beans:bean>
    </beans:list>
</beans:property>
<beans:property name="defaultViews">
    <beans:list>
        <beans:bean
            class="org.springframework.web.servlet.view.json.MappingJackson2JsonView" />
    </beans:list>
</beans:property>

于 2014-04-20T04:57:20.977 回答
8

除了升级到Spring 3.1.2,您还必须将消息转换器更改为 org.springframework.web.servlet.view.json.MappingJackson**2**HttpMessageConverter

于 2013-01-17T07:36:27.800 回答
0

我的配置看起来像这样

<bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver" p:order="1">
  <property name="mediaTypes">
    <map>
      <entry key="html" value="text/html"/>
      <entry key="json" value="application/json"/>
    </map>
   </property>
  <property name="viewResolvers">
    <!-- this is not important for your jackson problem -->
    <bean id="tilesViewResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver" p:viewClass="org.springframework.web.servlet.view.tiles2.TilesView" p:order="2" depends-on="tilesConfigurer"/>
  </property>
  <property name="defaultViews">
    <list>
      <bean class="org.springframework.web.servlet.view.json.MappingJacksonJsonView"/>
    </list>
  </property>
</bean>

我的控制器看起来像这样

@Controller
public class SearchController {
   @RequestMapping(value="/search", method = {RequestMethod.GET, RequestMethod.POST})
   public SearchResponse search(@RequestParam(value = QUERY, required = true) String query) {
       return generateSearchResponse(query);
   }
}    

我的 SearchResponse 结构更复杂,但它包含这种类型的对象。

@JsonIgnoreProperties({ "popis" })
public class Kurz implements Serializable {
    private String nazev;
    private String popis;
}
于 2012-04-10T08:17:40.870 回答