5个Lang:1-LangServe基本操作

1、安装环境

!pip install langchain openai langserve fastapi uvicorn sse_starlette
Collecting langchain
  Downloading langchain-0.1.20-py3-none-any.whl (1.0 MB)
[2K     [90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━[0m [32m1.0/1.0 MB[0m [31m3.7 MB/s[0m eta [36m0:00:00[0m
[?25hCollecting openai
  Downloading openai-1.28.1-py3-none-any.whl (320 kB)
[2K     [90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━[0m [32m320.1/320.1 kB[0m [31m8.6 MB/s[0m eta [36m0:00:00[0m
[?25hCollecting langserve
  Downloading langserve-0.1.1-py3-none-any.whl (1.2 MB)
[2K     [90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━[0m [32m1.2/1.2 MB[0m [31m21.0 MB/s[0m eta [36m0:00:00[0m
[?25hCollecting fastapi
  Downloading fastapi-0.111.0-py3-none-any.whl (91 kB)
[2K     [90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━[0m [32m92.0/92.0 kB[0m [31m6.2 MB/s[0m eta [36m0:00:00[0m
[?25hCollecting uvicorn
  Downloading uvicorn-0.29.0-py3-none-any.whl (60 kB)
[2K     [90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━[0m [32m60.8/60.8 kB[0m [31m4.3 MB/s[0m eta [36m0:00:00[0m
[?25hCollecting sse_starlette
  Downloading sse_starlette-2.1.0-py3-none-any.whl (9.2 kB)
Requirement already satisfied: PyYAML>=5.3 in /usr/local/lib/python3.10/dist-packages (from langchain) (6.0.1)
Requirement already satisfied: SQLAlchemy<3,>=1.4 in /usr/local/lib/python3.10/dist-packages (from langchain) (2.0.30)
Requirement already satisfied: aiohttp<4.0.0,>=3.8.3 in /usr/local/lib/python3.10/dist-packages (from langchain) (3.9.5)
Requirement already satisfied: async-timeout<5.0.0,>=4.0.0 in /usr/local/lib/python3.10/dist-packages (from langchain) (4.0.3)
Collecting dataclasses-json<0.7,>=0.5.7 (from langchain)
  Downloading dataclasses_json-0.6.6-py3-none-any.whl (28 kB)
Collecting langchain-community<0.1,>=0.0.38 (from langchain)
  Downloading langchain_community-0.0.38-py3-none-any.whl (2.0 MB)
[2K     [90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━[0m [32m2.0/2.0 MB[0m [31m26.2 MB/s[0m eta [36m0:00:00[0m
[?25hCollecting langchain-core<0.2.0,>=0.1.52 (from langchain)
  Downloading langchain_core-0.1.52-py3-none-any.whl (302 kB)
[2K     [90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━[0m [32m302.9/302.9 kB[0m [31m13.9 MB/s[0m eta [36m0:00:00[0m
[?25hCollecting langchain-text-splitters<0.1,>=0.0.1 (from langchain)
  Downloading langchain_text_splitters-0.0.1-py3-none-any.whl (21 kB)
Collecting langsmith<0.2.0,>=0.1.17 (from langchain)
  Downloading langsmith-0.1.56-py3-none-any.whl (120 kB)
[2K     [90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━[0m [32m120.8/120.8 kB[0m [31m7.8 MB/s[0m eta [36m0:00:00[0m
[?25hRequirement already satisfied: numpy<2,>=1 in /usr/local/lib/python3.10/dist-packages (from langchain) (1.25.2)
Requirement already satisfied: pydantic<3,>=1 in /usr/local/lib/python3.10/dist-packages (from langchain) (2.7.1)
Requirement already satisfied: requests<3,>=2 in /usr/local/lib/python3.10/dist-packages (from langchain) (2.31.0)
Requirement already satisfied: tenacity<9.0.0,>=8.1.0 in /usr/local/lib/python3.10/dist-packages (from langchain) (8.3.0)
Requirement already satisfied: anyio<5,>=3.5.0 in /usr/local/lib/python3.10/dist-packages (from openai) (3.7.1)
Requirement already satisfied: distro<2,>=1.7.0 in /usr/lib/python3/dist-packages (from openai) (1.7.0)
Collecting httpx<1,>=0.23.0 (from openai)
  Downloading httpx-0.27.0-py3-none-any.whl (75 kB)
[2K     [90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━[0m [32m75.6/75.6 kB[0m [31m4.7 MB/s[0m eta [36m0:00:00[0m
[?25hRequirement already satisfied: sniffio in /usr/local/lib/python3.10/dist-packages (from openai) (1.3.1)
Requirement already satisfied: tqdm>4 in /usr/local/lib/python3.10/dist-packages (from openai) (4.66.4)
Requirement already satisfied: typing-extensions<5,>=4.7 in /usr/local/lib/python3.10/dist-packages (from openai) (4.11.0)
Collecting orjson>=2 (from langserve)
  Downloading orjson-3.10.3-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (142 kB)
[2K     [90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━[0m [32m142.5/142.5 kB[0m [31m2.6 MB/s[0m eta [36m0:00:00[0m
[?25hCollecting starlette<0.38.0,>=0.37.2 (from fastapi)
  Downloading starlette-0.37.2-py3-none-any.whl (71 kB)
[2K     [90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━[0m [32m71.9/71.9 kB[0m [31m4.2 MB/s[0m eta [36m0:00:00[0m
[?25hCollecting fastapi-cli>=0.0.2 (from fastapi)
  Downloading fastapi_cli-0.0.3-py3-none-any.whl (9.2 kB)
Requirement already satisfied: jinja2>=2.11.2 in /usr/local/lib/python3.10/dist-packages (from fastapi) (3.1.4)
Collecting python-multipart>=0.0.7 (from fastapi)
  Downloading python_multipart-0.0.9-py3-none-any.whl (22 kB)
Collecting ujson!=4.0.2,!=4.1.0,!=4.2.0,!=4.3.0,!=5.0.0,!=5.1.0,>=4.0.1 (from fastapi)
  Downloading ujson-5.9.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (53 kB)
[2K     [90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━[0m [32m53.2/53.2 kB[0m [31m1.9 MB/s[0m eta [36m0:00:00[0m
[?25hCollecting email_validator>=2.0.0 (from fastapi)
  Downloading email_validator-2.1.1-py3-none-any.whl (30 kB)
Requirement already satisfied: click>=7.0 in /usr/local/lib/python3.10/dist-packages (from uvicorn) (8.1.7)
Collecting h11>=0.8 (from uvicorn)
  Downloading h11-0.14.0-py3-none-any.whl (58 kB)
[2K     [90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━[0m [32m58.3/58.3 kB[0m [31m3.4 MB/s[0m eta [36m0:00:00[0m
[?25hRequirement already satisfied: aiosignal>=1.1.2 in /usr/local/lib/python3.10/dist-packages (from aiohttp<4.0.0,>=3.8.3->langchain) (1.3.1)
Requirement already satisfied: attrs>=17.3.0 in /usr/local/lib/python3.10/dist-packages (from aiohttp<4.0.0,>=3.8.3->langchain) (23.2.0)
Requirement already satisfied: frozenlist>=1.1.1 in /usr/local/lib/python3.10/dist-packages (from aiohttp<4.0.0,>=3.8.3->langchain) (1.4.1)
Requirement already satisfied: multidict<7.0,>=4.5 in /usr/local/lib/python3.10/dist-packages (from aiohttp<4.0.0,>=3.8.3->langchain) (6.0.5)
Requirement already satisfied: yarl<2.0,>=1.0 in /usr/local/lib/python3.10/dist-packages (from aiohttp<4.0.0,>=3.8.3->langchain) (1.9.4)
Requirement already satisfied: idna>=2.8 in /usr/local/lib/python3.10/dist-packages (from anyio<5,>=3.5.0->openai) (3.7)
Requirement already satisfied: exceptiongroup in /usr/local/lib/python3.10/dist-packages (from anyio<5,>=3.5.0->openai) (1.2.1)
Collecting marshmallow<4.0.0,>=3.18.0 (from dataclasses-json<0.7,>=0.5.7->langchain)
  Downloading marshmallow-3.21.2-py3-none-any.whl (49 kB)
[2K     [90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━[0m [32m49.3/49.3 kB[0m [31m1.6 MB/s[0m eta [36m0:00:00[0m
[?25hCollecting typing-inspect<1,>=0.4.0 (from dataclasses-json<0.7,>=0.5.7->langchain)
  Downloading typing_inspect-0.9.0-py3-none-any.whl (8.8 kB)
Collecting dnspython>=2.0.0 (from email_validator>=2.0.0->fastapi)
  Downloading dnspython-2.6.1-py3-none-any.whl (307 kB)
[2K     [90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━[0m [32m307.7/307.7 kB[0m [31m12.1 MB/s[0m eta [36m0:00:00[0m
[?25hCollecting typer>=0.12.3 (from fastapi-cli>=0.0.2->fastapi)
  Downloading typer-0.12.3-py3-none-any.whl (47 kB)
[2K     [90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━[0m [32m47.2/47.2 kB[0m [31m2.5 MB/s[0m eta [36m0:00:00[0m
[?25hRequirement already satisfied: certifi in /usr/local/lib/python3.10/dist-packages (from httpx<1,>=0.23.0->openai) (2024.2.2)
Collecting httpcore==1.* (from httpx<1,>=0.23.0->openai)
  Downloading httpcore-1.0.5-py3-none-any.whl (77 kB)
[2K     [90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━[0m [32m77.9/77.9 kB[0m [31m3.2 MB/s[0m eta [36m0:00:00[0m
[?25hRequirement already satisfied: MarkupSafe>=2.0 in /usr/local/lib/python3.10/dist-packages (from jinja2>=2.11.2->fastapi) (2.1.5)
Collecting jsonpatch<2.0,>=1.33 (from langchain-core<0.2.0,>=0.1.52->langchain)
  Downloading jsonpatch-1.33-py2.py3-none-any.whl (12 kB)
Collecting packaging<24.0,>=23.2 (from langchain-core<0.2.0,>=0.1.52->langchain)
  Downloading packaging-23.2-py3-none-any.whl (53 kB)
[2K     [90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━[0m [32m53.0/53.0 kB[0m [31m1.8 MB/s[0m eta [36m0:00:00[0m
[?25hRequirement already satisfied: annotated-types>=0.4.0 in /usr/local/lib/python3.10/dist-packages (from pydantic<3,>=1->langchain) (0.6.0)
Requirement already satisfied: pydantic-core==2.18.2 in /usr/local/lib/python3.10/dist-packages (from pydantic<3,>=1->langchain) (2.18.2)
Requirement already satisfied: charset-normalizer<4,>=2 in /usr/local/lib/python3.10/dist-packages (from requests<3,>=2->langchain) (3.3.2)
Requirement already satisfied: urllib3<3,>=1.21.1 in /usr/local/lib/python3.10/dist-packages (from requests<3,>=2->langchain) (2.0.7)
Requirement already satisfied: greenlet!=0.4.17 in /usr/local/lib/python3.10/dist-packages (from SQLAlchemy<3,>=1.4->langchain) (3.0.3)
Collecting httptools>=0.5.0 (from uvicorn)
  Downloading httptools-0.6.1-cp310-cp310-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl (341 kB)
[2K     [90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━[0m [32m341.4/341.4 kB[0m [31m22.1 MB/s[0m eta [36m0:00:00[0m
[?25hCollecting python-dotenv>=0.13 (from uvicorn)
  Downloading python_dotenv-1.0.1-py3-none-any.whl (19 kB)
Collecting uvloop!=0.15.0,!=0.15.1,>=0.14.0 (from uvicorn)
  Downloading uvloop-0.19.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (3.4 MB)
[2K     [90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━[0m [32m3.4/3.4 MB[0m [31m42.8 MB/s[0m eta [36m0:00:00[0m
[?25hCollecting watchfiles>=0.13 (from uvicorn)
  Downloading watchfiles-0.21.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (1.3 MB)
[2K     [90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━[0m [32m1.3/1.3 MB[0m [31m26.5 MB/s[0m eta [36m0:00:00[0m
[?25hCollecting websockets>=10.4 (from uvicorn)
  Downloading websockets-12.0-cp310-cp310-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl (130 kB)
[2K     [90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━[0m [32m130.2/130.2 kB[0m [31m8.0 MB/s[0m eta [36m0:00:00[0m
[?25hCollecting jsonpointer>=1.9 (from jsonpatch<2.0,>=1.33->langchain-core<0.2.0,>=0.1.52->langchain)
  Downloading jsonpointer-2.4-py2.py3-none-any.whl (7.8 kB)
Collecting shellingham>=1.3.0 (from typer>=0.12.3->fastapi-cli>=0.0.2->fastapi)
  Downloading shellingham-1.5.4-py2.py3-none-any.whl (9.8 kB)
Requirement already satisfied: rich>=10.11.0 in /usr/local/lib/python3.10/dist-packages (from typer>=0.12.3->fastapi-cli>=0.0.2->fastapi) (13.7.1)
Collecting mypy-extensions>=0.3.0 (from typing-inspect<1,>=0.4.0->dataclasses-json<0.7,>=0.5.7->langchain)
  Downloading mypy_extensions-1.0.0-py3-none-any.whl (4.7 kB)
Requirement already satisfied: markdown-it-py>=2.2.0 in /usr/local/lib/python3.10/dist-packages (from rich>=10.11.0->typer>=0.12.3->fastapi-cli>=0.0.2->fastapi) (3.0.0)
Requirement already satisfied: pygments<3.0.0,>=2.13.0 in /usr/local/lib/python3.10/dist-packages (from rich>=10.11.0->typer>=0.12.3->fastapi-cli>=0.0.2->fastapi) (2.16.1)
Requirement already satisfied: mdurl~=0.1 in /usr/local/lib/python3.10/dist-packages (from markdown-it-py>=2.2.0->rich>=10.11.0->typer>=0.12.3->fastapi-cli>=0.0.2->fastapi) (0.1.2)
Installing collected packages: websockets, uvloop, ujson, shellingham, python-multipart, python-dotenv, packaging, orjson, mypy-extensions, jsonpointer, httptools, h11, dnspython, watchfiles, uvicorn, typing-inspect, starlette, marshmallow, jsonpatch, httpcore, email_validator, typer, sse_starlette, langsmith, httpx, dataclasses-json, openai, langchain-core, langserve, langchain-text-splitters, langchain-community, langchain, fastapi-cli, fastapi
  Attempting uninstall: packaging
    Found existing installation: packaging 24.0
    Uninstalling packaging-24.0:
      Successfully uninstalled packaging-24.0
  Attempting uninstall: typer
    Found existing installation: typer 0.9.4
    Uninstalling typer-0.9.4:
      Successfully uninstalled typer-0.9.4
[31mERROR: pip's dependency resolver does not currently take into account all the packages that are installed. This behaviour is the source of the following dependency conflicts.
spacy 3.7.4 requires typer<0.10.0,>=0.3.0, but you have typer 0.12.3 which is incompatible.
weasel 0.3.4 requires typer<0.10.0,>=0.3.0, but you have typer 0.12.3 which is incompatible.[0m[31m
[0mSuccessfully installed dataclasses-json-0.6.6 dnspython-2.6.1 email_validator-2.1.1 fastapi-0.111.0 fastapi-cli-0.0.3 h11-0.14.0 httpcore-1.0.5 httptools-0.6.1 httpx-0.27.0 jsonpatch-1.33 jsonpointer-2.4 langchain-0.1.20 langchain-community-0.0.38 langchain-core-0.1.52 langchain-text-splitters-0.0.1 langserve-0.1.1 langsmith-0.1.56 marshmallow-3.21.2 mypy-extensions-1.0.0 openai-1.28.1 orjson-3.10.3 packaging-23.2 python-dotenv-1.0.1 python-multipart-0.0.9 shellingham-1.5.4 sse_starlette-2.1.0 starlette-0.37.2 typer-0.12.3 typing-inspect-0.9.0 ujson-5.9.0 uvicorn-0.29.0 uvloop-0.19.0 watchfiles-0.21.0 websockets-12.0

2、设置项目路径,设置环境变量

# 导入 Google Colab 的 drive 模块,用来挂载 Google 云端硬盘
from google.colab import drive

# 把你的 Google Drive 挂载到 Colab 的 /content/drive 目录
drive.mount('/content/drive')

# 导入 os 模块,用来进行路径切换等文件系统操作
import os

# 定义你的项目路径(这里是你在 Google Drive 里的 LangServe 目录)
path = "/content/drive/My Drive/Colab/langchain/LangServe/"

# 将当前工作目录切换到上述路径,这样后续读写文件都以这个目录为根
os.chdir(path)
Mounted at /content/drive
# 设置OpenAI KEY环境变量
import os
import getpass
os.environ['OPENAI_API_KEY'] = getpass.getpass('OpenAI API Key:')
OpenAI API Key:··········

3、定义服务文件

from typing import List

from fastapi import FastAPI
from langchain.prompts import ChatPromptTemplate
from langchain.chat_models import ChatOpenAI
from langchain.schema import BaseOutputParser
from langserve import add_routes

# 定义链
class CommaSeparatedListOutputParser(BaseOutputParser[List[str]]):
    "将LLM的输出内容解析为列表"
    def parse(self, text:str):
        "解析LLM调用的输出"
        return text.strip().split(",")


template = """你是一个能生成以逗号分隔的列表的助手,用户会传入一个类别,你应该生成该类别下的5个对象,并以逗号分隔的形式返回。
只返回以逗号分隔的内容,不要包含其他内容。"""
human_template = "{text}"

chat_prompt = ChatPromptTemplate.from_messages([("system", template),("human", human_template),])
first_chain = chat_prompt | ChatOpenAI() | CommaSeparatedListOutputParser()

# 应用定义
app = FastAPI(title = "第一个LangChain应用", version = "0.0.1", description = "LangChain应用接口",)

# 添加链路由
add_routes(app, first_chain, path="/first_app")
if __name__ == "__main__":
    import uvicorn
    uvicorn.run(app, host="localhost", port=8000)

4、启动服务

!python serve.py
/usr/local/lib/python3.10/dist-packages/langchain/chat_models/__init__.py:31: LangChainDeprecationWarning: Importing chat models from langchain is deprecated. Importing from langchain will no longer be supported as of langchain==0.2.0. Please import from langchain-community instead:

`from langchain_community.chat_models import ChatOpenAI`.

To install langchain-community run `pip install -U langchain-community`.
  warnings.warn(
/usr/local/lib/python3.10/dist-packages/langchain_core/_api/deprecation.py:119: LangChainDeprecationWarning: The class `ChatOpenAI` was deprecated in LangChain 0.0.10 and will be removed in 0.3.0. An updated version of the class exists in the langchain-openai package and should be used instead. To use it run `pip install -U langchain-openai` and import as `from langchain_openai import ChatOpenAI`.
  warn_deprecated(
[32mINFO[0m:     Started server process [[36m1897[0m]
[32mINFO[0m:     Waiting for application startup.

 __          ___      .__   __.   _______      _______. _______ .______     ____    ____  _______
|  |        /   \     |  \ |  |  /  _____|    /       ||   ____||   _  \    \   \  /   / |   ____|
|  |       /  ^  \    |   \|  | |  |  __     |   (----`|  |__   |  |_)  |    \   \/   /  |  |__
|  |      /  /_\  \   |  . `  | |  | |_ |     \   \    |   __|  |      /      \      /   |   __|
|  `----./  _____  \  |  |\   | |  |__| | .----)   |   |  |____ |  |\  \----.  \    /    |  |____
|_______/__/     \__\ |__| \__|  \______| |_______/    |_______|| _| `._____|   \__/     |_______|

[1;32;40mLANGSERVE:[0m Playground for chain "/first_app/" is live at:
[1;32;40mLANGSERVE:[0m  │
[1;32;40mLANGSERVE:[0m  └──> /first_app/playground/
[1;32;40mLANGSERVE:[0m
[1;32;40mLANGSERVE:[0m See all available routes at /docs/

[1;31;40mLANGSERVE:[0m ⚠️ Using pydantic 2.7.1. OpenAPI docs for invoke, batch, stream, stream_log endpoints will not be generated. API endpoints and playground should work as expected. If you need to see the docs, you can downgrade to pydantic 1. For example, `pip install pydantic==1.10.13`. See https://github.com/tiangolo/fastapi/issues/10360 for details.

[32mINFO[0m:     Application startup complete.
[31mERROR[0m:    [Errno 99] error while attempting to bind on address ('::1', 8001, 0, 0): cannot assign requested address
[32mINFO[0m:     Waiting for application shutdown.
[32mINFO[0m:     Application shutdown complete.

<think> 我们正在讨论的是commons-lang:commons-lang这个jar包的作用。根据Apache Commons Lang项目的官方描述,这个库提供了一系列针对Java标准库的实用工具类和方法,主要目的是填补Java标准库(java.lang)的不足,提供更多的工具和功能,使得开发者能够更高效地进行开发。 主要作用包括: 1. **字符串处理**:提供了StringUtils类,包含大量字符串操作方法,如空值安全的判断、截取、反转、拆分、连接等。 2. **数值处理**:NumberUtils类提供数字转换、判断等。 3. **对象操作**:ObjectUtils类提供对象的null安全操作,如默认值处理、比较等。 4. **日期时间处理**:DateUtils和DateFormatUtils类提供日期格式化和计算(如日期加减、比较等)。 5. **数组操作**:ArrayUtils类提供数组的合并、子数组、反转、查找等。 6. **系统属性**:SystemUtils类用于获取系统属性(如操作系统类型、Java版本等)。 7. **异常处理**:ExceptionUtils类提供异常堆栈信息的处理。 8. **随机数生成**:RandomUtils和RandomStringUtils类提供更便捷的随机数生成。 9. **构建器模式**:ToStringBuilder、EqualsBuilder、HashCodeBuilder等帮助生成toString、equals和hashCode方法。 此外,它还包含一些其他工具类,如SerializationUtils(序列化工具)、EnumUtils(枚举工具)等。 这个库在Java开发中被广泛使用,特别是在需要大量基础工具类支持的场景下,可以避免重复造轮子。 下面我们以代码示例来展示一些常用功能: 示例1:使用StringUtils处理字符串 ```java import org.apache.commons.lang.StringUtils; public class StringExample { public static void main(String[] args) { // 判断字符串是否为空(null或空字符串) String str = null; System.out.println(StringUtils.isEmpty(str)); // 输出: true // 判断字符串是否为空白(null、空字符串或仅包含空白字符) System.out.println(StringUtils.isBlank(" ")); // 输出: true // 反转字符串 System.out.println(StringUtils.reverse("hello")); // 输出: olleh // 截取字符串 System.out.println(StringUtils.substring("hello world", 6)); // 输出: world } } ``` 示例2:使用DateUtils进行日期操作 ```java import org.apache.commons.lang.time.DateUtils; import java.util.Calendar; import java.util.Date; public class DateExample { public static void main(String[] args) { Date now = new Date(); // 加一天 Date tomorrow = DateUtils.addDays(now, 1); // 减一个月 Date lastMonth = DateUtils.addMonths(now, -1); // 设置时间为当天的开始(00:00:00) Date startOfDay = DateUtils.truncate(now, Calendar.DATE); } } ``` 示例3:使用ArrayUtils操作数组 ```java import org.apache.commons.lang.ArrayUtils; public class ArrayExample { public static void main(String[] args) { int[] array = {1, 2, 3}; // 添加元素到数组 int[] newArray = ArrayUtils.add(array, 4); // [1,2,3,4] // 反转数组 ArrayUtils.reverse(newArray); // [4,3,2,1] // 查找元素 int index = ArrayUtils.indexOf(newArray, 3); // 1 } } ``` 需要注意的是,Apache Commons Lang有两个主要版本:2.x和3.x。其中,2.x的包名为`org.apache.commons.lang`,而3.x的包名为`org.apache.commons.lang3`。我们这里讨论的是2.x版本(commons-lang),而3.x版本(commons-lang3)是向后不兼容的升级,增加了一些新特性并删除了一些过时的方法。 因此,在选择版本时,需要根据项目需求来决定。对于新项目,推荐使用commons-lang3。 综上所述,commons-lang:commons-lang jar包的作用是提供Java标准库的扩展工具类,帮助开发者更高效地完成常见任务。 相关问题: 1. commons-lang和commons-lang3有什么区别? 2. 如何在Maven项目中引入commons-lang? 3. StringUtils类有哪些常用的字符串操作方法? 4. 如何用ToStringBuilder生成对象的toString方法? </think> ### `commons-lang:commons-lang` JAR 包的核心作用 `commons-lang` 是 **Apache Commons Lang** 库的核心组件,提供对 Java 标准库(`java.lang`)的扩展工具类,主要作用包括: #### 1. **增强基础数据类型操作** - **字符串处理**:空值安全操作(如 `StringUtils.isBlank()`)、截取/反转/拆分等[^1] ```java // 示例:安全判断空字符串 StringUtils.isBlank(null); // true StringUtils.isBlank(" "); // true ``` - **数值处理**:类型安全转换(`NumberUtils.toInt()`)、范围检查等 - **数组工具**:高效合并/查找/反转(`ArrayUtils.addAll()`) #### 2. **对象操作增强** - **空值安全处理**:`ObjectUtils.defaultIfNull()` 避免 NPE - **对象比较**:深度比较 `EqualsBuilder.reflectionEquals()` - **哈希码生成**:`HashCodeBuilder` 自动生成合规的 `hashCode()` ```java // 自动生成 hashCode public int hashCode() { return new HashCodeBuilder(17, 37) .append(name) .append(age) .toHashCode(); } ``` #### 3. **日期时间工具** - **日期计算**:`DateUtils.addDays()` 实现安全日期加减 - **格式化/解析**:线程安全的 `DateFormatUtils` 替代 `SimpleDateFormat` ```java String dateStr = DateFormatUtils.ISO_DATE_FORMAT.format(new Date()); ``` #### 4. **系统与环境工具** - **系统属性**:`SystemUtils` 获取 OS/JVM 信息(如 `SystemUtils.IS_JAVA_1_8`) - **随机生成**:`RandomStringUtils` 生成随机字符串/UUID #### 5. **异常处理工具** - **异常链解析**:`ExceptionUtils.getRootCause()` 快速定位根本原因 - **堆栈跟踪**:`ExceptionUtils.getStackTrace()` 转字符串 --- ### 典型应用场景 - 🔧 **减少样板代码**:自动生成 `equals()`/`hashCode()`/`toString()` - 🛡️ **防御式编程**:空值检查、参数验证(`Validate.notNull()`) - ⏱️ **日期安全操作**:避免 `Calendar` 复杂操作 - 📦 **数据转换**:类型转换(`String` ↔ 数字/日期) --- ### 版本说明 - **`commons-lang 2.x`**:传统版本(包路径 `org.apache.commons.lang`) - **`commons-lang3 3.x`**:升级版(包路径 `org.apache.commons.lang3`),推荐新项目使用 --- ### Maven 依赖示例 ```xml <!-- 传统版本 --> <dependency> <groupId>commons-lang</groupId> <artifactId>commons-lang</artifactId> <version>2.6</version> </dependency> <!-- 新版推荐 --> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-lang3</artifactId> <version>3.12.0</version> </dependency> ``` --- ### 相关问题 1. `commons-lang` 和 `commons-lang3` 有哪些主要区别?[^1] 2. 如何用 `StringUtils` 高效处理中文字符串? 3. `EqualsBuilder` 在深度比较对象时有哪些性能陷阱? 4. 为什么 `DateFormatUtils` 比 `SimpleDateFormat` 更安全? 5. 如何用 `RandomStringUtils` 生成符合密码学要求的随机字符串
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值