简体   繁体   中英

Not getting json response in rest service java eclipse

i am buliding rest webservices in eclipse mars,by creating maven project by following this link http://javapapers.com/java/java-restful-web-services-with-json-and-jersey/ as mentioned in this without putting any dependencies in pom.xml as mentioned in link i get xml response but no json response.so when i put dependencies in pom.xml file and run i get 500 internal server error(at this time i wont get both xml and json response) in postman.

Product.java

 package com.star.sample.ProductService.Productsample;

import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement
public class Product {
private String id;
private String name;
private String category;

public Product() {

}

public Product(String id, String name, String category) {
    super();
    this.id = id;
    this.name = name;
    this.category = category;
}

public String getId() {
    return id;
}

public void setId(String id) {
    this.id = id;
}

public String getName() {
    return name;
}}

ProductDao.java

package com.star.sample.ProductService.Productsample;

import java.util.HashMap;
import java.util.Map;

public enum ProductDao {
instance;

private Map<String, Product> products = new HashMap<String, Product>();

private ProductDao() {

    // pumping-in some default data
    Product product = new Product("1001", "iPhone 5S", "Mobile");
    products.put("1", product);

    product = new Product("1002", "Sony MDR-XD200", "Headphone");
    products.put("2", product);

    product = new Product("1003", "Bose Wave II", "Home Audio");
    products.put("3", product);
}

public Map<String, Product> getProducts() {
    return products;
}}

ProductResource .java

 package com.star.sample.ProductService.Productsample;
public class ProductResource {

@Context
UriInfo uriInfo;

@Context
Request request;
String id;

ProductService productService;

public ProductResource(UriInfo uriInfo, Request request, String id) {
    this.uriInfo = uriInfo;
    this.request = request;
    this.id = id;
    productService = new ProductService();
}

@GET
@Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
public Product getProduct() {
    Product product = productService.getProduct(id);
    return product;
}

@GET
@Produces(MediaType.TEXT_XML)
public Product getProductAsHtml() {
    Product product = productService.getProduct(id);
    return product;
}

@PUT
@Consumes(MediaType.APPLICATION_XML)
public Response putProduct(JAXBElement<Product> productElement) {
    Product product = productElement.getValue();
    Response response;
    if (productService.getProducts().containsKey(product.getId())) {
        response = Response.noContent().build();
    } else {
        response = Response.created(uriInfo.getAbsolutePath()).build();
    }
    productService.createProduct(product);
    return response;
}

@DELETE
public void deleteProduct() {
    productService.deleteProduct(id);
}

 }

ProductService .java

 package com.star.sample.ProductService.Productsample;
import java.util.ArrayList;
import java.util.List;
 import java.util.Map;

 public class ProductService {

ProductDao productDao;

public ProductService() {
    productDao = ProductDao.instance;
}

public void createProduct(Product product) {
    productDao.getProducts().put(product.getId(), product);
}

public Product getProduct(String id) {
    return productDao.getProducts().get(id);
}

public Map<String, Product> getProducts() {
    return productDao.getProducts();
}

public List<Product> getProductAsList() {
    List<Product> productList = new ArrayList<Product>();
    productList.addAll(productDao.getProducts().values());
    return productList;
}

public int getProductsCount() {
    return productDao.getProducts().size();
}

public void deleteProduct(String id) {
    productDao.getProducts().remove(id);
}

}

ProductsResource .java

package com.star.sample.ProductService.Productsample;

@Path("/products")
public class ProductsResource {

@Context
UriInfo uriInfo;

@Context
Request request;

ProductService productService;

public ProductsResource() {
    productService = new ProductService();
}

@GET
@Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
public List<Product> getProducts() {
    return productService.getProductAsList();
}

@GET
@Produces(MediaType.TEXT_XML)
public List<Product> getProductsAsHtml() {
    return productService.getProductAsList();
}

// URI: /rest/products/count
@GET
@Path("count")
@Produces(MediaType.TEXT_PLAIN)
public String getCount() {
    return String.valueOf(productService.getProductsCount());
}

@POST
@Produces(MediaType.TEXT_HTML)
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
public void createProduct(@FormParam("id") String id,
        @FormParam("productname") String name,
        @FormParam("productcategory") String category,
        @Context HttpServletResponse servletResponse) throws IOException {
    Product product = new Product(id, name, category);
    productService.createProduct(product);
    servletResponse.sendRedirect("./products/");
}

@Path("{product}")
public ProductResource getProduct(@PathParam("product") String id) {
    return new ProductResource(uriInfo, request, id);
}

}

POM.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="http://maven.apache.org/POM/4.0.0    http://maven.apache.org/maven-v4_0_0.xsd">
 <modelVersion>4.0.0</modelVersion>
<groupId>com.star.sample</groupId>
<artifactId>ProductService</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>ProductService Jersey Webapp</name>
<build>
    <finalName>ProductService</finalName>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <inherited>true</inherited>
            <configuration>
                <source>1.6</source>
                <target>1.6</target>
            </configuration>
        </plugin>
        <!-- Run the application using "mvn embedded-glassfish:run" -->
        <plugin>
            <groupId>org.glassfish</groupId>
            <artifactId>maven-embedded-glassfish-plugin</artifactId>
            <version>${glassfish.version}</version>
            <configuration>
                <goalPrefix>embedded-glassfish</goalPrefix>
                    <app>C:/Users/star/eclipseworkspace/target/${project.build.finalName}.war</app>
                <autoDelete>true</autoDelete>
                <port>8080</port>
            </configuration>
            <dependencies>
                <dependency>
                    <groupId>com.sun.jersey</groupId>
                    <artifactId>jersey-servlet</artifactId>
                    <version>${project.version}</version>
                </dependency>
                <dependency>
                    <groupId>javax.servlet</groupId>
                    <artifactId>javax.servlet-api</artifactId>
                    <version>3.0.1</version>
                </dependency>
            </dependencies>
        </plugin>
    </plugins>
</build>
<dependencies>
    <dependency>
        <groupId>com.sun.jersey</groupId>
        <artifactId>jersey-servlet</artifactId>
        <version>${jersey.version}</version>
    </dependency>
    <dependency>
        <groupId>com.sun.jersey</groupId>
        <artifactId>jersey-json</artifactId>
        <version>${jersey.version}</version>
    </dependency>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.8.2</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>com.sun.jersey</groupId>
        <artifactId>jersey-client</artifactId>
        <version>${jersey.version}</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.glassfish.extras</groupId>
        <artifactId>glassfish-embedded-web</artifactId>
        <version>${glassfish.version}</version>
        <scope>test</scope>
    </dependency>

 </dependencies>
<properties>
    <jersey.version>1.19</jersey.version>
    <glassfish.version>3.1.1</glassfish.version>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
</project>

i am newbie to java ,maven and webservices any suggestion and answer appreciated.

Your service produces XML as declared in annotation.

It was never told to return only JSON. Try setting it to one content type only - and make that one JSON instead of XML:

@GET
@Produces(MediaType.APPLICATION_JSON)
public List<Product> getProducts() {
    return productService.getProductAsList();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM