Commit bbc3902b by Java-张振楠

①轮播图接口

②首页滚动信息、弹出信息接口
③完善框架内容
parent 01a2e078
package com.thinkgem.jeesite.common.baseBean;
import com.alibaba.fastjson.annotation.JSONField;
import com.fasterxml.jackson.annotation.JsonIgnore;
public class BaseEntity implements java.io.Serializable {
private String id; // 主键
private String code; // 唯一编码
@JSONField(deserialize = false, serialize = false)
@JsonIgnore
private String created; //创建时间
@JSONField(deserialize = false, serialize = false)
@JsonIgnore
private String createdUser; //创建人
@JSONField(deserialize = false, serialize = false)
@JsonIgnore
private String modified; //修改时间
@JSONField(deserialize = false, serialize = false)
@JsonIgnore
private String modifiedUser; //修改人
public String getId() {
......
......@@ -10,8 +10,8 @@ public class Response implements java.io.Serializable {
private String status;
private String message;
private String error;
private String current;//FORMAT.format(System.currentTimeMillis());
private Long currentTimeMillis;//FORMAT.format(System.currentTimeMillis());
private String current;
private Long currentTimeMillis;
public Response() {
this.setStatus(ComCode.STATUS_CODE_2000);
......
......@@ -15,5 +15,7 @@ public class ComCode {
public static final String STATUS_CODE_2000_DESC = "成功";
public static final String STATUS_CODE_9999 = "9999";
public static final String STATUS_CODE_9999_DESC = "验证SIGN失败";
public static final String STATUS_CODE_9998 = "9998";
public static final String STATUS_CODE_9998_DESC = "通用异常";
}
......@@ -11,7 +11,7 @@ import java.io.IOException;
/**
* 验证sign
*/
@WebFilter(filterName = "validationFilter", urlPatterns = "/api/*")
//@WebFilter(filterName = "validationFilter", urlPatterns = "/api/*")
public class ValidationFilter implements Filter {
@Override
public void init(FilterConfig filterConfig) throws ServletException {
......
......@@ -32,230 +32,235 @@ import com.google.common.collect.Maps;
/**
* 简单封装Jackson,实现JSON String<->Java Object的Mapper.
* 封装不同的输出风格, 使用不同的builder函数创建实例.
*
* @author ThinkGem
* @version 2013-11-15
*/
public class JsonMapper extends ObjectMapper {
private static final long serialVersionUID = 1L;
private static final long serialVersionUID = 1L;
private static Logger logger = LoggerFactory.getLogger(JsonMapper.class);
private static Logger logger = LoggerFactory.getLogger(JsonMapper.class);
private static JsonMapper mapper;
private static JsonMapper mapper;
public JsonMapper() {
this(Include.NON_EMPTY);
}
public JsonMapper() {
this(Include.ALWAYS);
}
public JsonMapper(Include include) {
// 设置输出时包含属性的风格
if (include != null) {
this.setSerializationInclusion(include);
}
// 允许单引号、允许不带引号的字段名称
this.enableSimple();
// 设置输入时忽略在JSON字符串中存在但Java对象实际没有的属性
this.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
public JsonMapper(Include include) {
// 设置输出时包含属性的风格
if (include != null) {
this.setSerializationInclusion(include);
}
// 允许单引号、允许不带引号的字段名称
this.enableSimple();
// 设置输入时忽略在JSON字符串中存在但Java对象实际没有的属性
this.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
// 空值处理为空串
this.getSerializerProvider().setNullValueSerializer(new JsonSerializer<Object>(){
@Override
public void serialize(Object value, JsonGenerator jgen,
SerializerProvider provider) throws IOException,
JsonProcessingException {
jgen.writeString("");
}
this.getSerializerProvider().setNullValueSerializer(new JsonSerializer<Object>() {
@Override
public void serialize(Object value, JsonGenerator jgen,
SerializerProvider provider) throws IOException,
JsonProcessingException {
jgen.writeString("");
}
});
// 进行HTML解码。
this.registerModule(new SimpleModule().addSerializer(String.class, new JsonSerializer<String>(){
@Override
public void serialize(String value, JsonGenerator jgen,
SerializerProvider provider) throws IOException,
JsonProcessingException {
jgen.writeString(StringEscapeUtils.unescapeHtml4(value));
}
// 进行HTML解码。
this.registerModule(new SimpleModule().addSerializer(String.class, new JsonSerializer<String>() {
@Override
public void serialize(String value, JsonGenerator jgen,
SerializerProvider provider) throws IOException,
JsonProcessingException {
jgen.writeString(StringEscapeUtils.unescapeHtml4(value));
}
}));
// 设置时区
this.setTimeZone(TimeZone.getDefault());//getTimeZone("GMT+8:00")
}
// 设置时区
this.setTimeZone(TimeZone.getDefault());//getTimeZone("GMT+8:00")
}
/**
* 创建只输出非Null且非Empty(如List.isEmpty)的属性到Json字符串的Mapper,建议在外部接口中使用.
*/
public static JsonMapper getInstance() {
if (mapper == null){
mapper = new JsonMapper().enableSimple();
}
return mapper;
}
/**
* 创建只输出非Null且非Empty(如List.isEmpty)的属性到Json字符串的Mapper,建议在外部接口中使用.
*/
public static JsonMapper getInstance() {
if (mapper == null) {
mapper = new JsonMapper().enableSimple();
}
return mapper;
}
/**
* 创建只输出初始值被改变的属性到Json字符串的Mapper, 最节约的存储方式,建议在内部接口中使用。
*/
public static JsonMapper nonDefaultMapper() {
if (mapper == null){
mapper = new JsonMapper(Include.NON_DEFAULT);
}
return mapper;
}
/**
* Object可以是POJO,也可以是Collection或数组。
* 如果对象为Null, 返回"null".
* 如果集合为空集合, 返回"[]".
*/
public String toJson(Object object) {
try {
return this.writeValueAsString(object);
} catch (IOException e) {
logger.warn("write to json string error:" + object, e);
return null;
}
}
/**
* 创建只输出初始值被改变的属性到Json字符串的Mapper, 最节约的存储方式,建议在内部接口中使用。
*/
public static JsonMapper nonDefaultMapper() {
if (mapper == null) {
mapper = new JsonMapper(Include.NON_DEFAULT);
}
return mapper;
}
/**
* 反序列化POJO或简单Collection如List<String>.
*
* 如果JSON字符串为Null或"null"字符串, 返回Null.
* 如果JSON字符串为"[]", 返回空集合.
*
* 如需反序列化复杂Collection如List<MyBean>, 请使用fromJson(String,JavaType)
* @see #fromJson(String, JavaType)
*/
public <T> T fromJson(String jsonString, Class<T> clazz) {
if (StringUtils.isEmpty(jsonString)) {
return null;
}
try {
return this.readValue(jsonString, clazz);
} catch (IOException e) {
logger.warn("parse json string error:" + jsonString, e);
return null;
}
}
/**
* Object可以是POJO,也可以是Collection或数组。
* 如果对象为Null, 返回"null".
* 如果集合为空集合, 返回"[]".
*/
public String toJson(Object object) {
try {
return this.writeValueAsString(object);
} catch (IOException e) {
logger.warn("write to json string error:" + object, e);
return null;
}
}
/**
* 反序列化复杂Collection如List<Bean>, 先使用函數createCollectionType构造类型,然后调用本函数.
* @see #createCollectionType(Class, Class...)
*/
@SuppressWarnings("unchecked")
public <T> T fromJson(String jsonString, JavaType javaType) {
if (StringUtils.isEmpty(jsonString)) {
return null;
}
try {
return (T) this.readValue(jsonString, javaType);
} catch (IOException e) {
logger.warn("parse json string error:" + jsonString, e);
return null;
}
}
/**
* 反序列化POJO或简单Collection如List<String>.
* <p>
* 如果JSON字符串为Null或"null"字符串, 返回Null.
* 如果JSON字符串为"[]", 返回空集合.
* <p>
* 如需反序列化复杂Collection如List<MyBean>, 请使用fromJson(String,JavaType)
*
* @see #fromJson(String, JavaType)
*/
public <T> T fromJson(String jsonString, Class<T> clazz) {
if (StringUtils.isEmpty(jsonString)) {
return null;
}
try {
return this.readValue(jsonString, clazz);
} catch (IOException e) {
logger.warn("parse json string error:" + jsonString, e);
return null;
}
}
/**
* 構造泛型的Collection Type如:
* ArrayList<MyBean>, 则调用constructCollectionType(ArrayList.class,MyBean.class)
* HashMap<String,MyBean>, 则调用(HashMap.class,String.class, MyBean.class)
*/
public JavaType createCollectionType(Class<?> collectionClass, Class<?>... elementClasses) {
return this.getTypeFactory().constructParametricType(collectionClass, elementClasses);
}
/**
* 反序列化复杂Collection如List<Bean>, 先使用函數createCollectionType构造类型,然后调用本函数.
*
* @see #createCollectionType(Class, Class...)
*/
@SuppressWarnings("unchecked")
public <T> T fromJson(String jsonString, JavaType javaType) {
if (StringUtils.isEmpty(jsonString)) {
return null;
}
try {
return (T) this.readValue(jsonString, javaType);
} catch (IOException e) {
logger.warn("parse json string error:" + jsonString, e);
return null;
}
}
/**
* 當JSON裡只含有Bean的部分屬性時,更新一個已存在Bean,只覆蓋該部分的屬性.
*/
@SuppressWarnings("unchecked")
public <T> T update(String jsonString, T object) {
try {
return (T) this.readerForUpdating(object).readValue(jsonString);
} catch (JsonProcessingException e) {
logger.warn("update json string:" + jsonString + " to object:" + object + " error.", e);
} catch (IOException e) {
logger.warn("update json string:" + jsonString + " to object:" + object + " error.", e);
}
return null;
}
/**
* 構造泛型的Collection Type如:
* ArrayList<MyBean>, 则调用constructCollectionType(ArrayList.class,MyBean.class)
* HashMap<String,MyBean>, 则调用(HashMap.class,String.class, MyBean.class)
*/
public JavaType createCollectionType(Class<?> collectionClass, Class<?>... elementClasses) {
return this.getTypeFactory().constructParametricType(collectionClass, elementClasses);
}
/**
* 輸出JSONP格式數據.
*/
public String toJsonP(String functionName, Object object) {
return toJson(new JSONPObject(functionName, object));
}
/**
* 當JSON裡只含有Bean的部分屬性時,更新一個已存在Bean,只覆蓋該部分的屬性.
*/
@SuppressWarnings("unchecked")
public <T> T update(String jsonString, T object) {
try {
return (T) this.readerForUpdating(object).readValue(jsonString);
} catch (JsonProcessingException e) {
logger.warn("update json string:" + jsonString + " to object:" + object + " error.", e);
} catch (IOException e) {
logger.warn("update json string:" + jsonString + " to object:" + object + " error.", e);
}
return null;
}
/**
* 設定是否使用Enum的toString函數來讀寫Enum,
* 為False時時使用Enum的name()函數來讀寫Enum, 默認為False.
* 注意本函數一定要在Mapper創建後, 所有的讀寫動作之前調用.
*/
public JsonMapper enableEnumUseToString() {
this.enable(SerializationFeature.WRITE_ENUMS_USING_TO_STRING);
this.enable(DeserializationFeature.READ_ENUMS_USING_TO_STRING);
return this;
}
/**
* 輸出JSONP格式數據.
*/
public String toJsonP(String functionName, Object object) {
return toJson(new JSONPObject(functionName, object));
}
/**
* 支持使用Jaxb的Annotation,使得POJO上的annotation不用与Jackson耦合。
* 默认会先查找jaxb的annotation,如果找不到再找jackson的。
*/
public JsonMapper enableJaxbAnnotation() {
JaxbAnnotationModule module = new JaxbAnnotationModule();
this.registerModule(module);
return this;
}
/**
* 設定是否使用Enum的toString函數來讀寫Enum,
* 為False時時使用Enum的name()函數來讀寫Enum, 默認為False.
* 注意本函數一定要在Mapper創建後, 所有的讀寫動作之前調用.
*/
public JsonMapper enableEnumUseToString() {
this.enable(SerializationFeature.WRITE_ENUMS_USING_TO_STRING);
this.enable(DeserializationFeature.READ_ENUMS_USING_TO_STRING);
return this;
}
/**
* 允许单引号
* 允许不带引号的字段名称
*/
public JsonMapper enableSimple() {
this.configure(Feature.ALLOW_SINGLE_QUOTES, true);
this.configure(Feature.ALLOW_UNQUOTED_FIELD_NAMES, true);
return this;
}
/**
* 取出Mapper做进一步的设置或使用其他序列化API.
*/
public ObjectMapper getMapper() {
return this;
}
/**
* 支持使用Jaxb的Annotation,使得POJO上的annotation不用与Jackson耦合。
* 默认会先查找jaxb的annotation,如果找不到再找jackson的。
*/
public JsonMapper enableJaxbAnnotation() {
JaxbAnnotationModule module = new JaxbAnnotationModule();
this.registerModule(module);
return this;
}
/**
* 允许单引号
* 允许不带引号的字段名称
*/
public JsonMapper enableSimple() {
this.configure(Feature.ALLOW_SINGLE_QUOTES, true);
this.configure(Feature.ALLOW_UNQUOTED_FIELD_NAMES, true);
return this;
}
/**
* 取出Mapper做进一步的设置或使用其他序列化API.
*/
public ObjectMapper getMapper() {
return this;
}
/**
* 对象转换为JSON字符串
*
* @param object
* @return
*/
public static String toJsonString(Object object) {
return JsonMapper.getInstance().toJson(object);
}
/**
* JSON字符串转换为对象
*
* @param jsonString
* @param clazz
* @return
*/
public static Object fromJsonString(String jsonString, Class<?> clazz) {
return JsonMapper.getInstance().fromJson(jsonString, clazz);
}
/**
* 测试
*/
public static void main(String[] args) {
List<Map<String, Object>> list = Lists.newArrayList();
Map<String, Object> map = Maps.newHashMap();
map.put("id", 1);
map.put("pId", -1);
map.put("name", "根节点");
list.add(map);
map = Maps.newHashMap();
map.put("id", 2);
map.put("pId", 1);
map.put("name", "你好");
map.put("open", true);
list.add(map);
String json = JsonMapper.getInstance().toJson(list);
System.out.println(json);
}
/**
* 对象转换为JSON字符串
* @param object
* @return
*/
public static String toJsonString(Object object){
return JsonMapper.getInstance().toJson(object);
}
/**
* JSON字符串转换为对象
* @param jsonString
* @param clazz
* @return
*/
public static Object fromJsonString(String jsonString, Class<?> clazz){
return JsonMapper.getInstance().fromJson(jsonString, clazz);
}
/**
* 测试
*/
public static void main(String[] args) {
List<Map<String, Object>> list = Lists.newArrayList();
Map<String, Object> map = Maps.newHashMap();
map.put("id", 1);
map.put("pId", -1);
map.put("name", "根节点");
list.add(map);
map = Maps.newHashMap();
map.put("id", 2);
map.put("pId", 1);
map.put("name", "你好");
map.put("open", true);
list.add(map);
String json = JsonMapper.getInstance().toJson(list);
System.out.println(json);
}
}
......@@ -19,9 +19,15 @@ public class AdvController {
@PostMapping("/getAdvList")
public Response getAdvList(AdvRequest request) {
Response resp = new Response();
resp.setData(advService.getAdvList(request));
resp.setStatus(ComCode.STATUS_CODE_2000);
resp.setMessage(ComCode.STATUS_CODE_2000_DESC);
try {
resp.setData(advService.getAdvList(request));
resp.setStatus(ComCode.STATUS_CODE_2000);
resp.setMessage(ComCode.STATUS_CODE_2000_DESC);
} catch (Exception e) {
resp.setStatus(ComCode.STATUS_CODE_9998);
resp.setMessage(ComCode.STATUS_CODE_9998_DESC);
resp.setError(e.getMessage());
}
return resp;
}
......
package com.thinkgem.jeesite.modules.homepage.api;
import com.thinkgem.jeesite.common.baseBean.Response;
import com.thinkgem.jeesite.common.constant.ComCode;
import com.thinkgem.jeesite.modules.homepage.bean.AdvRequest;
import com.thinkgem.jeesite.modules.homepage.bean.NoticeRequest;
import com.thinkgem.jeesite.modules.homepage.service.AdvService;
import com.thinkgem.jeesite.modules.homepage.service.NoticeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* 验证失败返回
*/
@RestController
@RequestMapping("/api/home")
public class NoticeController {
@Autowired
private NoticeService noticeService;
@PostMapping("/getNotices")
public Response getNotices(NoticeRequest request) {
Response resp = new Response();
try {
resp.setData(noticeService.getNotice(request));
resp.setStatus(ComCode.STATUS_CODE_2000);
resp.setMessage(ComCode.STATUS_CODE_2000_DESC);
} catch (Exception e) {
resp.setStatus(ComCode.STATUS_CODE_9998);
resp.setMessage(ComCode.STATUS_CODE_9998_DESC);
resp.setError(e.getMessage());
}
return resp;
}
}
package com.thinkgem.jeesite.modules.homepage.bean;
import com.thinkgem.jeesite.common.baseBean.Request;
public class NoticeRequest extends Request {
private String noticeType; // 通知类型,F首页滚动通知 T首页弹窗
public String getNoticeType() {
return noticeType;
}
public void setNoticeType(String noticeType) {
this.noticeType = noticeType;
}
}
package com.thinkgem.jeesite.modules.homepage.dao;
import com.thinkgem.jeesite.common.persistence.annotation.MyBatisDao;
import com.thinkgem.jeesite.modules.homepage.bean.AdvRequest;
import com.thinkgem.jeesite.modules.homepage.bean.NoticeRequest;
import com.thinkgem.jeesite.modules.homepage.entity.AdvEntity;
import com.thinkgem.jeesite.modules.homepage.entity.NoticeEntity;
import java.util.List;
@MyBatisDao
public interface NoticeDao {
List<NoticeEntity> getNotice(NoticeRequest request);
}
package com.thinkgem.jeesite.modules.homepage.entity;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.thinkgem.jeesite.common.baseBean.BaseEntity;
import java.io.Serializable;
......@@ -11,9 +12,15 @@ public class AdvEntity extends BaseEntity implements Serializable {
private String href; //链接地址
private String target; //打开目标
private String picture; //图片地址
@JsonIgnore
private String weight; //权重
@JsonIgnore
private String status; //状态
private String clicks; //点击次数
@JsonIgnore
private String expireTime; //过期时间
@JsonIgnore
private String beginTime; //生效时间
public String getType() {
return type;
......@@ -78,4 +85,20 @@ public class AdvEntity extends BaseEntity implements Serializable {
public void setClicks(String clicks) {
this.clicks = clicks;
}
public String getExpireTime() {
return expireTime;
}
public void setExpireTime(String expireTime) {
this.expireTime = expireTime;
}
public String getBeginTime() {
return beginTime;
}
public void setBeginTime(String beginTime) {
this.beginTime = beginTime;
}
}
package com.thinkgem.jeesite.modules.homepage.entity;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.thinkgem.jeesite.common.baseBean.BaseEntity;
import java.io.Serializable;
public class NoticeEntity extends BaseEntity implements Serializable {
private String title; // 标题
private String image; // 图片
private String keywords; // 关键字
private String description; // 摘要
@JsonIgnore
private String weight; // 权重
@JsonIgnore
private String hits; // 点击数
@JsonIgnore
private String is_flow; // 是否滚动通知
@JsonIgnore
private String is_tip; // 是否弹窗通知
private String content; // 内容
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getImage() {
return image;
}
public void setImage(String image) {
this.image = image;
}
public String getKeywords() {
return keywords;
}
public void setKeywords(String keywords) {
this.keywords = keywords;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getWeight() {
return weight;
}
public void setWeight(String weight) {
this.weight = weight;
}
public String getHits() {
return hits;
}
public void setHits(String hits) {
this.hits = hits;
}
public String getIs_flow() {
return is_flow;
}
public void setIs_flow(String is_flow) {
this.is_flow = is_flow;
}
public String getIs_tip() {
return is_tip;
}
public void setIs_tip(String is_tip) {
this.is_tip = is_tip;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
}
......@@ -19,7 +19,7 @@ public class AdvService {
@Autowired
private AdvDao advDao;
public List<AdvEntity> getAdvList(AdvRequest request) {
public List<AdvEntity> getAdvList(AdvRequest request) throws Exception {
return advDao.getAdvList(request);
}
}
package com.thinkgem.jeesite.modules.homepage.service;
import com.thinkgem.jeesite.modules.homepage.bean.AdvRequest;
import com.thinkgem.jeesite.modules.homepage.bean.NoticeRequest;
import com.thinkgem.jeesite.modules.homepage.dao.AdvDao;
import com.thinkgem.jeesite.modules.homepage.dao.NoticeDao;
import com.thinkgem.jeesite.modules.homepage.entity.AdvEntity;
import com.thinkgem.jeesite.modules.homepage.entity.NoticeEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
/**
* 轮播图
*/
@Service
@Transactional(readOnly = true)
public class NoticeService {
@Autowired
private NoticeDao noticeDao;
public List<NoticeEntity> getNotice(NoticeRequest request) throws Exception {
return noticeDao.getNotice(request);
}
}
......@@ -3,22 +3,23 @@
<mapper namespace="com.thinkgem.jeesite.modules.homepage.dao.AdvDao">
<select id="getAdvList" resultType="com.thinkgem.jeesite.modules.homepage.entity.AdvEntity">
SELECT a.id,
a.code,
a.type,
a.title,
a.href,
a.target,
a.picture,
a.weight,
a.status,
a.clicks,
a.expire_time,
a.begin_time,
a.created,
a.created_user,
a.modified,
a.modified_user
SELECT
a.id,
a.code,
a.type,
a.title,
a.href,
a.target,
a.picture,
a.weight,
a.status,
a.clicks,
a.expire_time AS expireTime,
a.begin_time AS beginTime,
a.create_date AS created,
a.create_by AS createdUser,
a.update_date AS modified,
a.update_by AS modifiedUser
FROM youka_adv a
WHERE a.status = 1
<![CDATA[ AND (a.begin_time <= NOW() OR a.begin_time IS NULL)]]>
......
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.thinkgem.jeesite.modules.homepage.dao.NoticeDao">
<select id="getNotice" resultType="com.thinkgem.jeesite.modules.homepage.entity.NoticeEntity" parameterType="com.thinkgem.jeesite.modules.homepage.bean.NoticeRequest">
SELECT
a.id,
a.title,
a.image,
a.keywords,
a.description,
a.weight,
a.hits,
a.is_flow,
a.is_tip,
a.create_by,
a.create_date,
a.update_by,
a.update_date,
b.content
FROM
cms_article a
LEFT JOIN cms_article_data b ON a.id = b.id
WHERE
<if test='noticeType=="F"'>
a.is_flow = 'Y'
</if>
<if test='noticeType=="T"'>
a.is_tip = 'Y'
</if>
<if test='noticeType!="T" and noticeType!="F"'>
1=2
</if>
ORDER BY
a.weight DESC
</select>
</mapper>
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment