1

I require to write a simple java webservice that could show its output in the form of a XML/JSON file.

For example the user will click a link or a button, and a simple SQL statement would get executed SELECT * FROM PERSON and the result of the above SQL query should be displayed in the form a XML/JSON file.

I have googled this several times but failed to find a suitable tutorial or a sample code. Can some one help me by providing a sample code or a tutorial that would help me.

Illep
  • 16,375
  • 46
  • 171
  • 302
  • 1
    [an example using Spring 3](http://www.informit.com/guides/content.aspx?g=java&seqNum=604) – smp7d Nov 22 '11 at 16:10

1 Answers1

0

You could do something like the following with JAX-RS:

package org.example;

import java.util.List;

import javax.ejb.*;
import javax.persistence.*;
import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;

@Stateless
@LocalBean
@Path("/customers")
public class CustomerService {

    @PersistenceContext(unitName="CustomerService",
                        type=PersistenceContextType.TRANSACTION)
    EntityManager entityManager;


    @GET
    @Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
    @Path("{id}")
    public Customer read(@PathParam("id") long id) {
        return entityManager.find(Customer.class, id);
    }

}

Full Example

bdoughan
  • 147,609
  • 23
  • 300
  • 400