How to exclude root tag -element from jaxb wrapper?











up vote
0
down vote

favorite












There is app on spring+jaxb+jpa



So, I have rest - controllers and jaxv Elements:



@AllArgsConstructor
@NoArgsConstructor
@XmlRootElement(name = "root")
@Getter
public class UserWrapper {
@XmlElementWrapper(name = "Users")
@XmlElement(name = "User")
private List<User> Users;

@XmlElement(name="UserError")
private UserError error;
}


@Getter
@Setter
@XmlRootElement(name="User")
@NoArgsConstructor
public class User{
@XmlElement
private String name;
@XmlElement
private String surname;
}


There are User entity and Wrapper for contain List of Users.



Response from my controller is:



<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<root>
<Users>
<User>
<Name>AAA</Name>
<Surname>AAA</Surname>
</User>
<User>
<Name>BBB</Name>
<Surname>BBB</Surname>
</User>
</Users>
</root>


How to make response without tag ?



Rest controller is:



@RequestMapping(value = "/users", method = RequestMethod.GET)
public @ResponseBody UsersWrapper findByParams(
@RequestParam(value = "id") String id) throws Exception {
RiskMetricError error = null;
List<User> users = userService.find(id);

return (new UsersWrapper(users, error));
}


P.S.
I need to make response xml like this:



        <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Users>
<User>
<Name>AAA</Name>
<Surname>AAA</Surname>
</User>
<User>
<Name>BBB</Name>
<Surname>BBB</Surname>
</User>
</Users>









share|improve this question




















  • 1




    may you clarify what you mean by "How to make response without tag ?"; how should the response be?
    – Angelo Immediata
    Nov 7 at 17:45










  • @AngeloImmediata updated.
    – Vladislav Osipenkov
    Nov 8 at 7:53










  • @AngeloImmediata yes, but there is error message :org.springframework.http.converter.HttpMessageNotWritableException: No converter found for return value of type: class java.util.Collections$SingletonList
    – Vladislav Osipenkov
    Nov 8 at 7:58












  • So, on stackoverflow I accept advise - use wrapper.
    – Vladislav Osipenkov
    Nov 8 at 7:59






  • 1




    I was wrong so i deleted my comment... I'll think on your issue and I'll let you know... sorry for the mistake
    – Angelo Immediata
    Nov 8 at 8:00















up vote
0
down vote

favorite












There is app on spring+jaxb+jpa



So, I have rest - controllers and jaxv Elements:



@AllArgsConstructor
@NoArgsConstructor
@XmlRootElement(name = "root")
@Getter
public class UserWrapper {
@XmlElementWrapper(name = "Users")
@XmlElement(name = "User")
private List<User> Users;

@XmlElement(name="UserError")
private UserError error;
}


@Getter
@Setter
@XmlRootElement(name="User")
@NoArgsConstructor
public class User{
@XmlElement
private String name;
@XmlElement
private String surname;
}


There are User entity and Wrapper for contain List of Users.



Response from my controller is:



<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<root>
<Users>
<User>
<Name>AAA</Name>
<Surname>AAA</Surname>
</User>
<User>
<Name>BBB</Name>
<Surname>BBB</Surname>
</User>
</Users>
</root>


How to make response without tag ?



Rest controller is:



@RequestMapping(value = "/users", method = RequestMethod.GET)
public @ResponseBody UsersWrapper findByParams(
@RequestParam(value = "id") String id) throws Exception {
RiskMetricError error = null;
List<User> users = userService.find(id);

return (new UsersWrapper(users, error));
}


P.S.
I need to make response xml like this:



        <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Users>
<User>
<Name>AAA</Name>
<Surname>AAA</Surname>
</User>
<User>
<Name>BBB</Name>
<Surname>BBB</Surname>
</User>
</Users>









