This file contains hidden or 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
@Provider | |
public class MyExceptionMapper implements ExceptionMapper<Exception> { | |
public Response toResponse(Exception e) { | |
ResourceError resourceError = new ResourceError(); | |
String error = "Service encountered an internal error"; | |
if (e instanceof CoffeeNotFoundException) { | |
resourceError.setCode(Response.Status.NOT_FOUND.getStatusCode()); | |
resourceError.setMessage(e.getMessage()); | |
return Response.status(Response.Status.NOT_FOUND) | |
.entity(resourceError) |
This file contains hidden or 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
// Controller | |
@POST | |
@Consumes(MediaType.APPLICATION_JSON) | |
@Produces(MediaType.APPLICATION_JSON) | |
@ValidateOnExecution | |
public Response addCoffee(@Valid Coffee coffee) { | |
... | |
} | |
// Coffee POJO |
This file contains hidden or 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
@WebFilter(filterName = "LoggingFilter", urlPatterns = {"/*"}) | |
public class LoggingFilter implements Filter { | |
static final Logger logger = Logger.getLogger(LoggingFilter.class); | |
@Override | |
public void doFilter(ServletRequest servletRequest, | |
ServletResponse servletResponse, | |
FilterChain filterChain) throws IOException, ServletException { | |
HttpServletRequest httpServletRequest = (HttpServletRequest) servletRequest; | |
logger.info("request" + httpServletRequest.getPathInfo().toString); | |
filterChain.doFilter(servletRequest, servletResponse); |
This file contains hidden or 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
// Coffee 객체로부터 JSON 문자열을 읽어들이는 코드 | |
ObjectMapper objectMapper = new ObjectMapper(); | |
Coffee coffee = objectMapper.readValue(jsonData, Coffee.class); |
This file contains hidden or 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
@GET | |
@Produces(MediaType.TEXT_PLAIN) | |
@Path("/orders/{id}/chunk") | |
public ChunkedOutput<String> chunkExample(final @PathParam("id") int id) { | |
final ChunkedOutput<String> output = new ChunkedOutput<String>(String.class); | |
new Thread() { | |
@Override | |
public void run() { | |
try { | |
output.write("test"); |
This file contains hidden or 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
@GET | |
@Produces(MediaType.TEXT_PLAIN) | |
@Path("/orders/{id}") | |
public Response streamExample(@PathParam("id") int id) { | |
final Coffee coffee = CoffeeService.getCoffee(id); | |
StreamingOutput stream = new StreamingOutput() { | |
@Override | |
public void write(OutputStream os) throws IOException, | |
WebApplicationException { | |
Writer writer = new BufferedWriter(new OutputStreamWriter(os)); |
This file contains hidden or 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
@Path("/v1/books/") | |
public class BookResource { | |
@Path("{resourceID}.xml") | |
@GET | |
public Response getBookInXML(@PathParam("resourceID") String resourceID) { | |
// XML 형식 응답 | |
} | |
@Path("{resourceID}.json") | |
@GET |
This file contains hidden or 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
@GET | |
public Reponse getCoffee(@Context Request r) { | |
List<Variant> vs = Variant.mediatypes("application/xml", "application/json").build(); // build()로 List<Variant> 생성 | |
Variant v = r.selectVariant(vs); | |
if (v == null) { | |
return Response.notAcceptable(vs).build(); | |
} else { | |
Coffee coffee = ..; // v값에 따라 표현형을 선택한다. | |
return Reponse.ok(coffee, v); | |
} |
This file contains hidden or 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
@WebServlet(name = "TestClient", urlPatterns = {"/TestClient"} ,asyncSupported=true) | |
public class TestClient extends HttpServlet { | |
private final static String TARGET_URI = "http://localhost:8080/helloworld/v1/coffees/orders"; | |
@Override | |
protected void service(final HttpServletRequest request, final HttpServletResponse response) | |
throws ServletException, IOException { | |
response.setContentType("text/html;charset=UTF-8"); |
This file contains hidden or 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
@Path("v1/coffees") | |
public class CoffeesResource { | |
@GET | |
@Path("orders") | |
@Produces(MediaType.APPLICATION_JSON) | |
public List<Coffee> getCoffeeList() { | |
... | |
} | |
@POST |
NewerOlder