杰克逊的注释在春天被忽略了

时间:2012-04-10 07:54:22

标签: spring jackson

我正在尝试隐藏域类中的属性,但它一直出现在输出的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));
    }
}

5 个答案:

答案 0 :(得分:11)

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

答案 1 :(得分: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>

self.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

但是使用ObjectMapper和Jackson 2.0中的其他Jackson类而不是Jackson 1。*

答案 2 :(得分:8)

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

答案 3 :(得分:8)

如果您使用的是Spring 4版本,则必须执行以下操作:Update dependency

<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,已弃用。

  

ContentNegotiatingViewResolver

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

  

MappingJackson2JsonView

<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>

答案 4 :(得分: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;
}