问题描述
我正在尝试隐藏域类中的属性,但它一直出现在输出的JSON中。我正在使用Jackson 2.0和Spring 3.1.1
I'm trying to make a property in a domain class hidden but it keeps appearing in the outputted JSON. I'm using Jackson 2.0 and Spring 3.1.1
输出/ users / 1:
Output of /users/1:
{"id":1,"password":null,"email":"[email protected]","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配置:
My springmvc config:
...
<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));
}
}
推荐答案
问题是Spring不适用于Jackson 2.0。这已通过以下方式解决:
The problem is that Spring doesn't work with Jackson 2.0. This has been solved in the following way:
<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基于此:
And the own.implementation.of.MappingJacksonHttpMessageConverter is based on this:
但是使用来自Jackson 2.0的ObjectMapper和其他Jackson类而不是Jackson 1。*
But use ObjectMapper and other Jackson classes from Jackson 2.0 instead of Jackson 1.*
这篇关于杰克逊的注释在春天被忽略了的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!