0

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?

1
  • You cannot expose a list of XML. You will need a wrapper which is XML as well. The XML to be returned has to be valid XML. Commented Sep 19, 2018 at 8:49

3 Answers 3

1

Mark the controller method as producing application/xml responses (produces = MediaType.APPLICATION_XML_VALUE).

Sign up to request clarification or add additional context in comments.

2 Comments

HTTP Status 406 – Not Acceptable Description The target resource does not have a current representation that would be acceptable to the user agent, according to the proactive negotiation header fields received in the request, and the server is unwilling to supply a default representation.
your request's header should contain "Accept" and its value is "application/xml"
1

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.

3 Comments

If I use Postman for testing, what it mean for me? Add header Content-Type: application/xml ?
you need to add Request header Accepts set to application/xml.
@VladislavOsipenkov you can read this full answer here stackoverflow.com/a/27793306/6572971
0

Solved by adding

    <dependency>
        <groupId>com.fasterxml.jackson.dataformat</groupId>
        <artifactId>jackson-dataformat-xml</artifactId>
    </dependency>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.