Apache Wink 1.4 JAX-RS issue - apache-wink

When i am returning response in my code i am getting it as 406 Not acceptable .
M using apache wink 1.x, jdk 7, tomcat 7. I am able to return a string when the type is "application/json", but not able to return json object as weel as response

This returns Json response back if not explicitly requested in header.
#Produces({"application/json", "application/xml" })
public Response retrieve(){
BookingType booking ;
//create booking where BookingType can be a POJO(annotated) or JAXB object.
return Response.ok(booking).build();
}

Related

Decompressing REST response with GZIPInputStream

I'm trying to decompress a gzip:ed response i receive from a REST service:
Content-Encoding=[gzip], Content-Type=[application/json], Content-Length=[710] ...
I'm using the Grails REST Client Builder Plugin:
def response = new RestBuilder().get(HOST + "/api/..."){
contentType "application/json"
accept "application/json"
}
The returned response is a Spring ResponseEntity. I'm trying to decompress the data using GZIPInputStream:
String body = response.getBody()
new GZIPInputStream(new ByteArrayInputStream(body.getBytes())).text
This fails to Caused by ZipException: Not in GZIP format
Obviously there is something I'm doing wrong, but I can't figure out what. All advice is appriciated.
If you really need to keep using the Rest Client Builder you only need to modify your client code slightly:
def response = new RestBuilder().get(HOST + "/api/..."){
contentType "application/json"
accept byte[].class, "application/json" }
Note the extra parameter in the accept call - byte[].class - which signifies that RestTemplate should refrain from any parsing of the response.
To decompress you can now do:
new GZIPInputStream(new ByteArrayInputStream(response.body))
Yeah, I know, already answered with accept but some might still find it helpful in case switching Rest components is not an option.
I never managed to get it to work with grails / groovy libraries, so i switched to spring and httpcomponents:
HttpComponentsClientHttpRequestFactory clientHttpRequestFactory = new HttpComponentsClientHttpRequestFactory(HttpClientBuilder.create().build());
RestTemplate restTemplate = new RestTemplate(clientHttpRequestFactory);
ResponseEntity<String> response = restTemplate.exchange(
"some/url/", HttpMethod.GET, new HttpEntity<Object>(requestHeaders),
String.class);
Which decoded gzip automatically and thus there are no longer any need for manual decoding.

SAPUI5: sap-message not retrieved using getHeaders()

I use the functionality of mapping messages from a SAP Gateway Service to the header of the response of an OData request. This allows me to receive on the client side the messages from a backend directly in an http-header called SAP-message
Unfortunately, and although I can see the SAP-message header in the response headers of my Odata response in the Chrome Dev Tools, when using the getHeaders method of the OData Model class, this particular header is not returned
Here is the code I use:
oModel.create("/PurchaseContractHeaderCollection", mPayload, {
async : true,
success : jQuery.proxy(function(mResponse) {
// get headers
var mHeaders = this.oModel.getHeaders();
// SAP-message is not mapped in the headers of the OData model
As a result, mHeaders doesn't contain any SAP-message object
Here is the content of the response headers visible in the Chrome Dev Tools:
content-length:705
content-type:application/json; charset=iso-8859-1
dataserviceversion:2.0
location:http://<server>.<domain>/PurchaseContractHeaderCollection('xxxxxx')
sap-message:{
"code":"ME/887",
"message":"Erreur lors de la reprise des données ExtensionIn pour l'extension",
"severity":"warning","target":"",
"details":[
{"code":"BAPI/000","message":"Exception","target":"","severity":"info"},
{"code":"06/017","message":" créé(e)","target":"","severity":"info"}
]}
server:SAP NetWeaver Application Server / ABAP 731
Any idea on where the issue lies?
I got that answer from SAP:
The ODataModel.getHeaders function is only there to access the request headers which were set by the application beforehand and the ODataModel itself - You cannot access the response header via this function
Use the following code instead:
oDataModel.create(...
{success: function (oData, oResponse) {
// access response headers here: oResponse.headers
...
}});

how to add content-type to Twilio response?

How to add content-type to Twilio Response? I am getting 502 bad gateway error and the error says that it could be because of missing Content-Type. But I do see that the response has the Content-type. So what could be going wrong? I am also seeing the twilio reason is connection-time out! What does that mean? This is related to my earlier post at: An attempt to retrieve content from returned the HTTP status code 502. Please check the URL and try again
Response - The HTTP headers and body of your server's response to Twilio
Headers
Content-Type text/html
Transfer-Encoding chunked
X-Twilio-Reason connection timed out
Body
1
<html><head><title>502 Bad Gateway</title></head><body><h1>Bad Gateway</h1>An upstream server returned an invalid response.</body></html>
Can somebody help me to find out why twilio is giving error while accessing my API?
This is what I have in my controller:
public class TestController : ApiController
{
[HttpPost]
public HttpResponseMessage Post([FromBody]SmsRequest smsReq)
{
string smsReqUpper = smsReq.Body.ToUpper();
string testString = "TEST";
var response = new Twilio.TwiML.TwilioResponse();
if (smsReqUpper == testString)
{
response.Sms("Test Successful");
return Request.CreateResponse(HttpStatusCode.OK, response.Element);
}
else
{
string strBody = "Invalid Text Command. Please text the word TEST " ;
response.Sms(strBody);
return Request.CreateResponse(HttpStatusCode.OK, response.Element);
//return new TwiMLResult(response);
}
}
The Content-Type text/html bit refers to the 502 bad gateway response, not the actual response coming from your server. Add the "text/xml" content type to your request (I don't recognize the framework you are using) and you should be good to go.
To debug your response you can use curl -vvv [URL] until the Content-Type: text/xml part is present.

Liferay Webservice JSON response type

I have a web service in liferay.. and the response would be
{"response":{"status":{"code":"200","message":"ok"},"userProfile": {"screenName":"testUser","userName":"testUser"}}}
I'm getting following error when consuming it from an ios client:
Encountered unexpected response with status code: 200 (MIME Type: text/javascript)
The MIME type must be application/json for json resonse; please change
Where to configure in Liferay to make the MIME type to be "application:json"
Thanks for your help!
I think it needs to be configured in your ios client rather than in Liferay.
Since Liferay is sending the response as desired and the error you are getting is on the client side.
Hope this helps.
Create a servlet ..
from the doGet method
set response.setContentType("application/json");
then add this servlet in the web.xml
response mime type would be "application/json"

RestResponse is null on POST request using RestSharp

I am making a POST request with RestSharp (on windows phone 7.1 client). I sent string to a service in a request body. Looks like the service is successfully called and it returns proper value (integer), however response object is null:
client.ExecuteAsync<T>(request, (response) => {
data = response.Data; // response is null in debugger
});
I cant understand why is that so.
<T> isn't a valid value for that call. I'm not sure that would even build there unless you've wrapped it in a generic method.
Also, is the response coming back as plain text? What's the Content-Type returned? Most likely, you should just use ExecuteAsync(request, callback) without the generic parameter and grab the data out of response.Content which is a string of the response body. response.Data is for the automatically deserialized XML or JSON (or custom) response if you use the generic method overload that specifies a type to deserialize to.
This seems to be an ongoing issue with RestSharp asynchronous calls - for HTTP transport errors ErrorException object is useless (returns null). Check the StatusCode property if it returns with anything but HttpStatusCode.OK. StatusDescription is not extremely useful either as it doesn't match complete status message from server response payload.

Resources