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>
xml spring jaxb
add a comment |
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>
xml spring jaxb
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
add a comment |
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>
xml spring jaxb
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
xml spring jaxb
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
add a comment |
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
add a comment |
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.
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
add a comment |
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.
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
add a comment |
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.
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
add a comment |
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.
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.
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
add a comment |
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
add a comment |
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%2f53194359%2fhow-to-exclude-root-tag-element-from-jaxb-wrapper%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
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