share|improve this question




















  • 1




    may you clarify what you mean by "How to make response without tag ?"; how should the response be?
    – Angelo Immediata
    Nov 7 at 17:45










  • @AngeloImmediata updated.
    – Vladislav Osipenkov
    Nov 8 at 7:53










  • @AngeloImmediata yes, but there is error message :org.springframework.http.converter.HttpMessageNotWritableException: No converter found for return value of type: class java.util.Collections$SingletonList
    – Vladislav Osipenkov
    Nov 8 at 7:58












  • So, on stackoverflow I accept advise - use wrapper.
    – Vladislav Osipenkov
    Nov 8 at 7:59






  • 1




    I was wrong so i deleted my comment... I'll think on your issue and I'll let you know... sorry for the mistake
    – Angelo Immediata
    Nov 8 at 8:00













up vote
0
down vote

favorite









up vote
0
down vote

favorite











There is app on spring+jaxb+jpa



So, I have rest - controllers and jaxv Elements:



@AllArgsConstructor
@NoArgsConstructor
@XmlRootElement(name = "root")
@Getter
public class UserWrapper {
@XmlElementWrapper(name = "Users")
@XmlElement(name = "User")
private List<User> Users;

@XmlElement(name="UserError")
private UserError error;
}


@Getter
@Setter
@XmlRootElement(name="User")
@NoArgsConstructor
public class User{
@XmlElement
private String name;
@XmlElement
private String surname;
}


There are User entity and Wrapper for contain List of Users.



Response from my controller is:



<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<root>
<Users>
<User>
<Name>AAA</Name>
<Surname>AAA</Surname>
</User>
<User>
<Name>BBB</Name>
<Surname>BBB</Surname>
</User>
</Users>
</root>


How to make response without tag ?



Rest controller is:



@RequestMapping(value = "/users", method = RequestMethod.GET)
public @ResponseBody UsersWrapper findByParams(
@RequestParam(value = "id") String id) throws Exception {
RiskMetricError error = null;
List<User> users = userService.find(id);

return (new UsersWrapper(users, error));
}


P.S.
I need to make response xml like this:



        <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Users>
<User>
<Name>AAA</Name>
<Surname>AAA</Surname>
</User>
<User>
<Name>BBB</Name>
<Surname>BBB</Surname>
</User>
</Users>









share|improve this question















There is app on spring+jaxb+jpa



So, I have rest - controllers and jaxv Elements:



@AllArgsConstructor
@NoArgsConstructor
@XmlRootElement(name = "root")
@Getter
public class UserWrapper {
@XmlElementWrapper(name = "Users")
@XmlElement(name = "User")
private List<User> Users;

@XmlElement(name="UserError")
private UserError error;
}


@Getter
@Setter
@XmlRootElement(name="User")
@NoArgsConstructor
public class User{
@XmlElement
private String name;
@XmlElement
private String surname;
}


There are User entity and Wrapper for contain List of Users.



Response from my controller is:



<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<root>
<Users>
<User>
<Name>AAA</Name>
<Surname>AAA</Surname>
</User>
<User>
<Name>BBB</Name>
<Surname>BBB</Surname>
</User>
</Users>
</root>


How to make response without tag ?



Rest controller is:



@RequestMapping(value = "/users", method = RequestMethod.GET)
public @ResponseBody UsersWrapper findByParams(
@RequestParam(value = "id") String id) throws Exception {
RiskMetricError error = null;
List<User> users = userService.find(id);

return (new UsersWrapper(users, error));
}


P.S.
I need to make response xml like this:



        <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Users>
<User>
<Name>AAA</Name>
<Surname>AAA</Surname>
</User>
<User>
<Name>BBB</Name>
<Surname>BBB</Surname>
</User>
</Users>






xml spring jaxb






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 8 at 7:53

























asked Nov 7 at 17:06









Vladislav Osipenkov

251114




