I have implemented Struts2 REST API from getting info from here
Is there any way to return custom response in in restful plugin in Struts2. I did all the required changes like
struts.rest.content.restrictToGET = false
Got from This Question. Still I'm getting this error
No result defined for action `com.web.Controller.RestDemoController` and result create,
If I don't add the above line I still get the same response.
This is the action i have provided in struts.xml
:
<action name="restdemo" class="com.web.Controller.RestDemoController">
<interceptor-ref name="customRestStack"></interceptor-ref>
</action>
This serves all the request GET
,POST
,PUT
,UPDATE
.
After Changing return type of post method from HttpHeader to String I'm still getting the same error
Error 404: No result defined for action com.web.Controller.RestDemoController and result <?xml version="1.0" encoding="UTF-8"?> <Status><code>200</code><message>Success</message></Status>
This is the code i have written for POST:
public HttpHeaders create(){
System.out.println(envision2Data.toString());
return new DefaultHttpHeaders("create").withStatus(200);
}
this is the POST request method with return type String
:
public String create(){
System.out.println(envision2Data.toString());
return "<?xml version=\"1.0\" encoding=\"UTF-8\"?> <Status><code>200</code><message>Success</message></Status>";
}
I'm getting perfect response for get either if i send request for xml or json, I do get xml and json based on extension.
like http://localhost:8080/restdemoapplication/restdemo.xml
, http://localhost:8080/restdemoapplication/restdemo.json
for POST request i do post request like
and you can see the response i get. The method i have written for post is written above with name create. I do have data in body and I do get the data in create method perfectly.
Now in post as i have seen in multiple example like
I don't want to return response for post request like these applications do. I want to return my own response, It will be a status code and a message like this
<?xml version="1.0" encoding="UTF-8"?> <Status><code>200</code><message>Success</message></Status>
After some debugging I found that DefaultContentTypeHandlerManager
in struts2-rest-plugin consider xhtml
as default template. While it should be either XML or JSON.
I want to return
code : 1xx,2xx,4xx
message: Success, Fail
in either XML or JSON when a POST request is entertained.
(This is application entertains both non-restful request and restful requests. I can make xml or json as default template but I don't want as it will effect non-restful requests.)
ContentTypeHandlerManager
, it won't help you . And the link is absolutely has nothing with your problem. I will mark this off-topic, nobody interestin to dig into Struts2 internals if any, without clear problem statement, without code, asking for XY problem. – Roman C