return type xml or json in restful
I have some code below, and when I test it with postman. It occurs an error "500". I don't understand the advantage of "@Produces(MediaType.APPLICATION_XML)". Does it define return type automatically as XML, or not.
import java.sql.SQLException;
import java.util.List;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
@Path("/UserService")
public class UserService {
UserDAO userDAO = new UserDAO();
@GET
@Path("/users")
@Produces(MediaType.APPLICATION_XML)
public List<User> getUsers() throws ClassNotFoundException, SQLException {
return userDAO.getAllUsers();
}
}
java xml
add a comment |
I have some code below, and when I test it with postman. It occurs an error "500". I don't understand the advantage of "@Produces(MediaType.APPLICATION_XML)". Does it define return type automatically as XML, or not.
import java.sql.SQLException;
import java.util.List;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
@Path("/UserService")
public class UserService {
UserDAO userDAO = new UserDAO();
@GET
@Path("/users")
@Produces(MediaType.APPLICATION_XML)
public List<User> getUsers() throws ClassNotFoundException, SQLException {
return userDAO.getAllUsers();
}
}
java xml
docs.oracle.com/cd/E19226-01/820-7627/gipxf/index.html
– achAmháin
Nov 21 '18 at 10:23
Yes, this indicates an XML returned type. Note to use the XML accept header at your postman.
– G.Noulas
Nov 21 '18 at 10:26
add a comment |
I have some code below, and when I test it with postman. It occurs an error "500". I don't understand the advantage of "@Produces(MediaType.APPLICATION_XML)". Does it define return type automatically as XML, or not.
import java.sql.SQLException;
import java.util.List;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
@Path("/UserService")
public class UserService {
UserDAO userDAO = new UserDAO();
@GET
@Path("/users")
@Produces(MediaType.APPLICATION_XML)
public List<User> getUsers() throws ClassNotFoundException, SQLException {
return userDAO.getAllUsers();
}
}
java xml
I have some code below, and when I test it with postman. It occurs an error "500". I don't understand the advantage of "@Produces(MediaType.APPLICATION_XML)". Does it define return type automatically as XML, or not.
import java.sql.SQLException;
import java.util.List;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
@Path("/UserService")
public class UserService {
UserDAO userDAO = new UserDAO();
@GET
@Path("/users")
@Produces(MediaType.APPLICATION_XML)
public List<User> getUsers() throws ClassNotFoundException, SQLException {
return userDAO.getAllUsers();
}
}
java xml
java xml
edited Nov 21 '18 at 11:36
veben
2,01721125
2,01721125
asked Nov 21 '18 at 10:21
long phamlong pham
235
235
docs.oracle.com/cd/E19226-01/820-7627/gipxf/index.html
– achAmháin
Nov 21 '18 at 10:23
Yes, this indicates an XML returned type. Note to use the XML accept header at your postman.
– G.Noulas
Nov 21 '18 at 10:26
add a comment |
docs.oracle.com/cd/E19226-01/820-7627/gipxf/index.html
– achAmháin
Nov 21 '18 at 10:23
Yes, this indicates an XML returned type. Note to use the XML accept header at your postman.
– G.Noulas
Nov 21 '18 at 10:26
docs.oracle.com/cd/E19226-01/820-7627/gipxf/index.html
– achAmháin
Nov 21 '18 at 10:23
docs.oracle.com/cd/E19226-01/820-7627/gipxf/index.html
– achAmháin
Nov 21 '18 at 10:23
Yes, this indicates an XML returned type. Note to use the XML accept header at your postman.
– G.Noulas
Nov 21 '18 at 10:26
Yes, this indicates an XML returned type. Note to use the XML accept header at your postman.
– G.Noulas
Nov 21 '18 at 10:26
add a comment |
4 Answers
4
active
oldest
votes
Yes, @Produce
define the produced format. So your list of users will be format as XML.
Don't forget to parametrize Postman header to accept XML.
add a comment |
It specifies the content-types (yes plural!) that this method produces (hence the name). This is used to
- select the correct method to execute for an incoming request
- determine what to produce as a response.
In your case when an incoming request needs JSON you will get a HTTP 406 as there is nothing that can handle this method.
Now if the method would have been annotated with @Produces( {MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON} )
it would be served and JSON would have been produced. Now you have single method serving both JSON and XML. What to serve is determined based on the Accept-Header
of the incoming request.
add a comment |
As your method, result return in xml format.
add a comment |
//To process HTTP GET requests.
@GET
//@Path Identifies the URI path that a resource class will serve requests for.
@Path("/abcd")
//@Produces defines the media type(s) that the methods of a resource class can produce.
@Produces(MediaType.APPLICATION_XML
I hope you have prepared the User class - with XmlRootElement and XML elements
for an example -
@XmlRootElement(name="User")
public class User{
private int id;
private String name;
public User() {
}
@XmlElement
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
@XmlElement
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
thanks so much. I understanded. :))
– long pham
Nov 21 '18 at 14:15
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53409911%2freturn-type-xml-or-json-in-restful%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
Yes, @Produce
define the produced format. So your list of users will be format as XML.
Don't forget to parametrize Postman header to accept XML.
add a comment |
Yes, @Produce
define the produced format. So your list of users will be format as XML.
Don't forget to parametrize Postman header to accept XML.
add a comment |
Yes, @Produce
define the produced format. So your list of users will be format as XML.
Don't forget to parametrize Postman header to accept XML.
Yes, @Produce
define the produced format. So your list of users will be format as XML.
Don't forget to parametrize Postman header to accept XML.
answered Nov 21 '18 at 10:33
vebenveben
2,01721125
2,01721125
add a comment |
add a comment |
It specifies the content-types (yes plural!) that this method produces (hence the name). This is used to
- select the correct method to execute for an incoming request
- determine what to produce as a response.
In your case when an incoming request needs JSON you will get a HTTP 406 as there is nothing that can handle this method.
Now if the method would have been annotated with @Produces( {MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON} )
it would be served and JSON would have been produced. Now you have single method serving both JSON and XML. What to serve is determined based on the Accept-Header
of the incoming request.
add a comment |
It specifies the content-types (yes plural!) that this method produces (hence the name). This is used to
- select the correct method to execute for an incoming request
- determine what to produce as a response.
In your case when an incoming request needs JSON you will get a HTTP 406 as there is nothing that can handle this method.
Now if the method would have been annotated with @Produces( {MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON} )
it would be served and JSON would have been produced. Now you have single method serving both JSON and XML. What to serve is determined based on the Accept-Header
of the incoming request.
add a comment |
It specifies the content-types (yes plural!) that this method produces (hence the name). This is used to
- select the correct method to execute for an incoming request
- determine what to produce as a response.
In your case when an incoming request needs JSON you will get a HTTP 406 as there is nothing that can handle this method.
Now if the method would have been annotated with @Produces( {MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON} )
it would be served and JSON would have been produced. Now you have single method serving both JSON and XML. What to serve is determined based on the Accept-Header
of the incoming request.
It specifies the content-types (yes plural!) that this method produces (hence the name). This is used to
- select the correct method to execute for an incoming request
- determine what to produce as a response.
In your case when an incoming request needs JSON you will get a HTTP 406 as there is nothing that can handle this method.
Now if the method would have been annotated with @Produces( {MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON} )
it would be served and JSON would have been produced. Now you have single method serving both JSON and XML. What to serve is determined based on the Accept-Header
of the incoming request.
answered Nov 21 '18 at 12:23
M. DeinumM. Deinum
69.3k13139149
69.3k13139149
add a comment |
add a comment |
As your method, result return in xml format.
add a comment |
As your method, result return in xml format.
add a comment |
As your method, result return in xml format.
As your method, result return in xml format.
answered Nov 21 '18 at 10:29
WGSSAMINTHAWGSSAMINTHA
10210
10210
add a comment |
add a comment |
//To process HTTP GET requests.
@GET
//@Path Identifies the URI path that a resource class will serve requests for.
@Path("/abcd")
//@Produces defines the media type(s) that the methods of a resource class can produce.
@Produces(MediaType.APPLICATION_XML
I hope you have prepared the User class - with XmlRootElement and XML elements
for an example -
@XmlRootElement(name="User")
public class User{
private int id;
private String name;
public User() {
}
@XmlElement
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
@XmlElement
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
thanks so much. I understanded. :))
– long pham
Nov 21 '18 at 14:15
add a comment |
//To process HTTP GET requests.
@GET
//@Path Identifies the URI path that a resource class will serve requests for.
@Path("/abcd")
//@Produces defines the media type(s) that the methods of a resource class can produce.
@Produces(MediaType.APPLICATION_XML
I hope you have prepared the User class - with XmlRootElement and XML elements
for an example -
@XmlRootElement(name="User")
public class User{
private int id;
private String name;
public User() {
}
@XmlElement
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
@XmlElement
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
thanks so much. I understanded. :))
– long pham
Nov 21 '18 at 14:15
add a comment |
//To process HTTP GET requests.
@GET
//@Path Identifies the URI path that a resource class will serve requests for.
@Path("/abcd")
//@Produces defines the media type(s) that the methods of a resource class can produce.
@Produces(MediaType.APPLICATION_XML
I hope you have prepared the User class - with XmlRootElement and XML elements
for an example -
@XmlRootElement(name="User")
public class User{
private int id;
private String name;
public User() {
}
@XmlElement
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
@XmlElement
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
//To process HTTP GET requests.
@GET
//@Path Identifies the URI path that a resource class will serve requests for.
@Path("/abcd")
//@Produces defines the media type(s) that the methods of a resource class can produce.
@Produces(MediaType.APPLICATION_XML
I hope you have prepared the User class - with XmlRootElement and XML elements
for an example -
@XmlRootElement(name="User")
public class User{
private int id;
private String name;
public User() {
}
@XmlElement
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
@XmlElement
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
answered Nov 21 '18 at 12:11
Ashish ShetkarAshish Shetkar
475616
475616
thanks so much. I understanded. :))
– long pham
Nov 21 '18 at 14:15
add a comment |
thanks so much. I understanded. :))
– long pham
Nov 21 '18 at 14:15
thanks so much. I understanded. :))
– long pham
Nov 21 '18 at 14:15
thanks so much. I understanded. :))
– long pham
Nov 21 '18 at 14:15
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53409911%2freturn-type-xml-or-json-in-restful%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
docs.oracle.com/cd/E19226-01/820-7627/gipxf/index.html
– achAmháin
Nov 21 '18 at 10:23
Yes, this indicates an XML returned type. Note to use the XML accept header at your postman.
– G.Noulas
Nov 21 '18 at 10:26