2015-12-21 98 views
0

我正在eclipse mars中通過創建maven項目來創建maven項目,如下所示:http://javapapers.com/java/java-restful-web-services-with-json-and-jersey/ 正如在這裏提到的那樣,沒有在鏈接中提到的任何依賴pom.xml鏈接我得到XML響應但沒有json response.so當我把依賴關係放在pom.xml文件中,並運行我得到500內部服務器錯誤(在這個時候我不會得到xml和json響應)在郵遞員。在rest服務沒有得到json響應java eclipse

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的任何建議,並回答讚賞。

+0

請分享你的代碼,將結果返回給你。 – Jan

回答

0

您的服務產生 XML作爲註解來聲明。

它從來沒有告訴過只返回JSON。嘗試將它設置爲一個內容僅供類型 - 並做出一個JSON而不是XML:

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

這被張貼作爲一個答案,但它並沒有試圖回答這個問題。它應該可能是編輯,評論,另一個問題,或者完全刪除。 –

+0

@Jan如果我通過http:// localhost:8080/ProductService/webresources/products檢查郵遞員?Accept = application/json&Content-Type = application/json並選擇get get this eror格式錯誤的JSON:意外'<' – allDroid

+0

@ MatiasCicero你是對的。該死的應用程序。應該已經評論。 – Jan