251114








  • 1




    may you clarify what you mean by "How to make response without tag ?"; how should the response be?
    – Angelo Immediata
    Nov 7 at 17:45










  • @AngeloImmediata updated.
    – Vladislav Osipenkov
    Nov 8 at 7:53










  • @AngeloImmediata yes, but there is error message :org.springframework.http.converter.HttpMessageNotWritableException: No converter found for return value of type: class java.util.Collections$SingletonList
    – Vladislav Osipenkov
    Nov 8 at 7:58












  • So, on stackoverflow I accept advise - use wrapper.
    – Vladislav Osipenkov
    Nov 8 at 7:59






  • 1




    I was wrong so i deleted my comment... I'll think on your issue and I'll let you know... sorry for the mistake
    – Angelo Immediata
    Nov 8 at 8:00














  • 1




    may you clarify what you mean by "How to make response without tag ?"; how should the response be?
    – Angelo Immediata
    Nov 7 at 17:45










  • @AngeloImmediata updated.
    – Vladislav Osipenkov
    Nov 8 at 7:53










  • @AngeloImmediata yes, but there is error message :org.springframework.http.converter.HttpMessageNotWritableException: No converter found for return value of type: class java.util.Collections$SingletonList
    – Vladislav Osipenkov
    Nov 8 at 7:58












  • So, on stackoverflow I accept advise - use wrapper.
    – Vladislav Osipenkov
    Nov 8 at 7:59






  • 1




    I was wrong so i deleted my comment... I'll think on your issue and I'll let you know... sorry for the mistake
    – Angelo Immediata
    Nov 8 at 8:00








1




1




may you clarify what you mean by "How to make response without tag ?"; how should the response be?
– Angelo Immediata
Nov 7 at 17:45




may you clarify what you mean by "How to make response without tag ?"; how should the response be?
– Angelo Immediata
Nov 7 at 17:45












@AngeloImmediata updated.
– Vladislav Osipenkov
Nov 8 at 7:53




@AngeloImmediata updated.
– Vladislav Osipenkov
Nov 8 at 7:53












@AngeloImmediata yes, but there is error message :org.springframework.http.converter.HttpMessageNotWritableException: No converter found for return value of type: class java.util.Collections$SingletonList
– Vladislav Osipenkov
Nov 8 at 7:58






@AngeloImmediata yes, but there is error message :org.springframework.http.converter.HttpMessageNotWritableException: No converter found for return value of type: class java.util.Collections$SingletonList
– Vladislav Osipenkov
Nov 8 at 7:58














So, on stackoverflow I accept advise - use wrapper.
– Vladislav Osipenkov
Nov 8 at 7:59




So, on stackoverflow I accept advise - use wrapper.
– Vladislav Osipenkov
Nov 8 at 7:59




1




1




I was wrong so i deleted my comment... I'll think on your issue and I'll let you know... sorry for the mistake
– Angelo Immediata
Nov 8 at 8:00




I was wrong so i deleted my comment... I'll think on your issue and I'll let you know... sorry for the mistake
– Angelo Immediata
Nov 8 at 8:00












1 Answer
1






active

oldest

votes

















up vote
3
down vote



accepted










Comparing the two xmls (actual vs desired) you do not want to have the users content within the <root>. You can do this:



@XmlRootElement(name = "Users")
@NoArgsConstructor
@AllArgsConstructor
@XmlAccessorType(XmlAccessType.FIELD)
public class Users {

@XmlElement(name = "User")
private List<User> users;
}


Then User will be:



@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@XmlAccessorType(XmlAccessType.FIELD)
public class User {

@XmlElement(name = "Name")
private String name;
@XmlElement(name = "Surname")
private String surname;
}


A small spring boot application to try it out:



@RestController
public class UsersEndpoint {

@GetMapping("/users")
public Users getUsers() {
List<User> users = new ArrayList<>();
users.add(new User("name1", "surname1"));
users.add(new User("name2", "surname2"));

return new Users(users);
}
}


Will return this:



<Users>
<User>
<Name>name1</Name>
<Surname>surname1</Surname>
</User>
<User>
<Name>name2</Name>
<Surname>surname2</Surname>
</User>
</Users>


Update to reply to comment (elaborate on the answer):



Your POJOs and your xml structure should match. The annotations help "smooth" differences. You had <root></root> because your root class UserWrapper had this annotation: @XmlRootElement(name = "root"). If in the name you had "blah" the external tags (<root></root>) would be <blah></blah>. Additionally above your list of users you had this annotation: @XmlElementWrapper(name = "Users"). This created an extra wrapper element outside your list elements with the provided name.



So what I did is properly name the root element and remove the extra wrapper element creation.






