Reputation: 361
I hava an Spring Boot Rest Api
@RestController
public class BookController {
@Autowired
private BookRepository bookRepo;
@GetMapping(value = "/library/", produces ={MediaType.APPLICATION_JSON_VALUE, MediaType.APPLICATION_XML_VALUE})
public List<Book> index(){
Iterable<Book> bookIterable = bookRepo.findAll();
List<Book> bookList = new ArrayList<>();
bookIterable.forEach(a->bookList.add(a));
return bookList;
}
My Homework is to add an additonal data representation so that when i put in the request i should can choose between which data representation i won't XML or JSON
I get even json how can i change between XML and Json when i do a get Request to the Endpoint
Upvotes: 0
Views: 121
Reputation: 361
Ok now i found it my self what you need to know in order to use an XML output is first add to the pom.xml file following dependencies: Jackson XML Dataformat
<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-xml</artifactId>
</dependency>
<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
<version>2.3.1</version>
</dependency>
Then i just have to Add Jackson Annotations to my Entity Class
@Entity
@JacksonXmlRootElement
public class Book {
public Book() {
}
@JacksonXmlProperty(isAttribute = true)
@Id
@GeneratedValue
private Integer id;
@JacksonXmlProperty(isAttribute = true)
private String title;
@JacksonXmlProperty(isAttribute = true)
private Integer numberOfCopies;
Thats it then i can Make a request with the Accept Header value application/xml
Upvotes: 0
Reputation: 36
To solve your problem you need to use the Accept
header. more details
The Content Type
header indicates the type of data that you pass in the request. more details
You need to make a request with the header, if you want to send and receive xml:
Accept: application/xml;
Content-Type: application/xml;
Upvotes: 2