Example usage for com.fasterxml.jackson.module.jaxb JaxbAnnotationIntrospector JaxbAnnotationIntrospector

List of usage examples for com.fasterxml.jackson.module.jaxb JaxbAnnotationIntrospector JaxbAnnotationIntrospector

Introduction

In this page you can find the example usage for com.fasterxml.jackson.module.jaxb JaxbAnnotationIntrospector JaxbAnnotationIntrospector.

Prototype

public JaxbAnnotationIntrospector(TypeFactory typeFactory) 

Source Link

Usage

From source file:com.linecorp.platform.channel.sample.Main.java

public static void main(String[] args) {

    BusinessConnect bc = new BusinessConnect();

    /**//from  w  w w  .ja  v a  2s.c o  m
     * Prepare the required channel secret and access token
     */
    String channelSecret = System.getenv("CHANNEL_SECRET");
    String channelAccessToken = System.getenv("CHANNEL_ACCESS_TOKEN");
    if (channelSecret == null || channelSecret.isEmpty() || channelAccessToken == null
            || channelAccessToken.isEmpty()) {
        System.err.println("Error! Environment variable CHANNEL_SECRET and CHANNEL_ACCESS_TOKEN not defined.");
        return;
    }

    port(Integer.valueOf(System.getenv("PORT")));
    staticFileLocation("/public");

    /**
     * Define the callback url path
     */
    post("/events", (request, response) -> {
        String requestBody = request.body();

        /**
         * Verify whether the channel signature is valid or not
         */
        String channelSignature = request.headers("X-LINE-CHANNELSIGNATURE");
        if (channelSignature == null || channelSignature.isEmpty()) {
            response.status(400);
            return "Please provide valid channel signature and try again.";
        }
        if (!bc.validateBCRequest(requestBody, channelSecret, channelSignature)) {
            response.status(401);
            return "Invalid channel signature.";
        }

        /**
         * Parse the http request body
         */
        ObjectMapper objectMapper = new ObjectMapper();
        objectMapper.configure(MapperFeature.USE_WRAPPER_NAME_AS_PROPERTY_NAME, true);
        objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
        objectMapper.configure(SerializationFeature.WRITE_NULL_MAP_VALUES, false);
        objectMapper.setAnnotationIntrospector(new JaxbAnnotationIntrospector(TypeFactory.defaultInstance()));
        objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);

        EventList events;
        try {
            events = objectMapper.readValue(requestBody, EventList.class);
        } catch (IOException e) {
            response.status(400);
            return "Invalid request body.";
        }

        ApiHttpClient apiHttpClient = new ApiHttpClient(channelAccessToken);

        /**
         * Process the incoming messages/operations one by one
         */
        List<String> toUsers;
        for (Event event : events.getResult()) {
            switch (event.getEventType()) {
            case Constants.EventType.MESSAGE:
                toUsers = new ArrayList<>();
                toUsers.add(event.getContent().getFrom());

                // @TODO: We strongly suggest you should modify this to process the incoming message/operation async
                bc.sendTextMessage(toUsers, "You said: " + event.getContent().getText(), apiHttpClient);
                break;
            case Constants.EventType.OPERATION:
                if (event.getContent().getOpType() == Constants.OperationType.ADDED_AS_FRIEND) {
                    String newFriend = event.getContent().getParams().get(0);
                    Profile profile = bc.getProfile(newFriend, apiHttpClient);
                    String displayName = profile == null ? "Unknown" : profile.getDisplayName();
                    toUsers = new ArrayList<>();
                    toUsers.add(newFriend);
                    bc.sendTextMessage(toUsers, displayName + ", welcome to be my friend!", apiHttpClient);
                    Connection connection = null;
                    connection = DatabaseUrl.extract().getConnection();
                    toUsers = bc.getFriends(newFriend, connection);
                    if (toUsers.size() > 0) {
                        bc.sendTextMessage(toUsers, displayName + " just join us, let's welcome him/her!",
                                apiHttpClient);
                    }
                    bc.addFriend(newFriend, displayName, connection);
                    if (connection != null) {
                        connection.close();
                    }
                }
                break;
            default:
                // Unknown type?
            }
        }
        return "Events received successfully.";
    });

    get("/", (request, response) -> {
        Map<String, Object> attributes = new HashMap<>();
        attributes.put("message", "Hello World!");
        return new ModelAndView(attributes, "index.ftl");
    }, new FreeMarkerEngine());
}

From source file:com.uimirror.core.framework.rest.util.ObjectMapperFactory.java

private static AnnotationIntrospector getAnnotationPair() {
    final AnnotationIntrospector primary = new JacksonAnnotationIntrospector();
    final AnnotationIntrospector secondary = new JaxbAnnotationIntrospector(TypeFactory.defaultInstance());
    return AnnotationIntrospector.pair(primary, secondary);
}

From source file:info.archinnov.achilles.json.DefaultJacksonMapper.java

private static ObjectMapper defaultJacksonMapper() {
    ObjectMapper defaultMapper = new ObjectMapper();
    defaultMapper.configure(MapperFeature.SORT_PROPERTIES_ALPHABETICALLY, true);
    defaultMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
    defaultMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
    AnnotationIntrospector primary = new JacksonAnnotationIntrospector();
    AnnotationIntrospector secondary = new JaxbAnnotationIntrospector(TypeFactory.defaultInstance());
    defaultMapper.setAnnotationIntrospector(AnnotationIntrospector.pair(primary, secondary));
    return defaultMapper;
}

From source file:com.faraox.rest.poc.provider.MyObjectMapperProvider.java

private static AnnotationIntrospector createJaxbJacksonAnnotationIntrospector() {

    final AnnotationIntrospector jaxbIntrospector = new JaxbAnnotationIntrospector(
            TypeFactory.defaultInstance());
    final AnnotationIntrospector jacksonIntrospector = new JacksonAnnotationIntrospector();

    return AnnotationIntrospector.pair(jacksonIntrospector, jaxbIntrospector);
}

From source file:com.devnexus.ting.web.JaxbJacksonObjectMapper.java

public JaxbJacksonObjectMapper() {
    final AnnotationIntrospector introspector = new JaxbAnnotationIntrospector(TypeFactory.defaultInstance());
    super.setAnnotationIntrospector(introspector);
}

From source file:com.yoshio3.utils.MyObjectMapperProvider.java

private static AnnotationIntrospector createJaxbJacksonAnnotationIntrospector() {
    final AnnotationIntrospector jaxbIntrospector = new JaxbAnnotationIntrospector(
            TypeFactory.defaultInstance());
    final AnnotationIntrospector jacksonIntrospector = new JacksonAnnotationIntrospector();
    return AnnotationIntrospector.pair(jacksonIntrospector, jaxbIntrospector);
}

From source file:com.devnexus.ting.core.model.SpeakerJacksonTest.java

@Test
@Ignore//www  .j av a  2s. c  o  m
public void testJacksonSerialization() throws JsonGenerationException, JsonMappingException, IOException {

    ObjectMapper mapper = new ObjectMapper();
    AnnotationIntrospector introspector = new JaxbAnnotationIntrospector(TypeFactory.defaultInstance());
    // make deserializer use JAXB annotations (only)
    mapper.setAnnotationIntrospector(introspector);
    // make serializer use JAXB annotations (only)
    mapper.setAnnotationIntrospector(introspector);

    Speaker speaker = new Speaker();

    speaker.setBio("bio");
    speaker.setFirstName("firstName");
    speaker.setLastName("lastName");

    mapper.writeValue(System.out, speaker);

}

From source file:ru.anr.base.services.serializer.JSONSerializerImpl.java

/**
 * Constructor. We can use both JAXB and Jackson annotations for mapping.
 *///from ww w . j a  v a2  s .  com
public JSONSerializerImpl() {

    super(new ObjectMapper());

    // mapper().configure(SerializationFeature.WRAP_ROOT_VALUE, true);
    // mapper().configure(DeserializationFeature.UNWRAP_ROOT_VALUE, true);

    AnnotationIntrospector introspector = new JaxbAnnotationIntrospector(mapper().getTypeFactory());
    AnnotationIntrospector secondary = new JacksonAnnotationIntrospector();

    mapper().setAnnotationIntrospector(new AnnotationIntrospectorPair(introspector, secondary));
}

From source file:com.infinities.keystone4j.utils.jackson.JacksonProvider.java

public JacksonProvider() {
    AnnotationIntrospector introspector = new JaxbAnnotationIntrospector(TypeFactory.defaultInstance());
    // if using BOTH JAXB annotations AND Jackson annotations:
    AnnotationIntrospector secondary = new JacksonAnnotationIntrospector();

    ObjectMapper mapper = new ObjectMapper().registerModule(new Hibernate4Module())
            .setSerializationInclusion(Include.NON_NULL)
            .configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false)
            .enable(SerializationFeature.INDENT_OUTPUT)
            .setAnnotationIntrospector(new AnnotationIntrospectorPair(introspector, secondary));
    // mapper = mapper.setSerializationInclusion(Include)
    setMapper(mapper);//from  w  w w  .  j av  a2  s  .co  m
}

From source file:com.infinities.nova.util.jackson.JacksonProvider.java

public JacksonProvider() {
    AnnotationIntrospector introspector = new JaxbAnnotationIntrospector(TypeFactory.defaultInstance());
    // if using BOTH JAXB annotations AND Jackson annotations:
    AnnotationIntrospector secondary = new JacksonAnnotationIntrospector();

    ObjectMapper mapper = new ObjectMapper().registerModule(new Hibernate4Module())
            .configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false)
            .setDateFormat(new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'"))
            .enable(SerializationFeature.INDENT_OUTPUT)
            .configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
            .setAnnotationIntrospector(new AnnotationIntrospectorPair(introspector, secondary));
    NullStringSerializer serializer = new NullStringSerializer();

    SimpleModule module = new SimpleModule("NullToNoneDeserializer");
    module.addSerializer(String.class, serializer);
    mapper.registerModule(module);/* w  ww  .  java2s  . com*/
    // mapper = mapper.setSerializationInclusion(Include)
    setMapper(mapper);
}