When defining a jersey ContainerResponseFilter, we added it as follows:
PackagesResourceConfig rc = new PackagesResourceConfig("com.company.xxx");
rc.getContainerResponseFilters().add(new MyContainerResponseFilter());
We have also created a ClientRequestFilter, which I understand will be called before the endpoint is invoked.
The question is, how (presumably using PackagesResourceConfig) is a ClientResponseFilter added?
The following will throw a runtime error:
rc.getContainerResponseFilters().add(new ClientResponseFilter());
and there is no method such as rc.getClientResponseFilters()
Thanks
Related
I'm using Project Reactor with Spring Integration to read from Kafka and write to MongoDB, and I the Kafka consume works well, but the .handle(MongoDb.reactiveOutboundChannelAdapter(mongoFactory)) stucks. I've seen that the internal code of this function is new ReactiveMongoDbStoringMessageHandler(mongoFactory)), so I've tried the following (I have a transform() method that converts from ConsumerRecord to Mono<String>, with the #Transformer annotation):
public IntegrationFlows writeToMongo() {
return IntegrationFlows.from(kafkaChannel)
.transform(this)
.handle(new ReactiveMongoDbStoringMessageHandler(mongoFactory))
.get();
}
The code follows the docs https://docs.spring.io/spring-integration/reference/html/mongodb.html#mongodb-reactive-channel-adapters.
The error I get is:
java.lang.IllegalArgumentException: Found ambiguous parameter type [class java.lang.Void] for method match: and then a very long list of functions. Any reason this could happen?
You cannot do new ReactiveMongoDbStoringMessageHandler(mongoFactory) if you are not going to subscribe to the returned Mono. A .handle(MongoDb.reactiveOutboundChannelAdapter(mongoFactory)) is the right way to do since it wraps that ReactiveMongoDbStoringMessageHandler into a ReactiveMessageHandlerAdapter for automatic subscription.
However I think your real problem is with the .transform(this). I believe you have a lot of methods in this class, so be more specific with method name. And this has nothing to do with Project Reactor. Not sure though why would one try to convert to Mono before sending to that ReactiveMongoDbStoringMessageHandler... You probably have problem in providing the payload (ConsumerRecord ?) which is not MongoDB mapped entity for saving into the collection.
Happened to me either. Solution was (notice the ReactiveMessageHandlerAdapter):
public IntegrationFlows writeToMongo() {
return IntegrationFlows.from(kafkaChannel)
.handle(new ReactiveMessageHandlerAdapter(new ReactiveMongoDbStoringMessageHandler(mongoFactory)))
.get();
}
This can be replaced by handleReactive() when this issue will be resolve.
We are using CXF 3.x, project has classes MyFeature extends AbstractFeature and `MyFilter extends OAuthRequestFilter.
Inside MyFeature class object of MyFilter created as MyFilter myFilter = new MyFilter (classObj); where classObj is object of class on which filter needs to be applied.
When I hit endpoint it throws error as 403.
Further when I used debugger and LOG messages, I come to know that inside file org.apache.cxf.interceptor.security.AbstractAuthorizingInInterceptor (cxf-core version 3.0.4.redhat-621159) there is function getTargetMethod and inside that method evaluation of BindingOperationInfo and m.get("org.apache.cxf.resource.method") are coming null, due to which I am getting 403.
Note: We are using osgi (jboss) bundle as deployment strategy.
Filters executed by JAXRSInInterceptor are in unmarshal state. In this state org.apache.cxf.resource.method remains null. I created interceptor in state PRE_STREAM and at that time org.apache.cxf.resource.method gives you correct name that should be executed.
currently I'm exploring approaches to implement 'request timeout management in an AOP way in Spring Boot' with several restrictions. The requirements/restrictions are stated as below:
The original purpose is that if the processing time of an api request exceeds 5 seconds, then directly return timeout result instead of continue processing
The rest api to be monitored is implemented by standard spring mvc rest controller. All apis inside are returning json strings like this:
#RestController
public class xxxxxx {
#RequestMapping(value = "xxxxxxx")
public String xxxxxx(#RequestParam(value = "xxxx", required = true) String xxxx) {
....
return json.toString();
}
}
The timeout logic is required to be implemented by AOP
(The real mean part)
No changes should be made to the controllers, which means: Request generation approach should not be changed; Return type should not be changed(No 'Callable<...>' allowed)
I have already found 1 answer(Async approach) which can perfectly resolve the problem itself with spring async, and the timeout return result is very pretty, but it's changing the return type, and also touching the code in controller. I also found one solution(AOP approach) which is using AOP, but the scenario is quite different from mine. It's already moving some business logic into AOP class, but I'm not allowed to touch the controller code. I would be grateful if anyone can provide a solution. Solutions that can't meet all the restrictions but are minimizing the differences are also admitted.
Since there is still no response to this question, I will put my own temporary solution here.
I'm using Hystrix dependency.
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-hystrix</artifactId>
<version>1.4.0.RELEASE</version>
</dependency>
It's well integrated with springboot, so the configuration is easy. Once properly configured, need to append an annotation on the request method that requires timeout handling. e.g.
#HystrixCommand(fallbackMethod="fallback")
#RequestMapping(value = "xxxxxxx")
public String xxxxxx(#RequestParam(value = "xxxx", required = true) String xxxx) {
....
return json.toString();
}
And need to add a fallback method with the name mapped to the value of 'fallbackMethod' inside annotation:
public String fallback() {
...
}
The timeout time value can be globally configured inside application.properties
hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds=3000
hystrix.command.default.circuitBreaker.sleepWindowInMilliseconds=3000
This is still not concise in these points:
Need to copy/paste this annotation for every method
Need to copy/paste the fallback method in every place hystrix is used
For hystrix fallback method itself, the parameter type and number need to be exactly same with the hystrix marked method. Currently I'm using several overloading method called 'fallback' for this, in each controller
But at least it's not changing method return types and code inside methods anymore and is the best solution I can think of currently. Will perform update once I find better solutions.
I'm trying to add some code to a class that is inside a jar(maven dependecy) and i'm doing it in the following way:
ClassPool classPool = ClassPool.getDefault();
CtClass ctClass = classPool.get("xyz.abc.ClassInADependecy");
CtMethod method = ctClass.getDeclaredMethod("getSomeValue");
method.insertBefore("{ System.out.println(\"modified\"); }");
I'm using Spring and the above code is being called using a #Configuration annotation.
When i call the method getSomeValue nothing is printed.
Can you help me find out what i'm doing wrong?
Thank you very much.
You are only changing the implementation as it is represented in Javassists type pool. You have to make sure that the class is also loaded by the respective class loader. Also, this must happen before the class is loaded for the first time, i.e. before your Spring application loads that class.
One way to do so is to manipulate the class from a Java agent: https://docs.oracle.com/javase/7/docs/api/java/lang/instrument/package-summary.html
I have created Spring ROO application using below link in Eclipse.
http://www.cubrid.org/blog/dev-platform/spring-roo-fast-java-application-development-tool/
In this i am having a controller named BookController which is having #RequestMapping("/book"). This works fine but When i want to get data from textfield to java class i am not getting by moving this RequestMapping to method level.
The actionURL of page is = /SPringDemo/book
When i am moving this to method level like this
#RequestMapping(value = "/book")
public String gettingData(Book book) {
System.out.println("Book is = " +book.getName());
return null;
}
This is not performing opeartion
Spring web mvc try to match request with methods. This include parameters required. In your request definition you ask for a Book instance but, if Spring can locate any parameter or model attribute which make matching to definition, your method will not be invoked.
Check this example which is similar to your case.
Good luck!