How to return javascript code to be executed by client in Jersey Rest?

What I am trying to do is that after executing the following REST API, the browser should warn "Hello World !!". string in the browser.

I thought annotating the function with @Produces ("text / javascript") would take care of this. But since I am clicking on the resource at the following url

    http://localhost:8080/JAXRS-HelloWorld/rest/script/helloWorld 

      

JAXRS-HelloWorld is the name of the application and I specified the url as / rest *

I am getting everything as String. Not as executable Javascript code.

  package com.bablo.rest;

  import javax.ws.rs.GET;
  import javax.ws.rs.Path;
  import javax.ws.rs.Produces;
  import javax.ws.rs.core.Response;
  @Path("script")

 public class JavaScriptTesting {
  @GET
  @Produces("text/javascript")
  @Path("/helloWorld")
  public Response helloWorld(){
        String responseParam = "alert(Hello World!!)";
        return Response.ok(responseParam).build();
    } 
}

      

How do I force the browser to perform a warning function? Can we achieve this by doing some server side REST API tricks?

+4


source to share


3 answers


You want the server to execute client side javascript. It won't work.

You need to update your REST client to evaluate the JavaScript code returned by the server:

 var response = callRestApi();
 eval(response);

      



Or, you can return the HTML that your script inserts for it to be evaluated by the browser:

    @GET
    @Path("/test")
    @Produces(value = MediaType.TEXT_HTML)
    public String test() {
        return "<script>alert('test');</script>";
    }

      

+3


source


@GET
@Path("/admission")
@Produces(value = MediaType.TEXT_HTML)
public String admission(@QueryParam("id") String id){
    System.out.println("PARAMETRO RECIBIDO : "+id);
    return "<script>alert('Notificacion exitosa');</script>";
}

      



0


source


We use the Command template and the window.call () function to execute commands.

The commands must match the JavaScript methods you want to execute (so toString () is overridden)

public enum Commands {
  LOAD_BLOCK,

  @Override
  public String toString() {
    String[] parts = name().split("_");
    StringBuilder sb = new StringBuilder();

    // loop through all parts of the name
    for (int i = 0; i < parts.length; i++) {
      String word = parts[i];
      // use a lowercase letter for the first word
      if (i == 0) {
        sb.append(word.toLowerCase());
      // follow camel case pattern (first letter capital)
      } else {
        sb.append(String.valueOf(word.charAt(0)));
        sb.append(word.substring(1, word.length()).toLowerCase());
      }
    }
    return sb.toString();
  }
}

      

Building and sending commands to the Client from the Rest resource

Command load = new Command(Commands.LOAD_BLOCK.toString(), new String[]{"b1"})
return Response.status(200).entity(load).build();

      

In your JavaScript file, prepare a method that calls this Rest resource and run a method like the one below that takes a list of commands as input and executes them with the arguments provided.

function triggerEvents(commands){
    for (var index = 0; index < commands.length; index++){
        var command = commands[index];
        console.debug("executing " + command.function);
        await window[command.function].call(null, command.arguments);
    }
}

      

It will search for a suitable function and will execute with the given parameters if found

async function loadBlock(blockId) {
  // do some work
}

      

0


source







All Articles