simplexmlelement类设置编码_「软帝学院」:2019java五大常用工具类整理

本文介绍了Java中用于JSON转换的JsonUtils工具类,包括对象转JSON字符串和JSON转对象的方法。接着讲解了CookieUtils,提供读写Cookie的实用方法,如获取和设置Cookie值。最后提到了HttpClientUtil,展示了如何使用HttpClient进行HTTP的GET和POST请求,以及JSON格式的数据传输。
摘要由CSDN通过智能技术生成

1.json转换工具

1. package com.taotao.utils;

3. import java.util.List;

5. import com.fasterxml.jackson.core.JsonProcessingException; 6. import com.fasterxml.jackson.databind.JavaType; 7. import com.fasterxml.jackson.databind.JsonNode; 8. import com.fasterxml.jackson.databind.ObjectMapper;

10. /**

11. * json转换工具类

12. */ 13. public class JsonUtils {

15. // 定义jackson对象 16. private static final ObjectMapper MAPPER = new ObjectMapper();

18. /**

19. * 将对象转换成json字符串。

20. *

Title: pojoToJson

21. *

Description:

22. * @param data

23. * @return

24. */ 25. public static String objectToJson(Object data) { 26. try { 27. String string = MAPPER.writeValueAsString(data); 28. return string; 29. } catch (JsonProcessingException e) { 30. e.printStackTrace(); 31. } 32. return null; 33. }

35. /**

36. * 将json结果集转化为对象

37. *

38. * @param jsonData json数据

39. * @param clazz 对象中的object类型

40. * @return

41. */ 42. public static T jsonToPojo(String jsonData, Class beanType) { 43. try { 44. T t = MAPPER.readValue(jsonData, beanType); 45. return t; 46. } catch (Exception e) { 47. e.printStackTrace(); 48. } 49. return null; 50. }

52. /**

53. * 将json数据转换成pojo对象list

54. *

Title: jsonToList

55. *

Description:

56. * @param jsonData

57. * @param beanType

58. * @return

59. */ 60. public static List jsonToList(String jsonData, Class beanType) { 61. JavaType javaType = MAPPER.getTypeFactory().constructParametricType(List.class, beanType); 62. try { 63. List list = MAPPER.readValue(jsonData, javaType); 64. return list; 65. } catch (Exception e) { 66. e.printStackTrace(); 67. }

69. return null; 70. }

72. }

2.cookie的读写

1. package com.taotao.common.utils;

3. import java.io.UnsupportedEncodingException; 4. import java.net.URLDecoder; 5. import java.net.URLEncoder;

7. import javax.servlet.http.Cookie; 8. import javax.servlet.http.HttpServletRequest; 9. import javax.servlet.http.HttpServletResponse;

12. /**

13. *

14. * Cookie 工具类

15. *

16. */ 17. public final class CookieUtils {

19. /**

20. * 得到Cookie的值, 不编码

21. *

22. * @param request

23. * @param cookieName

24. * @return

25. */ 26. public static String getCookieValue(HttpServletRequest request, String cookieName) { 27. return getCookieValue(request, cookieName, false); 28. }

30. /**

31. * 得到Cookie的值,

32. *

33. * @param request

34. * @param cookieName

35. * @return

36. */ 37. public static String getCookieValue(HttpServletRequest request, String cookieName, boolean isDecoder) { 38. Cookie[] cookieList = request.getCookies(); 39. if (cookieList == null || cookieName == null) { 40. return null; 41. } 42. String retValue = null; 43. try { 44. for (int i = 0; i < cookieList.length; i++) { 45. if (cookieList[i].getName().equals(cookieName)) { 46. if (isDecoder) { 47. retValue = URLDecoder.decode(cookieList[i].getValue(), "UTF-8"); 48. } else { 49. retValue = cookieList[i].getValue(); 50. } 51. break; 52. } 53. } 54. } catch (UnsupportedEncodingException e) { 55. e.printStackTrace(); 56. } 57. return retValue; 58. }

60. /**

61. * 得到Coo

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值