栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

在Spring MVC Controller中选择JsonView

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

在Spring MVC Controller中选择JsonView

我已经解决了扩展ResponseEntity这样的问题:

public class ResponseViewEntity<T> extends ResponseEntity<ContainerViewEntity<T>> {    private Class<? extends baseView> view;    public ResponseViewEntity(HttpStatus statusCode) {        super(statusCode);    }    public ResponseViewEntity(T body, HttpStatus statusCode) {        super(new ContainerViewEntity<T>(body, baseView.class), statusCode);    }    public ResponseViewEntity(T body, Class<? extends baseView> view, HttpStatus statusCode) {        super(new ContainerViewEntity<T>(body, view), statusCode);    }}

和ContainerViewEntity封装对象和选定的视图

public class ContainerViewEntity<T> {    private final T object;    private final Class<? extends baseView> view;    public ContainerViewEntity(T object, Class<? extends baseView> view) {        this.object = object;        this.view = view;    }    public T getObject() {        return object;    }    public Class<? extends baseView> getView() {        return view;    }    public boolean hasView() {        return this.getView() != null;    }}

之后,我们仅转换具有良好视图的对象。

public class JsonViewMessageConverter extends MappingJackson2HttpMessageConverter {    @Override    protected void writeInternal(Object object, HttpOutputMessage outputMessage) throws IOException, HttpMessageNotWritableException {        if (object instanceof ContainerViewEntity && ((ContainerViewEntity) object).hasView()) { writeView((ContainerViewEntity) object, outputMessage);        } else { super.writeInternal(object, outputMessage);        }    }    protected void writeView(ContainerViewEntity view, HttpOutputMessage outputMessage) throws IOException, HttpMessageNotWritableException {        JsonEncoding encoding = this.getJsonEncoding(outputMessage.getHeaders().getContentType());        ObjectWriter writer = this.getWriterForView(view.getView());        JsonGenerator jsonGenerator = writer.getFactory().createGenerator(outputMessage.getBody(), encoding);        try { writer.writevalue(jsonGenerator, view.getObject());        } catch (IOException ex) { throw new HttpMessageNotWritableException("Could not write JSON: " + ex.getMessage(), ex);        }    }    private ObjectWriter getWriterForView(Class<?> view) {        ObjectMapper mapper = new ObjectMapper();        mapper.configure(MapperFeature.DEFAULT_VIEW_INCLUSION, false);        return mapper.writer().withView(view);    }}

最后,我启用转换器

<mvc:annotation-driven>    <mvc:message-converters>        <bean />    </mvc:message-converters></mvc:annotation-driven>

就是这样,我可以在控制器中选择“视图”

@Override@RequestMapping(value = "/get_by_id/{accountId}", method = RequestMethod.GET, headers = "Accept=application/json")public ResponseViewEntity<Account> getById(@PathVariable(value = "accountId") Long accountId) throws ServiceException {    return new ResponseViewEntity<Account>(this.getAccountSrv().getById(accountId), PublicView.class, HttpStatus.OK);}


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/373346.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号