Can SpringFox generate Swagger Spec using Interface - springfox

I have an interface
public interface WebExportable {
#RequestMapping("/export")
Object export();
}
Does SpringFox support generating Swagger Spec using only the interface?

Springfox does support annotations on interfaces. However not all of them seem to traverse to super classes for resolution. I have tryed to outsource swagger annotations to interfaces and then let the Controller classes implement them. I have found out that nearly all of them will get resolved by Springfox but not the #ApiParam #PathVariable combination.
There is also an open issue on GitHub.

Related

Deserializing abstract types in jersey using jackson

I am writing a rest api and at the compile time i only have the model interfaces which i want to use as dto in my rest api. I dont want my rest api to have dependency of model implementations at compile time as i have diggerent jars for dealing with different databases. So it is obvious that jersey is not able to deserialize the json as it is not aware of the concrete type. I googled about instantiating abstract types but all the solutions are for compile time only wbich is not an option for me. Any help would be much appriciated.
#POST
public Response addMessage(Message message) {
// How to deserialize message since Message is an interface
}

How can I define a Java interface in JRuby rather than implement one?

I would like to invoke a Java API from JRuby that requires a Java interface, not a concrete class. The Java API uses java.lang.reflect.proxy to implement functionality based on the interface. I have found numerous examples of implementing a java interface with JRuby. However, I need to simply define a java interface in JRuby, not implement one. I can of course define an interface in Java and use this as the interface argument to the API via intf.java_class. However, for convenience, I'd like to be able to directly define the interface from JRuby code.
Update:
Here's the Java API I want to invoke from JRuby:
public interface Query {
<T> Collection<T> execute(Class<T> intf, String sql, Object... args);
}
The Java API requires that the intf argument be an interface, not a class. This is due to the fact that the implementation uses java.lang.reflect.proxy to provide a collection of data objects that can be accessed via the provided interface. Java's java.lang.reflect.proxy will only work with interfaces. As an example, suppose I were reading rows from a database that represented log statements. If I were invoking this from java I might define my data via
public interface LogRecord {
int id();
Timestamp when();
String msg();
String level();
String logger();
String thread();
}
and pass LogRecord.class as the first argument to the Query.execute method.
I don't think it can be done, because the Java class would have to inherit from the ruby interface, and apparently Java classes can't inherit from a JRuby class:
https://github.com/jruby/jruby/wiki/CallingJavaFromJRuby#java-classes-cant-inherit-from-a-jruby-class

How to intercept the marshmalling (json serial/deserial) from a CrudRepository declared as a #RepositoryRestResource?

Given this repository
#RepositoryRestResource( path = "u", itemResourceRel="ui", collectionResourceRel = "us")
public interface IUserRepository extends CrudRepository<Users, Long> {
}
When I go through a controller to call my service method findAllUsers(). I transform the
list of entities result (returned by userRepository.findAll() method provided for free) to a list of domain model objects. This list of domain model objects will get properly deserialize into JSON because I am controlling how to do the deserialization. There is a specific reason why I am doing this, see further below.
Now, if I want to use the userRepository directly, I am running into a loop during the serialization because the object being deserialized are coming from the library com.vividsolutions.jts.geom.Geometry. This class contains a method as described below
Geometry getEnvelope() Returns this Geometrys bounding box.
During the deserialization, jackson runs into a loop, because of it. I am able to correct the deserialization process within my controller. How could I intercept the deserialization from a CrudRepository?
The first idea that came to me was to create an implementation of my interface and override all methods, but it defeats the purpose of doing boiler-plate code.
You show the usage of #RepositoryRestResource I am assuming you're using Spring Data REST to export your repositories. For standard Spring MVC marshaling configuration see the reference docs.
Precisely speaking, you're not intercepting the repositories. Spring Data REST uses Jackson to marshal and unmarshal the responses and requests. Hence you simply customize the Jackson ObjectMapper that SD REST will use. To do so, you extend RepositoryRestMvcConfiguration and override configureJacksonObjectMapper(…):
class MyConfiguration extends RepositoryRestMvcConfiguration {
protected void configureJacksonObjectMapper(ObjectMapper mapper) {
// register custom serialializers, modules etc.
}
}
For customization options in general, have a look at the Jackson reference documentation.

Scala JSON serialization support in Jersey 2.5

I have created a Jersey 2.5 Scala REST API Project.
I have a ResourceConfig file, we will call it MyApplication, that looks similar to this:
class MyApplication extends ResourceConfig {
packages(classOf[MyResource].getPackage().getName())
}
All it does is register the resource: MyResource. How can I configure Jersey (2.5) to provide out-of-the-box style JSON Serialization/Deserialization.
For example, here is what MyResource might look like:
#Path("/")
class MyResource {
#POST
#Produces(Array("application/json"))
#Consumes(Array("application/json"))
def getIt(request:SomeRequestModel) = {
/* Do something with the request, return some response model */
return new SomeResponseModel
}
}
So to reiterate, how can I configure Jersey to automatically deserialize and serialize the request and response models, respectively?
It's not actually Jersey that provides the serialisation, it simply draws on an implementation of JAX-RS to perform that role.
Assuming Jersey is a strict requirement, the easiest solution here is to use jackson with Scala bindings. You can find an example here: https://bitbucket.org/jordipradel/jersey-scala-example
If you're not completely tied to Jersey... Might I suggest trying either Spray or spray2-mini instead for a far more idiomatic Scala solution?
I have used Jersey little when developing Java REST services. However, I would say you appear to be conflating two concepts--registering JAX-RS providers and configuring providers to serialize/deserialize JSON.
To register a provider, you use ResourceConfig as you have done.
As for the second issue of configuring Jersey to "know" how to serialize/deserialize JSON:
"As stated in Section 4.3, “Auto-Discoverable Features” JSON-Processing media module is one of the modules where you don't need to explicitly register it's Features (JsonProcessingFeature) in your client/server Configurable as this feature is automatically discovered and registered when you add jersey-media-json-processing module to your classpath."
To add the Jackson flavor of that module to the classpath, you just manually put it there or do this with Maven:
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-json-jackson</artifactId>
<version>2.5</version>
</dependency>
I chose Jackson because it is in my view the best JSON serializer/deserializer in the Java realm.
Having said all this, I once experimented writing services with my preferred Java REST framework, RESTEasy, in Scala. It was more awkward than my first date. Scala and RESTEasy just don't fit together because of an idiom mismatch, issues with types, and so on.
If you want to write REST services in Scala, please consider frameworks built with the language in mind like Scalatra, Unfiltered, or Spray.

JAX-RS client/server application using JSON and Wink

I'd like to implement JAX-RS server (on WebSphere Application Server) and client applications using JSON (Jackson convertor) format and Wink provider.
Do i need to use JAXB annotations for my DTO class that would be passed to and from the REST service and so must be serializable?
Which response type do i need to use, JSONObject or my Class type, i.e MyClass or String in Post methods negotiation between client/server?
In which cases we use JAXB annotations for domain classes in Rest services?
Your insight/directions would be high appreciated.
Thanks in advance,
Erwin
I guess you need to read more about Jackson.
But here are some short answers:
For 90% of cases you don't need JAXB annotations on your classes at all.
You need to use your classes:
#POST
public MyClass myMethod(MyOtherClass mcls)
You use JAXB annotations for some complex mapping, when you are not satisfied with the default results.
In addition to the above answer: it is often makes sense to use Jackson for JSON handling within Apache Wink. Jackson is more powerful and flexible than bundled facilities.
http://www.ibm.com/developerworks/java/library/wa-aj-jackson/index.html shows how to configure Apache Wink for Jackson.