Skip to content

Instantly share code, notes, and snippets.

@mlaccetti
Created August 20, 2013 15:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mlaccetti/6282812 to your computer and use it in GitHub Desktop.
Save mlaccetti/6282812 to your computer and use it in GitHub Desktop.
Expose a Spring service via REST/JSON
package laccetti.sample.controller;
import lombok.extern.slf4j.Slf4j;
import javax.annotation.PostConstruct;
import javax.servlet.http.HttpServletRequest;
import laccetti.sample.model.SearchRequest;
import laccetti.sample.model.SearchResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
@RequestMapping("/sample")
@Slf4j
public class SampleServiceController extends AbstractController {
@Autowired
private SampleService sampleService;
public SampleServiceController() {
// empty
}
@PostConstruct
public void init() {
// nothing here, but hey, let's just waste some time
}
@RequestMapping(value = "/search", method = RequestMethod.POST, produces = "application/json")
@ResponseBody
public SearchResponse search(@RequestBody SearchRequest searchRequest) {
// We're doing no validation, just flatout acting as a conduit
return sampleService.search(searchRequest.getSearchField());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment