Jfinal 与 spring boot 整合问题

我将Jfinal 与 spring boot 整合了。在返回数据的时候,如何不返回columnNames 和 columnValues

评论区

JFinal

2020-03-25 16:43

猜不到在问什么

chcode

2020-03-25 17:02

一看就是record 对象转json 的问题,需要参考jfinal源码配置下HttpMessageConverter springboot默认是Jackson

abcdefg

2020-03-25 18:03

@JFinal就是返回List 时,页面接收到columnNames 和 columnValues,导致Layui table 无法显示。

JFinal

2020-03-25 18:11

@abcdefg 没有代码,不好猜

建议先研究一下如何更好地提问

关键是站在回答者的角度,同理心是核心

xialinlin

2020-03-26 08:55

如果是:Jackson的话配置一下即可

import java.io.IOException;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.jfinal.plugin.activerecord.Record;

public class RecordJackSonSerializer extends JsonSerializer {

@Override
public void serialize(Record record, JsonGenerator gen, SerializerProvider serializers) throws IOException {
gen.writeObject(record.getColumns());
}
}


======================================================

import java.io.IOException;

import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;
import org.zalando.problem.ProblemModule;
import org.zalando.problem.violations.ConstraintViolationProblemModule;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.datatype.jdk8.Jdk8Module;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import com.fasterxml.jackson.module.afterburner.AfterburnerModule;
import com.jfinal.plugin.activerecord.Record;

@Configuration
public class JacksonConfiguration {

/**
* Support for Java date and time API.
* @return the corresponding Jackson module.
*/
@Bean
public JavaTimeModule javaTimeModule() {
return new JavaTimeModule();
}

@Bean
public Jdk8Module jdk8TimeModule() {
return new Jdk8Module();
}

@Bean
@Primary
@ConditionalOnMissingBean(ObjectMapper.class)
public ObjectMapper jacksonObjectMapper(Jackson2ObjectMapperBuilder builder) {
ObjectMapper objectMapper = builder.createXmlMapper(false).build();
SimpleModule simpleModule = new SimpleModule();
simpleModule.addSerializer(Record.class,new RecordJackSonSerializer());
objectMapper.registerModule(simpleModule);

objectMapper.getSerializerProvider().setNullValueSerializer(new JsonSerializer() {
@Override
public void serialize(Object o, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException, JsonProcessingException {
jsonGenerator.writeString("");
}
});
return objectMapper;
}


/*
* Jackson Afterburner module to speed up serialization/deserialization.
*/
@Bean
public AfterburnerModule afterburnerModule() {
return new AfterburnerModule();
}

/*
* Module for serialization/deserialization of RFC7807 Problem.
*/
@Bean
ProblemModule problemModule() {
return new ProblemModule();
}

/*
* Module for serialization/deserialization of ConstraintViolationProblem.
*/
@Bean
ConstraintViolationProblemModule constraintViolationProblemModule() {
return new ConstraintViolationProblemModule();
}

}