在休息服务java eclipse中没有得到json响应

时间:2015-12-21 11:56:38

标签: java eclipse rest maven

我通过以下链接创建maven项目,在eclipse mars中建立其余的webservices http://javapapers.com/java/java-restful-web-services-with-json-and-jersey/ 如上所述,没有在链接中提到的pom.xml中放置任何依赖关系我得到xml响应但没有json response.so当我将依赖项放在pom.xml文件中并运行时我得到500内部服务器错误(此时我不会得到邮递员中的xml和json响应。

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>

我是java,maven和webservices的新手任何建议和回答赞赏。

1 个答案:

答案 0 :(得分:0)

您的服务生成 XML,如注释中所声明的那样。

从未被告知只返回JSON。尝试将其设置为仅一个内容类型 - 并使其成为一个JSON而不是XML:

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