1

I have a frontend run by tomcat and my backend processing run by Mule.

Take for example,

HelloWorld.html

<div>
  <button id="btnSayHello" ng-click="sayHello()" data-transalte>Click Me</button>
</div>

HelloWorld.js

$scope.sayHello = function() {
  $http({
    method: 'GET',
    url: $rootScope.serviceUrl + '/triggers/greetings/' + $scope.name
  }).success(function (response) {
    alert("Success");
  }).error(function () {
    alert("Error");
  })
}

MyTriggers.java

@Component
@Path("/triggers")
public class MyTriggers {

  ...

  @GET
  @Produce("text/plain")
  @Path("/greetings/{name}")
  @Consume("text/plain")
  public String sayHello(@PathParam("name") String name) {
    
    log.info("Hello, " + name);
    
    return "SUCCESS";
  }
  
}

greeting.xml

<flow name="greeting">
   <jersey:resources>
       <spring-object bean="MyTriggers"/>
   </jersey:resources>
</flow>

enter image description here

My Result keep coming back as Error, which means it failed at the JavaScript level.

Not quite sure what went wrong.

Tomcat is running on localhost:8080.

Lele
  • 13
  • 3
  • Looks like you use JAX-RS annotations. If you want to do that, you need additional dependencies (like described in the answer here: https://stackoverflow.com/questions/42944777/difference-between-jax-rs-and-spring-rest) – dunni Jan 06 '22 at 09:58
  • Please share the exact Mule version and the Mule XML flow. – aled Jan 06 '22 at 12:08
  • Also share the packages of the annotations I'm your Java code. – aled Jan 06 '22 at 12:16
  • @aled yes I deleted my answer, I've got Spring on the brain. – Essex Boy Jan 06 '22 at 12:37
  • Please don't use screenshots for text errors. See the explanation at https://meta.stackoverflow.com/questions/285551/why-not-upload-images-of-code-errors-when-asking-a-question. Additionally if you are seeing the error in the browser you should also take a look at the logs in the server side. – aled Jan 12 '22 at 12:55

1 Answers1

0

I'll assume you are using Mule 3.x because you appear to be deploying a Mule deployable zip. There are several issues in your question.

Mule doesn't just automatically call a Spring bean or method. You need to have a Mule flow that calls it. Specifically the @Component annotation doesn't do anything for Mule by default.

I infer from you mentioning Tomcat that you are deploying a Mule deployable zip (ie a Mule Application for Mule 3) into Tomcat. Those zip files are meant to be deployed into a Mule server, not a Tomcat server. Without more details on how are you packaging and deploying, I would assume it is not going to work. If you are trying to do an embedded deployment of the application in a Tomcat you should follow the instructions at https://docs.mulesoft.com/mule-runtime/3.9/deploying-mule-as-a-service-to-tomcat. I don't recommend to use this method. It is a legacy method, confusing and doesn't support many components of Mule.

The other annotations in your Java method seems to be JAX-RS annotations. That is supported by Mule in the Jersey module. It should have something in a flow that calls though.

Example:

  <flow name="HelloWorld">
     <http:listener config-ref="HTTP_Listener_Configuration" path="/*" doc:name="HTTP"/>
     <jersey:resources>
         <component class="com.mycompany.jersey.HelloWorldResource"/>
     </jersey:resources>
  </flow>

Java side:

package com.mycompany.jersey;

import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.PathParam;

@Path("/helloworld")
public class HelloWorldResource {

    @POST
    @Produces("text/plain")
    @Path("/{name}")
    public String sayHelloWithUri(@PathParam("name") String name) {
        return "Hello " + name;
    }
}

Reference documentation: https://docs.mulesoft.com/mule-runtime/3.9/jersey-module-reference

The JavaScript/HTML side should not be very relevant. The HTTP Request that it generates is.

aled
  • 21,330
  • 3
  • 27
  • 34
  • Thank you aled, i have this issue where they say "Element jersey:resoruces is not allowed here". Do you know why? – Lele Jan 11 '22 at 10:03
  • Please share your current flow and the complete error message. – aled Jan 11 '22 at 12:13
  • I have the same flow as above, but i added a .xml to deploy the webservice (shown above) – Lele Jan 12 '22 at 06:40
  • Looks like you removed the message source from the flow. That's the component that triggers the start of the flow execution. The Jersey component is not a message source. In my example it is an HTTP Listener, however if you are still deploying to Tomcat -which I don't recommend- then the HTTP Listener will not work and you need to investigate using the Servlet connector instead. – aled Jan 12 '22 at 12:57