return type xml or json in restful












2















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();
}

}









share|improve this question

























  • 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
















2















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();
}

}









share|improve this question

























  • 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














2












2








2








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();
}

}









share|improve this question
















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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



















  • 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












4 Answers
4






active

oldest

votes


















1














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.






share|improve this answer































    1














    It specifies the content-types (yes plural!) that this method produces (hence the name). This is used to




    1. select the correct method to execute for an incoming request

    2. 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.






    share|improve this answer































      0














      As your method, result return in xml format.






      share|improve this answer































        0














        //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;
        }
        }





        share|improve this answer
























        • thanks so much. I understanded. :))

          – long pham
          Nov 21 '18 at 14:15











        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
        });


        }
        });














        draft saved

        draft discarded


















        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









        1














        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.






        share|improve this answer




























          1














          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.






          share|improve this answer


























            1












            1








            1







            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.






            share|improve this answer













            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.







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 21 '18 at 10:33









            vebenveben

            2,01721125




            2,01721125

























                1














                It specifies the content-types (yes plural!) that this method produces (hence the name). This is used to




                1. select the correct method to execute for an incoming request

                2. 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.






                share|improve this answer




























                  1














                  It specifies the content-types (yes plural!) that this method produces (hence the name). This is used to




                  1. select the correct method to execute for an incoming request

                  2. 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.






                  share|improve this answer


























                    1












                    1








                    1







                    It specifies the content-types (yes plural!) that this method produces (hence the name). This is used to




                    1. select the correct method to execute for an incoming request

                    2. 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.






                    share|improve this answer













                    It specifies the content-types (yes plural!) that this method produces (hence the name). This is used to




                    1. select the correct method to execute for an incoming request

                    2. 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.







                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Nov 21 '18 at 12:23









                    M. DeinumM. Deinum

                    69.3k13139149




                    69.3k13139149























                        0














                        As your method, result return in xml format.






                        share|improve this answer




























                          0














                          As your method, result return in xml format.






                          share|improve this answer


























                            0












                            0








                            0







                            As your method, result return in xml format.






                            share|improve this answer













                            As your method, result return in xml format.







                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Nov 21 '18 at 10:29









                            WGSSAMINTHAWGSSAMINTHA

                            10210




                            10210























                                0














                                //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;
                                }
                                }





                                share|improve this answer
























                                • thanks so much. I understanded. :))

                                  – long pham
                                  Nov 21 '18 at 14:15
















                                0














                                //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;
                                }
                                }





                                share|improve this answer
























                                • thanks so much. I understanded. :))

                                  – long pham
                                  Nov 21 '18 at 14:15














                                0












                                0








                                0







                                //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;
                                }
                                }





                                share|improve this answer













                                //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;
                                }
                                }






                                share|improve this answer












                                share|improve this answer



                                share|improve this answer










                                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



















                                • 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


















                                draft saved

                                draft discarded




















































                                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.




                                draft saved


                                draft discarded














                                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





















































                                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







                                Popular posts from this blog

                                Can a sorcerer learn a 5th-level spell early by creating spell slots using the Font of Magic feature?

                                Does disintegrating a polymorphed enemy still kill it after the 2018 errata?

                                A Topological Invariant for $pi_3(U(n))$