share|improve this answer























  • Can you prodive links to read for understand this issue?
    – Vladislav Osipenkov
    Nov 8 at 10:12










  • @VladislavOsipenkov I will update the answer with an explanation
    – mart
    Nov 8 at 10:33











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',
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%2f53194359%2fhow-to-exclude-root-tag-element-from-jaxb-wrapper%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
3
down vote



accepted










Comparing the two xmls (actual vs desired) you do not want to have the users content within the <root>. You can do this:



@XmlRootElement(name = "Users")
@NoArgsConstructor
@AllArgsConstructor
@XmlAccessorType(XmlAccessType.FIELD)
public class Users {

@XmlElement(name = "User")
private List<User> users;
}


Then User will be:



@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@XmlAccessorType(XmlAccessType.FIELD)
public class User {

@XmlElement(name = "Name")
private String name;
@XmlElement(name = "Surname")
private String surname;
}


A small spring boot application to try it out:



@RestController
public class UsersEndpoint {

@GetMapping("/users")
public Users getUsers() {
List<User> users = new ArrayList<>();
users.add(new User("name1", "surname1"));
users.add(new User("name2", "surname2"));

return new Users(users);
}
}


Will return this:



<Users>
<User>
<Name>name1</Name>
<Surname>surname1</Surname>
</User>
<User>
<Name>name2</Name>
<Surname>surname2</Surname>
</User>
</Users>


Update to reply to comment (elaborate on the answer):



Your POJOs and your xml structure should match. The annotations help "smooth" differences. You had <root></root> because your root class UserWrapper had this annotation: @XmlRootElement(name = "root"). If in the name you had "blah" the external tags (<root></root>) would be <blah></blah>. Additionally above your list of users you had this annotation: @XmlElementWrapper(name = "Users"). This created an extra wrapper element outside your list elements with the provided name.



So what I did is properly name the root element and remove the extra wrapper element creation.






share|improve this answer























  • Can you prodive links to read for understand this issue?
    – Vladislav Osipenkov
    Nov 8 at 10:12










  • @VladislavOsipenkov I will update the answer with an explanation
    – mart
    Nov 8 at 10:33















up vote
3
down vote



accepted










Comparing the two xmls (actual vs desired) you do not want to have the users content within the <root>. You can do this:



@XmlRootElement(name = "Users")
@NoArgsConstructor
@AllArgsConstructor
@XmlAccessorType(XmlAccessType.FIELD)
public class Users {

@XmlElement(name = "User")
private List<User> users;
}


Then User will be:



@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@XmlAccessorType(XmlAccessType.FIELD)
public class User {

@XmlElement(name = "Name")
private String name;
@XmlElement(name = "Surname")
private String surname;
}


A small spring boot application to try it out:



@RestController
public class UsersEndpoint {

@GetMapping("/users")
public Users getUsers() {
List<User> users = new ArrayList<>();
users.add(new User("name1", "surname1"));
users.add(new User("name2", "surname2"));

return new Users(users);
}
}


Will return this:



<Users>
<User>
<Name>name1</Name>
<Surname>surname1</Surname>
</User>
<User>
<Name>name2</Name>
<Surname>surname2</Surname>
</User>
</Users>


Update to reply to comment (elaborate on the answer):



Your POJOs and your xml structure should match. The annotations help "smooth" differences. You had <root></root> because your root class UserWrapper had this annotation: @XmlRootElement(name = "root"). If in the name you had "blah" the external tags (<root></root>) would be <blah></blah>. Additionally above your list of users you had this annotation: @XmlElementWrapper(name = "Users"). This created an extra wrapper element outside your list elements with the provided name.



So what I did is properly name the root element and remove the extra wrapper element creation.






share|improve this answer























  • Can you prodive links to read for understand this issue?
    – Vladislav Osipenkov
    Nov 8 at 10:12










  • @VladislavOsipenkov I will update the answer with an explanation
    – mart
    Nov 8 at 10:33













up vote
3
down vote



accepted







up vote
3
down vote



accepted






Comparing the two xmls (actual vs desired) you do not want to have the users content within the <root>. You can do this:



