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