I am porting jackson 1.6 code to jackson 2 and stumbled upon a deprecated code.
What i did in jackson 1.6 is:
CustomDeserializerFactory sf = new CustomDeserializerFactory();
mapper.setDeserializerProvider(new StdDeserializerProvider(sf));
sf.addSpecificMapping(BigDecimal.class, new BigDecimalDeserializer());
t = mapper.readValue(ts, X[].class);
Anyone knows how to do it in jackson 2?
To add a factory--not just a deserializer--don't use SimpleModule. Create your own Module and within it create a Deserializers object that is added to the SetUpContext. The Deserializers object will have access to similar methods that the factory did where you can get extra type information about the deserializer needed.
It will look something like this (note that it doesn't need to be an inner class):
public class MyCustomCollectionModule extends Module {
#Override
public void setupModule(final SetupContext context) {
context.addDeserializers(new MyCustomCollectionDeserializers());
}
private static class MyCustomCollectionDeserializers implements Deserializers {
...
#Override
public JsonDeserializer<?> findCollectionDeserializer(final CollectionType type, final DeserializationConfig config, final BeanDescription beanDesc, final TypeDeserializer elementTypeDeserializer, final JsonDeserializer<?> elementDeserializer) throws JsonMappingException {
if (MyCustomCollection.class.equals(type.getRawClass())) {
return new MyCustomCollectionDeserializer(type);
}
return null;
}
...
}
}
In Jackson 2.0:
Create a Module (usually SimpleModule)
Register custom handlers with it.
Call ObjectMapper.registerModule(module);.
This is available on Jackson 1.x as well (since 1.8 or so).
Here is an example of registering a module (in this case Joda date handling) in Jackson 2.x:
ClientConfig clientConfig = new DefaultClientConfig();
ObjectMapper mapper = new ObjectMapper();
mapper.registerModule(new JodaModule());
mapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
JacksonJsonProvider provider = new JacksonJsonProvider();
provider.configure(SerializationFeature.INDENT_OUTPUT, true);
provider.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
provider.setMapper(mapper);
clientConfig.getSingletons().add(provider);
Client client = Client.create(clientConfig);
Exemplifying #StaxMan answer
Basically you need to create a module (SimpleModule), add a deserializer and register this module
final SimpleModule sm = new SimpleModule();
sm.addDeserializer(Date.class, new JsonDeserializer<Date>(){
#Override
public Date deserialize(JsonParser p, DeserializationContext ctxt)
throws IOException {
try {
System.out.println("from my custom deserializer!!!!!!");
return new SimpleDateFormat("yyyy-MM-dd").parse(p.getValueAsString());
} catch (ParseException e) {
System.err.println("aw, it fails: " + e.getMessage());
throw new IOException(e.getMessage());
}
}
});
final CreationBean bean = JsonUtils.getMapper()
.registerModule(sm)
// .setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"))
.readValue("{\"dateCreation\": \"1995-07-19\"}", CreationBean.class);
Here a fully example
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.module.SimpleModule;
/**
* #author elvis
* #version $Revision: $<br/>
* $Id: $
* #since 8/22/16 8:38 PM
*/
public class JackCustomDeserializer {
public static void main(String[] args) throws IOException {
final SimpleModule sm = new SimpleModule();
sm.addDeserializer(Date.class, new JsonDeserializer<Date>(){
#Override
public Date deserialize(JsonParser p, DeserializationContext ctxt)
throws IOException {
try {
System.out.println("from my custom deserializer!!!!!!");
return new SimpleDateFormat("yyyy-MM-dd").parse(p.getValueAsString());
} catch (ParseException e) {
System.err.println("aw, it fails: " + e.getMessage());
throw new IOException(e.getMessage());
}
}
});
final CreationBean bean = JsonUtils.getMapper()
.registerModule(sm)
// .setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"))
.readValue("{\"dateCreation\": \"1995-07-19\"}", CreationBean.class);
System.out.println("parsed bean: " + bean.dateCreation);
}
static class CreationBean {
public Date dateCreation;
}
}
Related
My situation asks for a bit more complex serialisation. I have a class Available (this is a very simplified snippet):
public class Available<T> {
private T value;
private boolean available;
...
}
So a POJO
class Tmp {
private Available<Integer> myInt = Available.of(123);
private Available<Integer> otherInt = Available.clean();
...
}
would normally result in
{"myInt":{available:true,value:123},"otherInt":{available:false,value:null}}
However, I want a serialiser to render the same POJO like this:
{"myInt":123}
What I have now:
public class AvailableSerializer extends JsonSerializer<Available<?>> {
#Override
public void serialize(Available<?> available, JsonGenerator jsonGenerator, SerializerProvider provider) throws IOException, JsonProcessingException {
if (available != null && available.isAvailable()) {
jsonGenerator.writeObject(available.getValue());
}
// MISSING: nothing at all should be rendered here for the field
}
#Override
public Class<Available<?>> handledType() {
#SuppressWarnings({ "unchecked", "rawtypes" })
Class<Available<?>> clazz = (Class) Available.class;
return clazz;
}
}
A test
#Test
public void testSerialize() throws Exception {
SimpleModule module = new SimpleModule().addSerializer(new AvailableSerializer());
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.registerModule(module);
System.out.println(objectMapper.writeValueAsString(new Tmp()));
}
outputs
{"myInt":123,"otherInt"}
Can anyone tell me how to do the "MISSING"-stuff? Or if I'm doing it all wrong, how do I do it then?
The restriction I have is that I don't want the developers to add #Json...-annotations all the time to fields of type Available. So the Tmp-class above is an example of what a typical using class should look like. If that's possible...
Include.NON_DEFAULT
If we assume that your clean method is implemented in this way:
class Available<T> {
public static final Available<Object> EMPTY = clean();
//....
#SuppressWarnings("unchecked")
static <T> Available<T> clean() {
return (Available<T>) EMPTY;
}
}
You can set serialisation inclusion to JsonInclude.Include.NON_DEFAULT value and it should skip values set to EMPTY (default) values. See below example:
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.core.JsonGenerator;
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.databind.ser.BeanPropertyWriter;
import java.io.IOException;
public class JsonApp {
public static void main(String[] args) throws Exception {
SimpleModule module = new SimpleModule();
module.addSerializer(new AvailableSerializer());
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.registerModule(module);
objectMapper.setSerializationInclusion(JsonInclude.Include.NON_DEFAULT);
System.out.println(objectMapper.writeValueAsString(new Tmp()));
}
}
class AvailableSerializer extends JsonSerializer<Available<?>> {
#Override
public void serialize(Available<?> value, JsonGenerator jsonGenerator, SerializerProvider provider) throws IOException {
jsonGenerator.writeObject(value.getValue());
}
#Override
#SuppressWarnings({"unchecked", "rawtypes"})
public Class<Available<?>> handledType() {
return (Class) Available.class;
}
}
Above code prints:
{"myInt":123}
Custom BeanPropertyWriter
If you do not want to use Include.NON_DEFAULT you can write your custom BeanPropertyWriter and skip all values you want. See below example:
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.BeanDescription;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationConfig;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.databind.ser.BeanPropertyWriter;
import com.fasterxml.jackson.databind.ser.BeanSerializerModifier;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class JsonApp {
public static void main(String[] args) throws Exception {
SimpleModule module = new SimpleModule();
module.addSerializer(new AvailableSerializer());
module.setSerializerModifier(new BeanSerializerModifier() {
#Override
public List<BeanPropertyWriter> changeProperties(SerializationConfig config, BeanDescription beanDesc, List<BeanPropertyWriter> beanProperties) {
List<BeanPropertyWriter> writers = new ArrayList<>(beanProperties.size());
for (BeanPropertyWriter writer : beanProperties) {
if (writer.getType().getRawClass() == Available.class) {
writer = new SkipNotAvailableBeanPropertyWriter(writer);
}
writers.add(writer);
}
return writers;
}
});
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.registerModule(module);
System.out.println(objectMapper.writeValueAsString(new Tmp()));
}
}
class AvailableSerializer extends JsonSerializer<Available<?>> {
#Override
public void serialize(Available<?> value, JsonGenerator jsonGenerator, SerializerProvider provider) throws IOException {
jsonGenerator.writeObject(value.getValue());
}
#Override
#SuppressWarnings({"unchecked", "rawtypes"})
public Class<Available<?>> handledType() {
return (Class) Available.class;
}
}
class SkipNotAvailableBeanPropertyWriter extends BeanPropertyWriter {
SkipNotAvailableBeanPropertyWriter(BeanPropertyWriter base) {
super(base);
}
#Override
public void serializeAsField(Object bean, JsonGenerator gen, SerializerProvider prov) throws Exception {
// copier from super.serializeAsField(bean, gen, prov);
final Object value = (_accessorMethod == null) ? _field.get(bean) : _accessorMethod.invoke(bean, (Object[]) null);
if (value == null || value instanceof Available && !((Available) value).isAvailable()) {
return;
}
super.serializeAsField(bean, gen, prov);
}
}
Above code prints:
{"myInt":123}
After Michał Ziober's answer I had to look for something regarding Include.NON_DEFAULT and the default object and ran into this answer explaining Include.NON_EMPTY that Google didn't return in my first research (thanks Google).
So things become easier, it's now:
public class AvailableSerializer extends JsonSerializer<Available<?>> {
#Override
public void serialize(Available<?> available, JsonGenerator jsonGenerator, SerializerProvider provider) throws IOException, JsonProcessingException {
jsonGenerator.writeObject(available.getValue());
}
#Override
public Class<Available<?>> handledType() {
#SuppressWarnings({ "unchecked", "rawtypes" })
Class<Available<?>> clazz = (Class) Available.class;
return clazz;
}
#Override
public boolean isEmpty(SerializerProvider provider, Available<?> value) {
return value == null || !value.isAvailable();
}
}
with the test
#Test
public void testSerialize() throws Exception {
SimpleModule module = new SimpleModule().addSerializer(availableSerializer);
objectMapper.registerModule(module);
objectMapper.configOverride(Available.class).setInclude(
// the call comes from JavaDoc of objectMapper.setSerializationInclusion(...)
JsonInclude.Value.construct(JsonInclude.Include.NON_EMPTY, JsonInclude.Include.ALWAYS));
Tmp tmp = new Tmp();
assertThat(objectMapper.writeValueAsString(tmp)).isEqualTo("{\"myInt\":123}");
tmp.otherInt.setValue(123);
assertThat(objectMapper.writeValueAsString(tmp)).isEqualTo("{\"myInt\":123,\"otherInt\":123}");
}
So please, if you upvote my answer please also upvote Michał Ziober's as that's also working with a mildly different approach.
I am new to Camel and I am facing an issue while sending files to webservice via camel http.
I have a rest web service which consumes Multipart form data type content and accepts input as part of form data.
When I send file and form parameter via camel it gives me the following error at camel console:
Stacktrace
---------------------------------------------------------------------------------------------------------------------------------------
org.apache.camel.component.http.HttpOperationFailedException: HTTP operation failed invoking http://localhost:8080/JAX_RS_Application/resource/restwb/upload with statusCode: 415
at org.apache.camel.component.http.HttpProducer.populateHttpOperationFailedException(HttpProducer.java:230)
at org.apache.camel.component.http.HttpProducer.process(HttpProducer.java:156)
at org.apache.camel.util.AsyncProcessorConverterHelper$ProcessorToAsyncProcessorBridge.process(AsyncProcessorConverterHelper.java:61)
at org.apache.camel.processor.SendProcessor.process(SendProcessor.java:129)
at org.apache.camel.management.InstrumentationProcessor.process(InstrumentationProcessor.java:77)
at org.apache.camel.processor.RedeliveryErrorHandler.process(RedeliveryErrorHandler.java:448)
at org.apache.camel.processor.CamelInternalProcessor.process(CamelInternalProcessor.java:191)
at org.apache.camel.processor.Pipeline.process(Pipeline.java:118)
at org.apache.camel.processor.Pipeline.process(Pipeline.java:80)
at org.apache.camel.processor.CamelInternalProcessor.process(CamelInternalProcessor.java:191)
at org.apache.camel.component.file.GenericFileConsumer.processExchange(GenericFileConsumer.java:435)
at org.apache.camel.component.file.GenericFileConsumer.processBatch(GenericFileConsumer.java:211)
at org.apache.camel.component.file.GenericFileConsumer.poll(GenericFileConsumer.java:175)
at org.apache.camel.impl.ScheduledPollConsumer.doRun(ScheduledPollConsumer.java:174)
at org.apache.camel.impl.ScheduledPollConsumer.run(ScheduledPollConsumer.java:101)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:471)
at java.util.concurrent.FutureTask.runAndReset(FutureTask.java:304)
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$301(ScheduledThreadPoolExecutor.java:178)
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:293)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at java.lang.Thread.run(Thread.java:745)
The error i get on the server side console is as follows:
SEVERE: MessageBodyReader not found for media type=application/octet-stream, typ
e=class org.glassfish.jersey.media.multipart.FormDataMultiPart, genericType=clas
s org.glassfish.jersey.media.multipart.FormDataMultiPart.
The code snippet of the Rest web-service created via jersey is as follows:
import java.io.IOException;
import java.io.InputStream;
import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;
import org.apache.commons.io.IOUtils;
import org.glassfish.jersey.media.multipart.FormDataContentDisposition;
import org.glassfish.jersey.media.multipart.FormDataParam;
#Path("/restwb")
public class FileResource {
#POST
#Path("/upload")
#Consumes(MediaType.MULTIPART_FORM_DATA)
public String uploadFile(#FormDataParam("username") String username,#FormDataParam("password") String password,#FormDataParam("upload") InputStream is) {
String output ="Hi "+username+" your password is "+password;
output=output+IOUtils.LINE_SEPARATOR +IOUtils.LINE_SEPARATOR;
output=output+"Output :"+IOUtils.LINE_SEPARATOR+"------------------------------------------------------------------------------"+IOUtils.LINE_SEPARATOR;
try {
output=output+IOUtils.toString(is)+IOUtils.LINE_SEPARATOR+IOUtils.LINE_SEPARATOR;
output=output+"==================================================================================================="+IOUtils.LINE_SEPARATOR+IOUtils.LINE_SEPARATOR;
System.out.println("Output :"+output);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return output;
}
}
And my Camel config is as follows:
import org.apache.camel.*;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.impl.DefaultCamelContext;
import org.apache.camel.spi.Synchronization;
import org.apache.camel.spi.UnitOfWork;
import org.apache.http.HttpEntity;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.mime.HttpMultipartMode;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.entity.mime.content.StringBody;
import org.apache.james.mime4j.message.Multipart;
import org.apache.log4j.Logger;
import java.io.File;
import java.io.InputStream;
import java.util.List;
import java.util.Map;
/**
* Created by Manish.Pillai on 7/16/2015.
*/
public class LoggingMain {
private static final Logger logger =Logger.getLogger(LoggingMain.class);
public static void main(String[] args) throws Exception{
CamelContext camelContext =new DefaultCamelContext();
try {
camelContext.addRoutes(new RouteBuilder() {
#Override
public void configure() throws Exception {
from("file:C:\\temp?delay=5000&move=processed&moveFailed=error&antExclude=**/processed/**,**/error/**")
.process(new Processor() {
public void process(Exchange exchange) throws Exception {
exchange.getContext().getTypeConverterRegistry().addTypeConverter(HttpEntity.class,InputStream.class,new InputStreamToHttpEntityConvertor());
exchange.getOut().setBody(exchange.getIn().getBody(),HttpEntity.class);
}
})
.to("http://localhost:8080/JAX_RS_Application/resource/restwb/upload");
}
});
camelContext.getRestConfiguration();
camelContext.start();
Thread.sleep(5000);
camelContext.stop();
} catch (Exception e) {
logger.error(e.getMessage());
}
}
static class InputStreamToHttpEntityConvertor implements TypeConverter {
public boolean allowNull() {
return false;
}
public <T> T convertTo(Class<T> type, Object value) throws TypeConversionException {
Exchange exchange=(Exchange)value;
StringBody username = new StringBody("username", ContentType.MULTIPART_FORM_DATA);
StringBody password = new StringBody("password", ContentType.MULTIPART_FORM_DATA);
MultipartEntityBuilder multipartEntityBuilder=MultipartEntityBuilder.create();
multipartEntityBuilder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
multipartEntityBuilder.addPart("upload", new FileBody(exchange.getIn().getBody(File.class), ContentType.MULTIPART_FORM_DATA, (String) exchange.getIn().getHeader(Exchange.FILE_NAME)));
multipartEntityBuilder.addPart("username",username);
multipartEntityBuilder.addPart("password",password);
return (T)multipartEntityBuilder.build();
}
public <T> T convertTo(Class<T> aClass, Exchange exchange, Object o) throws TypeConversionException {
return convertTo(aClass,o);
}
public <T> T mandatoryConvertTo(Class<T> type, Object value) throws TypeConversionException, NoTypeConversionAvailableException {
return convertTo(type,value);
}
public <T> T mandatoryConvertTo(Class<T> type, Exchange exchange, Object value) throws TypeConversionException, NoTypeConversionAvailableException {
return convertTo(type,value);
}
public <T> T tryConvertTo(Class<T> type, Object value) {
return convertTo(type,value);
}
public <T> T tryConvertTo(Class<T> type, Exchange exchange, Object value) {
return convertTo(type,value);
}
}
}
Any leads would be helpful.
Well, there are several things that can be improved in your code.
First, since you are using a MultipartEntityBuilder, that means you're using Apache's HttpClient version 4.3+, so for best compatibility you should use Camel's HTTP4 component.
Third, in an example as small as this, you don't really need to use the converter, you can do something like this:
public class LoggingMain {
private static final Logger logger = Logger.getLogger(LoggingMain.class);
public static void main(String[] args) throws Exception {
CamelContext camelContext = new DefaultCamelContext();
try {
camelContext.addRoutes(new RouteBuilder() {
#Override
public void configure() throws Exception {
from("file:C:\\temp?delay=5000&move=processed&moveFailed=error&antExclude=**/processed/**,**/error/**")
.process(new Processor() {
public void process(Exchange exchange) throws Exception {
StringBody username = new StringBody("username", ContentType.MULTIPART_FORM_DATA);
StringBody password = new StringBody("password", ContentType.MULTIPART_FORM_DATA);
MultipartEntityBuilder multipartEntityBuilder = MultipartEntityBuilder.create();
multipartEntityBuilder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
multipartEntityBuilder.addPart("username", username);
multipartEntityBuilder.addPart("password", password);
String filename = (String) exchange.getIn().getHeader(Exchange.FILE_NAME);
File file = exchange.getIn().getBody(File.class);
multipartEntityBuilder.addPart("upload", new FileBody(file, ContentType.MULTIPART_FORM_DATA, filename));
exchange.getIn().setBody(multipartEntityBuilder.build());
}
})
.to("http4://localhost:8080/JAX_RS_Application/resource/restwb/upload");
}
});
camelContext.getRestConfiguration();
camelContext.start();
Thread.sleep(5000);
camelContext.stop();
} catch (Exception e) {
logger.error(e.getMessage());
}
}
}
I hope this helps!
In my spring boot application, I have multiple Rest Controllers and need to generate swagger for each controller seperately.
By using below Docket config for each controller in my spring boot application class, i am able to download controller specific swagger by going to /v2/api-docs?group=ai where i = 1 to n
However in swagger-ui.html, when i select a1(/v2/api-docs?group=a1), it shows path as "/api/a1/a1", while selecting a2(/v2/api-docs?greoup=a2), it shows correct path i.e. /api/a2
I have tried changing in Docket ,paths regex to absolute e.g. "api/a1" etc but that didn't help.
#Bean
public Docket a1Api() {
return new Docket(DocumentationType.SWAGGER_2)
.groupName("a1")
.apiInfo(a1Info())
.select().apis(RequestHandlerSelectors.any())
.paths(regex("/api/a1.*"))
.build()
.pathMapping("/");
}
#Bean
public Docket a2Api() {
return new Docket(DocumentationType.SWAGGER_2)
.groupName("a2")
.apiInfo(a1Info())
.select().apis(RequestHandlerSelectors.any())
.paths(regex("/api/a2.*"))
.build()
.pathMapping("/");
}
private ApiInfo a1Info() {
return new ApiInfoBuilder()
.title("a1 Swagger 2.0")
.description("a1")
.license("a1")
.version("1.0")
.build();
}
private ApiInfo a2Info() {
return new ApiInfoBuilder()
.title("a2 Swagger 2.0")
.description("a2")
.license("a2")
.version("1.0")
.build();
}
Rest Controllers
#RestController
#Api(tags = "A1")
#RequestMapping("/api/a1")
public class a1Controller {
#ApiOperation(value = "a1")
#RequestMapping(value = "", method = RequestMethod.POST)
public a1Response invoke(#RequestBody a1Request va1Request) {
.....;
}
}
#RestController
#Api(tags = "An")
#RequestMapping("/api/an")
public class a1Controller {
#ApiOperation(value = "an")
#RequestMapping(value = "", method = RequestMethod.POST)
public anResponse invoke(#RequestBody anRequest vanRequest) {
.....;
}
}
Any idea how can i address this....
i am using springfox swagger version 2.6.1
You can add multiple controller class using following Swagger Configuration:
1) Create a Swagger Configuration Class.
2) Then specify the base package of controllers.
import java.util.Collections;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import com.google.common.base.Predicate;
import com.google.common.base.Predicates;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
#Configuration
#EnableSwagger2
public class SwaggerConfig
{
private static final ApiInfo DEFAULT_API_INFO = null; //Swagger info
#Bean
public Docket api()
{
return new Docket(DocumentationType.SWAGGER_2)
.forCodeGeneration(Boolean.TRUE)
.select()
.apis(RequestHandlerSelectors.basePackage("com.user.controller"))
.paths(PathSelectors.any())
.paths(Predicates.not(PathSelectors.regex("/logout.*")))
.build()
.apiInfo(apiInfo());
}
private ApiInfo apiInfo() {
return new ApiInfo(
"REST API",
"REST description of API.",
"API TOS",
"Terms of service",
new Contact("Rajib Garai", "https://www.linkedin.com/in/rajibgarai90/", "90rajibgarai#gmail.com"),
"License of API", "API license URL", Collections.emptyList());
}
}
Here is the code i wrote to find and automatically create Docket on runtime per controller,
also has a Default Docket to show all in one group.
#Configuration
#EnableSwagger2
public class SwaggerConfig {
#Autowired
ConfigurableApplicationContext context;
//Default Docket to show all
#Bean
public Docket api() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(metaData())
.forCodeGeneration(Boolean.TRUE)
.select()
.apis(RequestHandlerSelectors.withClassAnnotation(RestController.class))
.paths(PathSelectors.any())
.paths(Predicates.not(PathSelectors.regex("/error.*")))
.build();
}
//Creating Docket Dynamically per Rest Controller
#PostConstruct
public void postConstruct() throws ClassNotFoundException {
ClassPathScanningCandidateComponentProvider provider
= new ClassPathScanningCandidateComponentProvider(false);
provider.addIncludeFilter(new AnnotationTypeFilter(RestController.class));
for (BeanDefinition beanDef : provider.findCandidateComponents("com.blah.blah.package")) {
Class<?> cl = Class.forName(beanDef.getBeanClassName());
RequestMapping requestMapping = cl.getAnnotation(RequestMapping.class);
if (null != requestMapping && null != requestMapping.value() && requestMapping.value().length > 0) {
String resource_group = requestMapping.value()[0];
SingletonBeanRegistry beanRegistry = context.getBeanFactory();
Docket docket = new Docket(DocumentationType.SWAGGER_2)
.groupName(resource_group)
.apiInfo(metaData())
.forCodeGeneration(Boolean.TRUE)
.select()
//.apis(RequestHandlerSelectors.withClassAnnotation(RestController.class))
.paths(PathSelectors.regex(resource_group + ".*"))
.paths(Predicates.not(PathSelectors.regex("/error.*")))
.build();
beanRegistry.registerSingleton(cl.getSimpleName() + "_docket_api", docket);
}
}
}
private ApiInfo metaData() {
return new ApiInfoBuilder()
.title("some Title Here")
.description("Some Desciption")
.version("1.0")
.contact(new Contact("Asad Abdin", "", "asadabdin#gmail.com"))
.build();
}
I am implementing a singleton service in a weblogic 12.2.1.2 with EBJ 3.1 in a maven multimodule EAR project.
My singleton service is registering in the cluster.
This is the log from the node where is registered:
<BEA-000189> <The Singleton Service Appscoped_Singleton_Service is now active on this server.>
And this is from other node:
<BEA-003130> <Appscoped_Singleton_Service successfully activated on server iss3.>
The singleton service is implementing the interface weblogic.cluster.singleton.SingletonService but the methods activate and deactivate is not invoked when the nodes starts or shutdown.
I was reading something about versioned EARs and MANIFEST files but not understood this.
I need some help to make methods activate and deactivate be invoked.
This is my class:
import java.io.Serializable;
import javax.inject.Inject;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import com.test.MyTimerLocal;
import weblogic.cluster.singleton.SingletonService;
public class MySingletonServiceClass implements SingletonService, Serializable, MySingletonServiceInterface {
private static final long serialVersionUID = 3966807367110330202L;
private static final String jndiName = "MySingletonServiceClass";
private int myValue;
#Inject
private MyTimerLocal myTimer;
#Override
public int getMyValue() {
return this.myValue;
}
#Override
public synchronized void setMyValue(final int myValue) {
this.myValue = myValue;
}
#Override
public void activate() {
System.out.println("activate triggered");
Context ic = null;
try {
ic = new InitialContext();
ic.bind(MySingletonServiceClass.jndiName, this);
System.out.println("Object now bound in JNDI at " + MySingletonServiceClass.jndiName);
this.myValue = 5;
final String msg = "###################### MySingletonServiceClass.activate():: Fechamento agendado para " + this.myTimer.agendaExecucao() + " ###############";
System.out.println(msg);
} catch (final NamingException e) {
this.myValue = -1;
e.printStackTrace();
} finally {
try {
if (ic != null) {
ic.close();
}
} catch (final NamingException e) {
e.printStackTrace();
}
}
}
#Override
public void deactivate() {
System.out.println("deactivate triggered");
Context ic = null;
try {
ic = new InitialContext();
ic.unbind(MySingletonServiceClass.jndiName);
System.out.println("Context unbound successfully");
} catch (final NamingException e) {
e.printStackTrace();
}
}
}
Thanks for your time.
I got this working now with this in my src\main\application\META-INF\weblogic-application.xml
<wls:singleton-service>
<wls:class-name>com.test.MySingletonServiceClass</wls:class-name>
<wls:name>Appscoped_Singleton_Service</wls:name>
</wls:singleton-service>
I am facing an issue in receiving a message from RabbitMQ.
I am sending a message like below
HashMap<Object, Object> senderMap=new HashMap<>();
senderMap.put("STATUS", "SUCCESS");
senderMap.put("EXECUTION_START_TIME", new Date());
rabbitTemplate.convertAndSend(Constants.ADAPTOR_OP_QUEUE,senderMap);
If we see in RabbitMQ, we will get a fully qualified type.
In the current scenario, we have n number of producer for the same consumer. If i use any mapper, it leads to an exception.
How will i send a message so that it doesn't contain any type_id and i can receive the message as Message object and later i can bind it to my custom object in the receiver.
I am receiving message like below.
Could you please let me know how to use Jackson2MessageConverter so that message will get directly binds to my Object/HashMap from Receiver end. Also i have removed the Type_ID now from the sender.
How Message looks in RabbitMQ
priority: 0 delivery_mode: 2 headers:
ContentTypeId: java.lang.Object
KeyTypeId: java.lang.Object content_encoding: UTF-8 content_type: application/json
{"Execution_start_time":1473747183636,"status":"SUCCESS"}
#Component
public class AdapterOutputHandler {
private static Logger logger = Logger.getLogger(AdapterOutputHandler.class);
#RabbitListener(containerFactory="adapterOPListenerContainerFactory",queues=Constants.ADAPTOR_OP_QUEUE)
public void handleAdapterQueueMessage(HashMap<String,Object> message){
System.out.println("Receiver:::::::::::"+message.toString());
}
}
Connection
#Bean(name="adapterOPListenerContainerFactory")
public SimpleRabbitListenerContainerFactory adapterOPListenerContainerFactory() {
SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory();
factory.setConnectionFactory(connectionFactory());
Jackson2JsonMessageConverter messageConverter = new Jackson2JsonMessageConverter();
DefaultClassMapper classMapper = new DefaultClassMapper();
messageConverter.setClassMapper(classMapper);
factory.setMessageConverter(messageConverter);
}
Exception
Caused by: org.springframework.amqp.support.converter.MessageConversionException: failed to convert Message content. Could not resolve __TypeId__ in header and no defaultType provided
at org.springframework.amqp.support.converter.DefaultClassMapper.toClass(DefaultClassMapper.java:139)
I don't want to use __TYPE__ID from sender because they are multiple senders for the same queue and only one consumer.
it leads to an exception
What exception?
TypeId: com.diff.approach.JobListenerDTO
That means you are sending a DTO, not a hash map as you describe in the question.
If you want to remove the typeId header, you can use a message post processor...
rabbitTemplate.convertAndSend(Constants.INPUT_QUEUE, dto, m -> {
m.getMessageProperties.getHeaders().remove("__TypeId__");
return m;
});
(or , new MessagePostProcessor() {...} if you're not using Java 8).
EDIT
What version of Spring AMQP are you using? With 1.6 you don't even have to remove the __TypeId__ header - the framework looks at the listener parameter type and tells the Jackson converter the type so it automatically converts to that (if it can). As you can see here; it works fine without removing the type id...
package com.example;
import java.util.HashMap;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.config.SimpleRabbitListenerContainerFactory;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitAdmin;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Bean;
#SpringBootApplication
public class So39443850Application {
private static final String QUEUE = "so39443850";
public static void main(String[] args) throws Exception {
ConfigurableApplicationContext context = SpringApplication.run(So39443850Application.class, args);
context.getBean(RabbitTemplate.class).convertAndSend(QUEUE, new DTO("baz", "qux"));
context.getBean(So39443850Application.class).latch.await(10, TimeUnit.SECONDS);
context.getBean(RabbitAdmin.class).deleteQueue(QUEUE);
context.close();
}
private final CountDownLatch latch = new CountDownLatch(1);
#RabbitListener(queues = QUEUE, containerFactory = "adapterOPListenerContainerFactory")
public void listen(HashMap<String, Object> message) {
System.out.println(message.getClass() + ":" + message);
latch.countDown();
}
#Bean
public Queue queue() {
return new Queue(QUEUE);
}
#Bean
public RabbitTemplate rabbitTemplate(ConnectionFactory connectionFactory) {
RabbitTemplate template = new RabbitTemplate(connectionFactory);
template.setMessageConverter(new Jackson2JsonMessageConverter());
return template;
}
#Bean
public SimpleRabbitListenerContainerFactory adapterOPListenerContainerFactory(ConnectionFactory connectionFactory) {
SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory();
factory.setConnectionFactory(connectionFactory);
factory.setMessageConverter(new Jackson2JsonMessageConverter());
return factory;
}
public static class DTO {
private String foo;
private String baz;
public DTO(String foo, String baz) {
this.foo = foo;
this.baz = baz;
}
public String getFoo() {
return this.foo;
}
public void setFoo(String foo) {
this.foo = foo;
}
public String getBaz() {
return this.baz;
}
public void setBaz(String baz) {
this.baz = baz;
}
}
}
Result:
class java.util.HashMap:{foo=baz, baz=qux}
This is described in the documentation...
In versions prior to 1.6, the type information to convert the JSON had to be provided in message headers, or a custom ClassMapper was required. Starting with version 1.6, if there are no type information headers, the type can be inferred from the target method arguments.
You can also configure a custom ClassMapper to always return HashMap.
Want to use "a" different Java calss when receive message?
Config #Bean Jackson2JsonMessageConverter with a custom ClassMapper
Want to use "many" different Java calss when receive message? such as :
#MyAmqpMsgListener
void handlerMsg(
// Main message class, by MessageConverter
#Payload MyMsg myMsg,
// Secondary message class - by MessageConverter->ConversionService
#Payload Map<String, String> map,
org.springframework.messaging.Message<MyMsg> msg,
org.springframework.amqp.core.Message amqpMsg
) {
// ...
}
Provide a custom #Bean Converter, ConversionService, RabbitListenerAnnotationBeanPostProcessor :
#Bean
FormattingConversionServiceFactoryBean rabbitMqCs(
Set<Converter> converters
) {
FormattingConversionServiceFactoryBean fac = new FormattingConversionServiceFactoryBean();
fac.setConverters(converters);
return fac;
}
#Bean
DefaultMessageHandlerMethodFactory messageHandlerMethodFactory(
#Qualifier("rabbitMqCs")
FormattingConversionService rabbitMqCs
) {
DefaultMessageHandlerMethodFactory defaultFactory = new DefaultMessageHandlerMethodFactory();
defaultFactory.setConversionService(rabbitMqCs);
return defaultFactory;
}
// copied from RabbitBootstrapConfiguration
#Bean(name = RabbitListenerConfigUtils.RABBIT_LISTENER_ANNOTATION_PROCESSOR_BEAN_NAME)
#Role(BeanDefinition.ROLE_INFRASTRUCTURE)
public RabbitListenerAnnotationBeanPostProcessor rabbitListenerAnnotationProcessor(
MessageHandlerMethodFactory handlerFac
) {
RabbitListenerAnnotationBeanPostProcessor bpp = new RabbitListenerAnnotationBeanPostProcessor();
bpp.setMessageHandlerMethodFactory(handlerFac);
return bpp;
}
#Bean(name = RabbitListenerConfigUtils.RABBIT_LISTENER_ENDPOINT_REGISTRY_BEAN_NAME)
public RabbitListenerEndpointRegistry defaultRabbitListenerEndpointRegistry() {
return new RabbitListenerEndpointRegistry();
}
References:
Jackson2JsonMessageConverter
AMQP-461
Debugging source code PayloadArgumentResolver