@XmlRootElement(name = "Users")
@NoArgsConstructor
@AllArgsConstructor
@XmlAccessorType(XmlAccessType.FIELD)
public class Users {

@XmlElement(name = "User")
private List<User> users;
}


Then User will be:



@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@XmlAccessorType(XmlAccessType.FIELD)
public class User {

@XmlElement(name = "Name")
private String name;
@XmlElement(name = "Surname")
private String surname;
}


A small spring boot application to try it out:



@RestController
public class UsersEndpoint {

@GetMapping("/users")
public Users getUsers() {
List<User> users = new ArrayList<>();
users.add(new User("name1", "surname1"));
users.add(new User("name2", "surname2"));

return new Users(users);
}
}


Will return this:



<Users>
<User>
<Name>name1</Name>
<Surname>surname1</Surname>
</User>
<User>
<Name>name2</Name>
<Surname>surname2</Surname>
</User>
</Users>


Update to reply to comment (elaborate on the answer):



Your POJOs and your xml structure should match. The annotations help "smooth" differences. You had <root></root> because your root class UserWrapper had this annotation: @XmlRootElement(name = "root"). If in the name you had "blah" the external tags (<root></root>) would be <blah></blah>. Additionally above your list of users you had this annotation: @XmlElementWrapper(name = "Users"). This created an extra wrapper element outside your list elements with the provided name.



So what I did is properly name the root element and remove the extra wrapper element creation.






share|improve this answer














Comparing the two xmls (actual vs desired) you do not want to have the users content within the <root>. You can do this:



@XmlRootElement(name = "Users")
@NoArgsConstructor
@AllArgsConstructor
@XmlAccessorType(XmlAccessType.FIELD)
public class Users {

@XmlElement(name = "User")
private List<User> users;
}


Then User will be:



@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@XmlAccessorType(XmlAccessType.FIELD)
public class User {

@XmlElement(name = "Name")
private String name;
@XmlElement(name = "Surname")
private String surname;
}


A small spring boot application to try it out:



@RestController
public class UsersEndpoint {

@GetMapping("/users")
public Users getUsers() {
List<User> users = new ArrayList<>();
users.add(new User("name1", "surname1"));
users.add(new User("name2", "surname2"));

return new Users(users);
}
}


Will return this:



<Users>
<User>
<Name>name1</Name>
<Surname>surname1</Surname>
</User>
<User>
<Name>name2</Name>
<Surname>surname2</Surname>
</User>
</Users>


Update to reply to comment (elaborate on the answer):



Your POJOs and your xml structure should match. The annotations help "smooth" differences. You had <root></root> because your root class UserWrapper had this annotation: @XmlRootElement(name = "root"). If in the name you had "blah" the external tags (<root></root>) would be <blah></blah>. Additionally above your list of users you had this annotation: @XmlElementWrapper(name = "Users"). This created an extra wrapper element outside your list elements with the provided name.



So what I did is properly name the root element and remove the extra wrapper element creation.







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 8 at 10:37

























answered Nov 8 at 8:56









mart

1,54257




1,54257












  • Can you prodive links to read for understand this issue?
    – Vladislav Osipenkov
    Nov 8 at 10:12










  • @VladislavOsipenkov I will update the answer with an explanation
    – mart
    Nov 8 at 10:33


















  • Can you prodive links to read for understand this issue?
    – Vladislav Osipenkov
    Nov 8 at 10:12










  • @VladislavOsipenkov I will update the answer with an explanation
    – mart
    Nov 8 at 10:33
















Can you prodive links to read for understand this issue?
– Vladislav Osipenkov
Nov 8 at 10:12




Can you prodive links to read for understand this issue?
– Vladislav Osipenkov
Nov 8 at 10:12












@VladislavOsipenkov I will update the answer with an explanation
– mart
Nov 8 at 10:33




@VladislavOsipenkov I will update the answer with an explanation
– mart
Nov 8 at 10:33


















 

draft saved


draft discarded



















































 


draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53194359%2fhow-to-exclude-root-tag-element-from-jaxb-wrapper%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







這個網誌中的熱門文章

Tangent Lines Diagram Along Smooth Curve

Yusuf al-Mu'taman ibn Hud

Zucchini