Skip to content

Exposing your application as a service.

Mario Muñoz edited this page Jun 24, 2014 · 5 revisions

You have an available sample of exposing an HTTP service within the classes samples/Server.java and samples/Application.java. Usually, you only need to create an annotated method that wraps the call for the application you created in the previous step. You can customize the context and the path where you want your service to be deployed using annotations, and also the port in which the server will run, via applications.properties file. Every class annotated with @RestController, just like samples/Server.java does, will be automatically deployed in the desired web context when running samples/Application.java.

@RestController
public class Server {

    @Autowired
    SimpleSentimentAnalyzer analyzer;

    @RequestMapping(value = "/sentiment",
                    method = RequestMethod.POST,
                    produces = {"application/json"})
    public String getSentiment(@RequestBody String input) {
        try {
            JSONObject jsonInput = new JSONObject(input);
            return analyzer.getSentiment(jsonInput.getString("input")).toString();
        } catch(Exception e) {
            throw new WebApplicationException(500);
        }
    }

}

In the given example, the final service will be exposed at http://0.0.0.0:server.port/sentiment (as specified in @RequestMapping annotation) and it will accept POST requests, returning a JSON NIF result. server.port is configurated in application.properties.

Clone this wiki locally