Resteasy three ways to accept HTTP requests

Source: Internet
Author: User
Tags getmessage

To configure the access path for the class:

@Path (Globalconstants.dbproxy_server_context + "/" + version._1 + "/attachment")


Access path to the configuration method:

GET Request:

The JSON type is returned to the client

@Path ("/get/{id}")
@GET
@Produces ("Application/json")
Public Serverresponse getattachment (@PathParam (value = "id") String ID)

Get POST Request

@POST

Public Serverresponse getattachment (@FormParam (value = "id") String ID)

Get file for put transfer:

@Context
Use @context to get httpservletrequest,httpservletresponse.

@Path ("/newattach/{id}") @PUT @Produces ("Application/json") public serverresponse Newattach (@PathParam ("ID") String
		ID, @Context httpservletrequest request) throws ioexception{Long RequestTime = System.currenttimemillis ();
		Logger.info ("@GET" + RequestTime + "Attachmentresource.newattach ID:" + ID);
		Serverresponse sr = null;
		InputStream in = null;
			try {in = Request.getinputstream ();
			byte[] data = Ioutil.read (in);
			Attachmenthelper.newattach (Id,data);
		sr = new Serverresponse ("{\" issuccess\ ": \" True\ "}");
			catch (Eopexception e) {e.printstacktrace (); Logger.error ("EXCEPTION:" + e.getexceptioncode () + "CODE:" + E.getexceptioncode (). GetCode () + "message:" + e.getmessage (
			), E);
		sr = new Serverresponse (E.getmessage (), E.getexceptioncode ());
			}catch (Exception e) {e.printstacktrace ();
			Logger.error (E.getmessage (), E);
		sr = new Serverresponse (E.getmessage (), exceptioncode.dbproxy_other); } logger.info ("@GET" + requesttime + "ServerreSponse.newattach sr: "+ sr.tostring () +" use: "+ (Requesttime-system.currenttimemillis ()));
	return SR; }

Resteasy returns the response file, which can be written in produces ("*/*") to represent arbitrary files, using @context annotations to obtain response.

@Path ("/getattach/{id}") @GET @Produces ("*/*") public serverresponse Getattach (@PathParam ("id") String ID, @Context Htt
		Pservletresponse response) {Long requesttime = System.currenttimemillis ();
		Logger.info ("@GET" + RequestTime + "Attachmentresource.getattach ID:" + ID);
		OutputStream outputstream = null;
		Serverresponse sr = null;
		byte[] data = null;
			try {outputstream = Response.getoutputstream ();
			data = Attachmenthelper.getattach (ID);
			Outputstream.write (data);
			Outputstream.flush ();
		Outputstream.close ();
			catch (Eopexception e) {e.printstacktrace (); Logger.error ("EXCEPTION:" + e.getexceptioncode () + "CODE:" + E.getexceptioncode (). GetCode () + "message:" + e.getmessage (
			), E);
		sr = new Serverresponse (E.getmessage (), E.getexceptioncode ());
			}catch (Exception e) {e.printstacktrace ();
			Logger.error (E.getmessage (), E);
		sr = new Serverresponse (E.getmessage (), exceptioncode.dbproxy_other); } logger.info ("@GET" + requesttime + "ServeRresponse.getattach sr: "+ sr.tostring () +" use: "+ (Requesttime-system.currenttimemillis ()));
	return SR; }






Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.