Reputation: 1395
There is spring-boot application(web+jpa) So, I have controller:
@RestController
public class CustomerController {
@Autowired
private CustomerService customerService;
@RequestMapping(value = "/customers", method = RequestMethod.GET)
public @ResponseBody List<Customer> findAllCustomers() {
return customerService.findAllCustomers();
}
@RequestMapping(value = "/customers", method = RequestMethod.POST)
public void addCustomer(@RequestBody Customer customer) {
customerService.addCustomer(customer);
}
Model:
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
@Entity
@Table(name="customer")
@XmlRootElement(name="customer")
public class Customer{
@Id
private String id;
private String name;
public Customer(String id, String name) {
this.id = id;
this.name = name;
}
public Customer() {
}
public String getId() {
return id;
}
@XmlElement
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
@XmlElement
public void setName(String name) {
this.name = name;
}
}
And service layer for bind jpa and rest.
When I do request:
<customer>
<id>first</id>
<name>first name of metric</name>
</customer>
it's okey, customer added in database, but, when I try to get all customers, the response in json format, but I expected xml. How to fix it issue?
Upvotes: 0
Views: 760
Reputation: 1395
Solved by adding
<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-xml</artifactId>
</dependency>
Upvotes: 0
Reputation: 991
I think that you use wrong accept type when you invoke rest method.
@ResponseBody
will automatically serialize the return value according to the external client's capabilities and the libraries available on the classpath. If Jackson is available on the classpath and the client has indicated that they can accept JSON, the return value will be automatically sent as JSON. If the JRE is 1.7 or higher (which means that JAXB is included with the JRE) and the client has indicated that they can accept XML, the return value will be automatically sent as XML.
Upvotes: 1
Reputation: 15878
Mark the controller method as producing application/xml
responses (produces = MediaType.APPLICATION_XML_VALUE
).
Upvotes: 1