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.0modelVersion>
<groupId>cn.yang.jersyTestgroupId>
<artifactId>jersyTestartifactId>
<packaging>warpackaging>
<version>0.0.1-SNAPSHOTversion>
<name>jersyTest Maven Webappname>
<url>http://maven.apache.orgurl>
<repository>
<id>snapshot-repository.java.netid>
<name>Java.net Snapshot Repository for Mavenname>
<url>https://maven.java.net/content/repositories/snapshots/url>
<layout>defaultlayout>
repository>
<dependencies>
<dependency>
<groupId>junitgroupId>
<artifactId>junitartifactId>
<version>3.8.1version>
<scope>testscope>
dependency>
<dependency>
<groupId>org.glassfish.jersey.containersgroupId>
<artifactId>jersey-container-grizzly2-httpartifactId>
<version>2.22.1version>
dependency>
<dependency>
<groupId>org.glassfish.jersey.containersgroupId>
<artifactId>jersey-container-grizzly2-servletartifactId>
<version>2.22.1version>
dependency>
<dependency>
<groupId>org.glassfish.jersey.containersgroupId>
<artifactId>jersey-container-jdk-httpartifactId>
<version>2.22.1version>
dependency>
<dependency>
<groupId>org.glassfish.jersey.containersgroupId>
<artifactId>jersey-container-simple-httpartifactId>
<version>2.22.1version>
dependency>
<dependency>
<groupId>org.glassfish.jersey.containersgroupId>
<artifactId>jersey-container-jetty-httpartifactId>
<version>2.22.1version>
dependency>
<dependency>
<groupId>org.glassfish.jersey.containersgroupId>
<artifactId>jersey-container-jetty-servletartifactId>
<version>2.22.1version>
dependency>
<dependency>
<groupId>org.glassfish.jersey.mediagroupId>
<artifactId>jersey-media-moxyartifactId>
<version>2.22.1version>
dependency>
dependencies>
<build>
<finalName>jersyTestfinalName>
build>
project>
web.xml文件
<web-app>
<display-name>Archetype Created Web Applicationdisplay-name>
<filter>
<filter-name>Set Character Encodingfilter-name>
<filter-class>cn.yang.test.CharactorFilterfilter-class>
<init-param>
<param-name>encodingparam-name>
<param-value>UTF-8param-value>
init-param>
filter>
<filter-mapping>
<filter-name>Set Character Encodingfilter-name>
<url-pattern>/*url-pattern>
filter-mapping>
<servlet>
<servlet-name>jersy-restfulservlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainerservlet-class>
<init-param>
<param-name>jersey.config.server.provider.packagesparam-name>
<param-value>cn.yang.testparam-value>
init-param>
<load-on-startup>1load-on-startup>
servlet>
<servlet-mapping>
<servlet-name>jersy-restfulservlet-name>
<url-pattern>/jersy/*url-pattern>
servlet-mapping>
web-app>
实体类
package cn.yang.test;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement
public class Book {
private String name;
private String price;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPrice() {
return price;
}
public void setPrice(String price) {
this.price = price;
}
}
中文乱码
package cn.yang.test;
import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
public class CharactorFilter implements Filter { //继承Filter类
//字符编码
String encoding=null;
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
if(encoding!=null){
//设置request字符编码
request.setCharacterEncoding(encoding);
//设置response字符编码
response.setContentType("text/html;charset="+encoding);
}
//传递给下一个过滤器
chain.doFilter(request, response);
}
public void init(FilterConfig filterConfig) throws ServletException {
//获取初始化参数
encoding=filterConfig.getInitParameter("encoding");
}
public void destroy() {
// TODO Auto-generated method stub
encoding=null;
}
}
package cn.yang.test;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
@Path("/index")
public class JersyDemo {
@GET
@Path("/hello1")
@Produces("text/plain")
public String getHello() {
return "Hello World!";
}
@GET
@Path("/hello2")
@Produces("text/plain")
public String getHello2() {
return "Hello World2!";
}
@GET
@Path("/hello3")
@Produces(MediaType.APPLICATION_JSON)
public Book getHello3() {
Book book = new Book();
book.setName("爵迹");
book.setPrice("35.65$");
return book;
}
}
运行结果图
实体类里面包含实体类的写法
package cn.yang.test;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement
public class Book {
private String name;
private String price;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPrice() {
return price;
}
public void setPrice(String price) {
this.price = price;
}
private Details details;
public void setDetails(Details details) {
this.details = details;
}
public Details getDetails() {
return details;
}
}
package cn.yang.test;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement
public class Details {
private String description;
private String num;
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getNum() {
return num;
}
public void setNum(String num) {
this.num = num;
}
public Details(String description, String num) {
super();
this.description = description;
this.num = num;
}
注意如果提供了有参的构造器 那么无参数的构造器一定要提供 否则会报这种错误
MessageBodyWriter not found for media type=application/json。。。。。
原因:没有无参构造器会导致对这个类执行反射操作时会失败(类比所有的框架基本上都会用到反射,所以写实体类的时候一定要注意无参构造器一定要有)
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement
public class Details {
private String description;
private String num;
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getNum() {
return num;
}
public void setNum(String num) {
this.num = num;
}
public Details(String description, String num) {
super();
this.description = description;
this.num = num;
}
public Details() {
}
}
public Details() {
}
}
下面只提供部分代码
@GET
@Path("/hello3")
@Produces(MediaType.APPLICATION_JSON)
public Book getHello3() {
Book book = new Book();
book.setName("哈利波特");
book.setPrice("35.65$");
Details details=new Details("这是崭新的一本书!!!", "156本");
book.setDetails(details);
return book;
}