JAX-RS JSON输出中的Unicode字符显示为问号

时间:2016-03-24 10:25:13

标签: java json java-ee utf-8 jax-rs

我正在为项目创建Web服务,我想从我的Android应用程序中使用它。

我使用Java EE,EJB,JAX-RS,JPA,MySQL和Glassfish作为应用服务器。

但是我的JSON提要编码有问题,我错过了UTF-8中的一些字符。有些字符没有显示,而是我得到了吗?。

网络服务链接:http://recipesgenie.com:8080/Horoskopium/api/day

REST服务代码:

@Path(value = "/")
@ApplicationPath(value = "api")
public class RestService extends Application{

@PersistenceContext
EntityManager em;

// http://localhost:8080/Horoskopium/api/day
// http://localhost:8080/Horoskopium/api/day?lang=sr
@GET
@Path(value = "/day")
@Produces(MediaType.APPLICATION_JSON+ ";charset=utf-8")
public List<Day> getToday(@QueryParam ("lang") String lang){
    Query q = em.createQuery("SELECT d FROM Day d ORDER BY d.id DESC");
    return q.setMaxResults(12).getResultList();
}

@GET
@Path(value = "/love")
@Produces(MediaType.APPLICATION_JSON+ ";charset=utf-8")
public List<Love> getLove(@QueryParam ("lang") String lang){
    Query q = em.createQuery("SELECT l FROM Love l ORDER BY l.id DESC");
    return q.setMaxResults(12).getResultList();
}

@GET
@Path(value = "/week")
@Produces(MediaType.APPLICATION_JSON+ ";charset=utf-8")
public List<Week> getWeek(@QueryParam ("lang") String lang){
    Query q = em.createQuery("SELECT w FROM Week w ORDER BY w.id DESC");
    return q.setMaxResults(12).getResultList();
}

@GET
@Path(value = "/month")
@Produces(MediaType.APPLICATION_JSON+ ";charset=utf-8")
public List<Month> getMonth(@QueryParam ("lang") String lang){
    Query q = em.createQuery("SELECT m FROM Month m ORDER BY m.id DESC");
    return q.setMaxResults(12).getResultList();
}            

}

我还 TimerService 连接到另一台服务器并读取 JSON 并将其保存到我的 MySQL 数据库。

来自我正在阅读并使用Gson解析到我的数据库中的另一台服务器的此JSON响应:http://aplikacije-za-android.com/apps/horoskopium/examples/nedeljnihoroskop.php

@Singleton
public class TimerService {

@EJB
HoroscopeEJB horoscopeEJB;    
HoroscopeParser horoscopeParser;

@Schedule(dayOfWeek = "*", hour="0-6", persistent = false)
public void downloadTodayHoroscope() throws Exception {
    System.out.println("upisujem today ");
    saveTodayHoroscope();
}

@Schedule(dayOfWeek = "*", hour="0-6", persistent = false)
public void downloadLoveHoroscope() throws Exception {
    System.out.println("upisujem love ");
    saveLoveHoroscope();
}

@Schedule(dayOfWeek = "Mon", hour="0/2", persistent = false)
public void downloadWeekHoroscope() throws Exception {
    System.out.println("upisujem week ");
    saveWeekHoroscope();
}

@Schedule(dayOfMonth = "1", hour = "4/3", persistent = false)
public void downloadMonthHoroscope() throws Exception {
    System.out.println("upisujem month ");
    saveMonthHoroscope();
}

public void saveTodayHoroscope() throws Exception {

    HoroscopeFeed horoscope = getTodayHoroscope();

    if (horoscope != null && horoscope.getHoroscope().size() > 0) {
        for (int i = 0; i < horoscope.getHoroscope().size(); i++) {
            Day d = new Day();
            d.setText(horoscope.getHoroscope().get(i).getTxtHrs());
            horoscopeEJB.addTodayHoroscope(d);
        }
    }
}

public void saveLoveHoroscope() throws Exception {

    HoroscopeFeed horoscope = getLoveHoroscope();
    if (horoscope != null && horoscope.getHoroscope().size() > 0) {
        for (int i = 0; i < horoscope.getHoroscope().size(); i++) {
            Love l = new Love();
            l.setText(horoscope.getHoroscope().get(i).getTxtHrs());
            horoscopeEJB.addLoveHoroscope(l);
        }
    }
}

public void saveWeekHoroscope() throws Exception {

    HoroscopeFeed horoscope = getWeekHoroscope();
    if (horoscope != null && horoscope.getHoroscope().size() > 0) {
        for (int i = 0; i < horoscope.getHoroscope().size(); i++) {
            Week w = new Week();
            w.setText(horoscope.getHoroscope().get(i).getTxtHrs());
            horoscopeEJB.addWeekHoroscope(w);
        }
    }
}

public void saveMonthHoroscope() throws Exception {

    HoroscopeFeed horoscope = getMonthHoroscope();

    if (horoscope != null && horoscope.getHoroscope().size() > 0) {
        for (int i = 0; i < horoscope.getHoroscope().size(); i++) {
            Month m = new Month();
            m.setText(horoscope.getHoroscope().get(i).getTxtHrs());
            horoscopeEJB.addMonthHoroscope(m);
        }
    }
}

public HoroscopeFeed getTodayHoroscope() throws Exception {
    horoscopeParser = new HoroscopeParser();
    return horoscopeParser.getTodayHoroscope("http://aplikacije-za-android.com/apps/horoskopium/examples/dnevnihoroskop.php");
}

public HoroscopeFeed getWeekHoroscope() throws Exception {
    horoscopeParser = new HoroscopeParser();
    return horoscopeParser.getWeekHoroscope("http://aplikacije-za-android.com/apps/horoskopium/examples/nedeljnihoroskop.php");
}

public HoroscopeFeed getLoveHoroscope() throws Exception {
    horoscopeParser = new HoroscopeParser();
    return horoscopeParser.getLoveHoroscope("http://aplikacije-za-android.com/apps/horoskopium/examples/dnevniljubavnihoroskop.php");
}

public HoroscopeFeed getMonthHoroscope() throws Exception {
    horoscopeParser = new HoroscopeParser();
    return horoscopeParser.getMonthHoroscope("http://aplikacije-za-android.com/apps/horoskopium/examples/mesecnihoroskop.php");
}

}

EJB 代码:

@Stateless
@LocalBean
public class HoroscopeEJB {

@PersistenceContext
EntityManager em;

public void addTodayHoroscope(Day day){

    boolean ifExist = checkDaily(day.getText());
    if(!ifExist){
        em.merge(day); 
    } else{
        System.out.println("Imam vec dnevni u bazi");
    }

}

public void addLoveHoroscope(Love love){
     boolean ifExist = checkLove(love.getText());
    if(!ifExist){
        em.merge(love); 
    }
}

 public void addWeekHoroscope(Week week){
     week.getText();
     boolean ifExist = checkWeek(week.getText());
    if(!ifExist){
        em.merge(week); 
    }
}

public void addMonthHoroscope(Month month){
    boolean ifExist = checkMonth(month.getText());
    if(!ifExist){
        em.merge(month); 
    }
}

private boolean checkDaily(String text){
    List<Day> results = em.createQuery("SELECT d FROM Day d WHERE d.text = :text")
                    .setParameter("text", text).getResultList();

    if(results.size() > 0){
        return true;
    } else{
        return false;
    }
}

private boolean checkLove(String text){
    List<Love> results = em.createQuery("SELECT l FROM Love l WHERE l.text = :text")
                    .setParameter("text", text).getResultList();

    if(results.size() > 0){
        return true;
    } else{
        return false;
    }
}

private boolean checkWeek(String text){
    List<Week> results = em.createQuery("SELECT w FROM Week w WHERE w.text = :text")
                    .setParameter("text", text).getResultList();

    if(results.size() > 0){
        return true;
    } else{
        return false;
    }
}

 private boolean checkMonth(String text){
    List<Month> results = em.createQuery("SELECT m FROM Month m WHERE m.text = :text")
                    .setParameter("text", text).getResultList();

    if(results.size() > 0){
        return true;
    } else{
        return false;
    }
}

}

0 个答案:

没有答案