Using #Context HttpServletResponse throws ambiguous dependencies org.glassfish.jersey.inject.cdi.se.bean.SupplierInstanceBeanBridge - jax-rs

I have this code on my server
#POST
#Path("subscribers")
#Consumes(MediaType.APPLICATION_XML)
public void createBook(SubscriberDTO dto, #Context HttpServletResponse response, #Context UriInfo uriInfo)throws Exception{
System.out.println("Test 1 : "+FILEPATH);
new SubscriberFileStorage().append(dto.toString(),FILEPATH);
String baseURL = uriInfo.getBaseUri().toString();
response.setStatus(HttpServletResponse.SC_CREATED);
response.setHeader(HttpHeaders.LOCATION, baseURL+dto.getTopic()+dto.getUrl());
try { response.flushBuffer(); }catch(Exception e){}
}
I get always the error, when submitting a post request
13-Jan-2023 05:01:56.403 SEVERE [http-nio-9090-exec-59] org.apache.catalina.core.StandardWrapperValve.invoke Servlet.service() for servlet [hesge.ch.tp05pubsubhub.HelloApplication] in context with path [/TP05PubSubHub_war_exploded] threw exception [org.jboss.weld.exceptions.AmbiguousResolutionException: WELD-001335: Ambiguous dependencies for type Ref with qualifiers #Default Possible dependencies:
org.glassfish.jersey.inject.cdi.se.bean.SupplierInstanceBeanBridge#2a0fe65e,
org.glassfish.jersey.inject.cdi.se.bean.SupplierInstanceBeanBridge#4491cd37] with root cause org.jboss.weld.exceptions.AmbiguousResolutionException: WELD-001335: Ambiguous dependencies for type Ref with qualifiers #Default Possible dependencies:
org.glassfish.jersey.inject.cdi.se.bean.SupplierInstanceBeanBridge#2a0fe65e,
org.glassfish.jersey.inject.cdi.se.bean.SupplierInstanceBeanBridge#4491cd37 at org.jboss.weld.bean.builtin.InstanceImpl.checkBeanResolved(InstanceImpl.java:244) at org.jboss.weld.bean.builtin.InstanceImpl.get(InstanceImpl.java:113)
I have checked all dependencies, none works. Any idea ? I really appreciate

Related

How to get inputs from an authentication failed on spring

As my project migrated from SPRING 3 to SPRING 5 the methods I had to get the user credentials with AuthenticationException getAuthentication.getPrincipal() became deprecated and now I can't get the info when a user fails to authenticate.
I tried to get the info from the HttpServletRequest with request.getParameter(WebAttributes.AUTHENTICATION_EXCEPTION); but as my authentication object is already null I get null;
I also tried with request.getSession().getAttribute(name); but the code doesn't recognize the "name" variable.
I could get the info needed with request.getparameter("name of your variable"); Here is an exemple;
public void onAuthenticationFailure(final HttpServletRequest request, final HttpServletResponse response, final AuthenticationException exception) throws IOException {
request.getparameter("name");
request.getparameter("lastname");
request.getparameter("id");
}

Kotlin class servlet provoke NoClassDefFoundError

In a sample Java servlet project in my IntelliJ IDE, i have created this class in Kotlin :
#WebServlet(name = "TwitterAPIServlet", description = "This is used to test the servlet api", urlPatterns = ["/twitterAPIServlet"])
class TwitterAPIServlet : HttpServlet() {
#Throws(IOException::class)
override fun doGet(req: HttpServletRequest, resp: HttpServletResponse) {
// Print answer
val out = resp.writer
out.println("Request Done : </br>")
}
}
When i am trying to call this with my .jsp page, i have this error :
2020-05-10 15:30:34.218:WARN:oejs.HttpChannel:qtp60559178-27: /twitterAPIServlet
java.lang.NoClassDefFoundError: kotlin/jvm/internal/Intrinsics
at main.java.co.rezo.api.internal.v1.TwitterAPIServlet.doGet(TwitterAPIServlet.kt)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:687)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:790)
When i am trying the same code in Java, it's working :
#WebServlet(
name = "TwitterAPIServlet",
description = "This is used to test the servlet api",
urlPatterns = "/twitterAPIServlet")
public class TwitterAPIServlet extends HttpServlet {
#Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
PrintWriter out = resp.getWriter();
out.println("Request Done : </br>");
}
}
What can i do to make my Kotlin code working?
Most probably the issue is that Kotlin's standard library is not included in the generated WAR (or is not on a classpath if you run the exploded app). Make sure to include it in your build.
Instructions for Gradle can be found in the official examples repository: kotlin-examples.
Maven is similar: https://kotlinlang.org/docs/reference/using-maven.html.
In case of pure IDEA (though it's recommended to use one of the build tools above), check the projects settings:
You should see Kotlin's stdlib included in the artifact:

NullPointerException when using CXF SSE by #Context SseEventSink

I am trying to use JAX-RS SSE(Server-sent event) feature in Tomcat 8.0.45 and Apache CXF 3.2.8, but always get a NullPointerException when send the GET request from browser via javascript new EventSource(url). Following is the code:
#Path("/sse")
public class SSEService {
#GET
#Path("/test")
#Produces(MediaType.SERVER_SENT_EVENTS)
public void test(#Context Sse sse, #Context SseEventSink eventSink) {
try (SseEventSink sink = eventSink) {
sink.send(sse.newEvent("Hello world"));
}
}
}
The exception:
Caused by: java.lang.NullPointerExecption
at org.apache.cxf.jaxrs.impl.AsyncResponseImpl.initContinuation(AsyncResponseImpl.java:306)
at org.apache.cxf.jaxrs.impl.AsyncResponseImpl.<init>(AsyncResponseImpl.java:68)
at org.apache.cxf.jaxrs.sse.SseEventSinkContextProvider.createContext(SseEventSinkContextProvider.java:47)
......
Thanks for your help!

How to use exception mapper for exceptions thrown from interceptors in MSF4J

I added a authorisation module in interceptor, and tried to use exception mapper to throw 401 status code, but even if it is scanning my exception mapper, it is not using it in runtime.
#Component
public class ExceptionMapper implements javax.ws.rs.ext.ExceptionMapper<Exception> {
public ExceptionMapper() {
System.out.println("true Manish= " + true);
}
#Override
public Response toResponse(Exception exception) {
System.out.println("product Manish Exception comning here");
return Response.status(Response.Status.EXPECTATION_FAILED).
entity(exception.getMessage()).
type("text/plain").
build();
}
}
So, it is printing true Manish=true, but when my interceptor throws an exception, it doesn't hit toResponse() method
At the moment MSF4J can't map the exception which are get thrown from interceptor level. It only can map the exceptions which are thrown inside the actual service. We would like to enhance that to having global exception mappers. https://github.com/wso2/msf4j/issues/327 been created for that.

spring data elasticsearch Invocation of init method failed; nested exception is java.lang.AbstractMethodError

I have problem with Spring Data Elasticsearch. I configure elasticsearch like this:
#Configuration
#EnableJpaRepositories(basePackages = {"org.project.repositories"})
#EnableElasticsearchRepositories(basePackages = "org.project.repositorieselastic")
#EnableTransactionManagement
public class PersistenceContext {
#Bean
public ElasticsearchOperations elasticsearchTemplate() {
return new ElasticsearchTemplate(client());
}
#Bean
public Client client(){
Settings settings = ImmutableSettings.settingsBuilder()
// Setting "transport.type" enables this module:
.put("cluster.name", "elasticsearch")
.put("client.transport.ignore_cluster_name", false)
.build();
TransportClient client= new TransportClient(settings);
TransportAddress address = new InetSocketTransportAddress("127.0.0.1", 9300);
client.addTransportAddress(address);
return client;
}
}
My repo looks like.
#Repository()
public interface UserFavoriteElasticRepo extends ElasticsearchRepository<UserFavorite, Long> {
}
Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: org.project.repositorieselastic.UserFavoriteElasticRepo org.project.services.elastic.FavoriteIndexerService.elasticRepo; nested exception
is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userFavoriteElasticRepo': Invocation of init method failed; nested exception is java.lang.AbstractMethodError
It's look like the implementation is not generated. But I don't know where to investigate. I tried to use one package and use this - https://github.com/izeye/spring-boot-throwaway-branches/commit/874ccba09189d6ef897bc430c43b6e3705404399 but with no success.
I solved the problem adding this in pom file
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-commons</artifactId>
<version>1.12.2.RELEASE</version>
</dependency>
I've got same exception using spring-data-elasticsearch.
But exception was thrown when I declared new methods in repository:
ex.:
public interface UserFavoriteElasticRepo extends ElasticsearchRepository<UserFavorite, Long> {
Page<UserFavorite> findBySomeProperty(String propertyValue, Pageable pageable);
}
It was casued due to spring-data-elasticsearch, spring-data-commons versions. Function declarations have changed: org.springframework.data.repository.query.QueryLookupStrategy.resolveQuery - it caused the exception.
For spring-data-elasticsearch version 2.0.0.RELEASE you have to use spring-data-commons with version 1.12.0.
If you have spring-data-jpa in your project it also uses spring-data-commons. For spring-data-jpa v1.9.0.RELEASE, spring-data-commons is v1.11.0.RELEASE
Could you provide what frameworks and versions of spring you are using? Also If you could put whole stacktrace, it will be helpfull?