Created
August 20, 2013 15:16
-
-
Save mlaccetti/6282812 to your computer and use it in GitHub Desktop.
Expose a Spring service via REST/JSON
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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