/src/test/java/com/alibaba/json/bvt/issue_1300/Issue1392.java

https://github.com/alibaba/fastjson · Java · 147 lines · 110 code · 37 blank · 0 comment · 0 complexity · 9917d43a7b4efab45f4f5a1d00733a85 MD5 · raw file

  1. package com.alibaba.json.bvt.issue_1300;
  2. import com.alibaba.fastjson.serializer.SerializerFeature;
  3. import com.alibaba.fastjson.support.config.FastJsonConfig;
  4. import com.alibaba.fastjson.support.jaxrs.FastJsonFeature;
  5. import org.glassfish.jersey.client.ClientConfig;
  6. import org.glassfish.jersey.server.JSONP;
  7. import org.glassfish.jersey.server.ResourceConfig;
  8. import org.glassfish.jersey.test.JerseyTest;
  9. import org.glassfish.jersey.test.TestProperties;
  10. import org.junit.Assert;
  11. import org.junit.Test;
  12. import javax.ws.rs.*;
  13. import javax.ws.rs.core.Application;
  14. import javax.ws.rs.ext.ContextResolver;
  15. import javax.ws.rs.ext.Provider;
  16. import java.util.Date;
  17. public class Issue1392 extends JerseyTest {
  18. static class Book {
  19. private int bookId;
  20. private String bookName;
  21. private String publisher;
  22. private String isbn;
  23. private Date publishTime;
  24. private Object hello;
  25. public int getBookId() {
  26. return bookId;
  27. }
  28. public void setBookId(int bookId) {
  29. this.bookId = bookId;
  30. }
  31. public String getBookName() {
  32. return bookName;
  33. }
  34. public void setBookName(String bookName) {
  35. this.bookName = bookName;
  36. }
  37. public String getPublisher() {
  38. return publisher;
  39. }
  40. public void setPublisher(String publisher) {
  41. this.publisher = publisher;
  42. }
  43. public String getIsbn() {
  44. return isbn;
  45. }
  46. public void setIsbn(String isbn) {
  47. this.isbn = isbn;
  48. }
  49. public Date getPublishTime() {
  50. return publishTime;
  51. }
  52. public void setPublishTime(Date publishTime) {
  53. this.publishTime = publishTime;
  54. }
  55. public Object getHello() {
  56. return hello;
  57. }
  58. public void setHello(Object hello) {
  59. this.hello = hello;
  60. }
  61. }
  62. @Provider
  63. static class FastJsonResolver implements ContextResolver<FastJsonConfig> {
  64. public FastJsonConfig getContext(Class<?> type) {
  65. FastJsonConfig fastJsonConfig = new FastJsonConfig();
  66. fastJsonConfig.setSerializerFeatures(
  67. SerializerFeature.WriteMapNullValue,
  68. SerializerFeature.BrowserSecure);
  69. return fastJsonConfig;
  70. }
  71. }
  72. @Path("book1392")
  73. public static class BookRestFul {
  74. @GET
  75. @Path("{id}")
  76. @Produces({"application/javascript", "application/json"})
  77. @Consumes({"application/javascript", "application/json"})
  78. @JSONP(queryParam = "callback")
  79. public Book getBookById(@PathParam("id") Long id) {
  80. Book book = new Book();
  81. book.setBookId(2);
  82. book.setBookName("Python源码剖析");
  83. book.setPublisher("电子工业出版社");
  84. book.setPublishTime(new Date());
  85. book.setIsbn("911122");
  86. return book;
  87. }
  88. }
  89. @Override
  90. protected void configureClient(ClientConfig config) {
  91. config.register(FastJsonFeature.class);
  92. }
  93. @Override
  94. protected Application configure() {
  95. enable(TestProperties.LOG_TRAFFIC);
  96. enable(TestProperties.DUMP_ENTITY);
  97. ResourceConfig config = new ResourceConfig();
  98. config.register(FastJsonResolver.class);
  99. config.register(FastJsonFeature.class);
  100. config.packages("com.alibaba.json.bvt.issue_1300");
  101. return config;
  102. }
  103. @Test
  104. public void test() {
  105. final String reponse = target("book1392").path("123").request().accept("application/javascript").get(String.class);
  106. System.out.println(reponse);
  107. Assert.assertTrue(reponse.indexOf("Python源码剖析") > 0);
  108. Assert.assertTrue(reponse.indexOf("电子工业出版社") > 0);
  109. }
  110. }