记录一下, 启动Servlet页面后提示 Class XXX is not a Servlet

报错如下:

 后台控制台的报错是:

java.lang.ClassCastException

 

-----------------------------------------------------------------------------------

错误原因:

在Servlet中的doGet方法中,开通了request.getReader()而没有使用.

解决方法:

删除改行

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java秒杀系统方案优化高性能高并发学习实战源代码以及笔记..zip 章节笔记 第1章-课程介绍及项目框架搭建 知识点 使用spring boot 搭建项目基础框架 使用Thymeleaf做页面展示,封装Result统一结果 集成 mybatis + druid 做数据操作 继承redis, 使用Jedis操作redis数据, 封装了统一的缓存key. 第2章-实现用户登录以及分布式session功能 学习了自己通过 cookie 实现分布式session, 不使用spring boot默认提供的 知识点 数据库设计 明文密码两次md5处理 JSR303参数校验 + 全局异常处理器 分布式session, 通过cookie完成分布式session功能. 将cookie存放到redis中. 第3章-秒杀功能开发及管理后台 学习到了基础的商品下单流程, 查库存,查是否重复下单,减库存,创建订单等过程 知识点 数据库设计(业务第一原则, 无需太注意三大范式) 完成商品列表页 完成商品详情页 完成订单详情页 第4章-秒杀压测-Jmeter压力测试 学习使用jmeter做压力测试, 学习jmeter变量定义, redis压力测试工具 知识点 jemter入门 自定义变量模拟多用户(线程组 -> 添加 -> 配置元件 -> CVS数据文件设置) jmeter在命令行下的使用. 下载tgz,解压缩并替换windows视窗下面配置的CVS信息路径 执行脚本 ./bin/jmeter.sh -n -t xxx.jmx -l result.jtl 完成后可以将result下载下来导入到 聚合报告 中查看 spring boot 打war包. 添加 plugins 插件 <!--打war包插件--> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-war-plugin</artifactId> <configuration> <failOnMissingWebXml>false</failOnMissingWebXml> </configuration> </plugin> 添加tomcat依赖 <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> <scope>provided</scope> </dependency> 编写启动类 public class ServletApplication extends SpringBootServletInitializer { @Override protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) { return builder.sources(ServletApplication.class); } }
xvii Contents Finishing Your Modules 154 Defining Module-Specific Errors 154 Choosing What to Export 155 Documenting Your Modules 156 Try It Out: Viewing Module Documentation 157 Testing Your Module 162 Running a Module as a Program 164 Try It Out: Running a Module 164 Creating a Whole Module 165 Try It Out: Finishing a Module 165 Try It Out: Smashing Imports 169 Installing Your Modules 170 Try It Out: Creating an Installable Package 171 Summary 174 Exercises 174 Chapter 11: Text Processing 175 Why Text Processing Is So Useful 175 Searching for Files 176 Clipping Logs 177 Sifting through Mail 178 Navigating the File System with the os Module 178 Try It Out: Listing Files and Playing with Paths 180 Try It Out: Searching for Files of a Particular Type 181 Try It Out: Refining a Search 183 Working with Regular Expressions and the re Module 184 Try It Out: Fun with Regular Expressions 186 Try It Out: Adding Tests 187 Summary 189 Exercises 189 Chapter 12: Testing 191 Assertions 191 Try It Out: Using Assert 192 Test Cases and Test Suites 193 Try It Out: Testing Addition 194 Try It Out: Testing Faulty Addition 195 Test Fixtures 196 Try It Out: Working with Test Fixtures 197 02_596543 ftoc.qxd 6/29/05 10:55 PM Page xvii xviii Contents Putting It All Together with Extreme Programming 199 Implementing a Search Utility in Python 200 Try It Out: Writing a Test Suite First 201 Try It Out: A General-Purpose Search Framework 203 A More Powerful Python Search 205 Try It Out: Extending the Search Framework 206 Formal Testing in the Software Life Cycle 207 Summary 208 Chapter 13: Writing a GUI with Python 209 GUI Programming Toolkits for Python 209 PyGTK Introduction 210 pyGTK Resources 211 Creating GUI Widgets with pyGTK 213 Try It Out: Writing a Simple pyGTK Program 213 GUI Signals 214 GUI Helper Threads and the GUI Event Queue 216 Try It Out: Writing a Multithreaded pyGTK App 219 Widget Packing 222 Glade: a GUI Builder for pyGTK 223 GUI Builders for Other GUI Frameworks 224 Using libGlade with Python 225 A Glade Walkthrough 225 Starting Glade 226 Creating a Project 227 Using the Palette to Create a Window 227 Putting Widgets into the Window 228 Glade Creates an XML Representation of the GUI 230 Try It Out: Building a GUI from a Glade File 231 Creating a Real Glade Application 231 Advanced Widgets 238 Further Enhancing PyRAP 241 Summary 248 Exercises 248 Chapter 14: Accessing Databases 249 Working with DBM Persistent Dictionaries 250 Choosing a DBM Module 250 Creating Persistent Dictionaries 251 Try It Out: Creating a Persistent Dictionary 251 02_596543 ftoc.qxd 6/29/05 10:55 PM Page xviii xix Contents Accessing Persistent Dictionaries 252 Try It Out: Accessing Persistent Dictionaries 253 Deciding When to Use DBM and When to Use a Relational Database 255 Working with Relational Databases 255 Writing SQL Statements 257 Defining Tables 259 Setting Up a Database 260 Try It Out: Creating a Gadfly Database 261 Using the Python Database APIs 262 Downloading Modules 263 Creating Connections 263 Working with Cursors 264 Try It Out: Inserting Records 264 Try It Out: Writing a Simple Query 266 Try It Out: Writing a Complex Join 267 Try It Out: Updating an Employee’s Manager 269 Try It Out: Removing Employees 270 Working with Transactions and Committing the Results 271 Examining Module Capabilities and Metadata 272 Handling Errors 272 Summary 273 Exercises 274 Chapter 15: Using Python for XML 275 What Is XML? 275 A Hierarchical Markup Language 275 A Family of Standards 277 What Is a Schema/DTD? 278 What Are Document Models For? 278 Do You Need One? 278 Document Type Definitions 278 An Example DTD 278 DTDs Aren’t Exactly XML 280 Limitations of DTDs 280 Schemas 280 An Example Schema 280 Schemas Are Pure XML 281 Schemas Are Hierarchical 281 Other Advantages of Schemas 281 Schemas Are Less Widely Supported 281 02_596543 ftoc.qxd 6/29/05 10:55 PM Page xix xx Contents XPath 282 HTML as a Subset of XML 282 The HTML DTDs 283 HTMLParser 283 Try It Out: Using HTMLParser 283 htmllib 284 Try It Out: Using htmllib 284 XML Libraries Available for Python 285 Validating XML Using Python 285 What Is Validation? 286 Well-Formedness versus Validation 286 Available Tools 286 Try It Out: Validation Using xmlproc 286 What Is SAX? 287 Stream-based 288 Event-driven 288 What Is DOM? 288 In-memory Access 288 Why Use SAX or DOM 289 Capability Trade-Offs 289 Memory Considerations 289 Speed Considerations 289 SAX and DOM Parsers Available for Python 289 PyXML 290 xml.sax 290 xml.dom.minidom 290 Try It Out: Working with XML Using DOM 290 Try It Out: Working with XML Using SAX 292 Intro to XSLT 293 XSLT Is XML 293 Transformation and Formatting Language 293 Functional,Template-Driven 293 Using Python to Transform XML Using XSLT 294 Try It Out: Transforming XML with XSLT 294 Putting It All Together: Working with RSS 296 RSS Overview and Vocabulary 296 Making Sense of It All 296 RSS Vocabulary 297 An RSS DTD 297 02_596543 ftoc.qxd 6/29/05 10:55 PM Page xx xxi Contents A Real-World Problem 297 Try It Out: Creating an RSS Feed 298 Creating the Document 300 Checking It Against the DTD 301 Another Real-World Problem 301 Try It Out: Creating An Aggregator 301 Summary 303 Exercises 303 Chapter 16: Network Programming 305 Try It Out: Sending Some E-mail 305 Understanding Protocols 307 Comparing Protocols and Programming Languages 307 The Internet Protocol Stack 308 A Little Bit About the Internet Protocol 309 Internet Addresses 309 Internet Ports 310 Sending Internet E-mail 311 The E-mail File Format 311 MIME Messages 313 MIME Encodings: Quoted-printable and Base64 313 MIME Content Types 314 Try It Out: Creating a MIME Message with an Attachment 315 MIME Multipart Messages 316 Try It Out: Building E-mail Messages with SmartMessage 320 Sending Mail with SMTP and smtplib 321 Try It Out: Sending Mail with MailServer 323 Retrieving Internet E-mail 323 Parsing a Local Mail Spool with mailbox 323 Try It Out: Printing a Summary of Your Mailbox 324 Fetching Mail from a POP3 Server with poplib 325 Try It Out: Printing a Summary of Your POP3 Mailbox 327 Fetching Mail from an IMAP Server with imaplib 327 Try It Out: Printing a Summary of Your IMAP Mailbox 329 IMAP’s Unique Message IDs 330 Try It Out: Fetching a Message by Unique ID 330 Secure POP3 and IMAP 331 Webmail Applications Are Not E-mail Applications 331 Socket Programming 331 Introduction to Sockets 332 Try It Out: Connecting to the SuperSimpleSocketServer with Telnet 333 02_596543 ftoc.qxd 6/29/05 10:55 PM Page xxi xxii Contents Binding to an External Hostname 334 The Mirror Server 335 Try It Out: Mirroring Text with the MirrorServer 336 The Mirror Client 336 SocketServer 337 Multithreaded Servers 339 The Python Chat Server 340 Design of the Python Chat Server 340 The Python Chat Server Protocol 341 Our Hypothetical Protocol in Action 341 Initial Connection 342 Chat Text 342 Server Commands 342 General Guidelines 343 The Python Chat Client 346 Single-Threaded Multitasking with select 348 Other Topics 350 Miscellaneous Considerations for Protocol Design 350 Trusted Servers 350 Terse Protocols 350 The Twisted Framework 351 Deferred Objects 351 The Peer-to-Peer Architecture 354 Summary 354 Exercises 354 Chapter 17: Extension Programming with C 355 Extension Module Outline 356 Building and Installing Extension Modules 358 Passing Parameters from Python to C 360 Returning Values from C to Python 363 The LAME Project 364 The LAME Extension Module 368 Using Python Objects from C Code 380 Summary 383 Exercises 383 Chapter 18: Writing Shareware and Commercial Programs 385 A Case Study: Background 385 How Much Python Should You Use? 386 02_596543 ftoc.qxd 6/29/05 10:55 PM Page xxii xxiii Contents Pure Python Licensing 387 Web Services Are Your Friend 388 Pricing Strategies 389 Watermarking 390 Other Models 394 Selling as a Platform,Rather Than a Product 395 Your Development Environment 395 Finding Python Programmers 396 Training non-Python Programmers 397 Python Employment Resources 397 Python Problems 397 Porting to Other Versions of Python 397 Porting to Other Operating Systems 398 Debugging Threads 399 Common Gotchas 399 Portable Distribution 400 Essential Libraries 401 Timeoutsocket 401 PyGTK 402 GEOip 402 Summary 403 Chapter 19: Numerical Programming 405 Numbers in Python 405 Integers 406 Long Integers 406 Floating-point Numbers 407 Formatting Numbers 408 Characters as Numbers 410 Mathematics 412 Arithmetic 412 Built-in Math Functions 414 The math Module 415 Complex Numbers 416 Arrays 418 The array Module 420 The numarray Package 422 Using Arrays 422 Computing the Standard Deviation 423 Summary 424 Exercises 425 02_596543 ftoc.qxd 6/29/05 10:55 PM Page xxiii xxiv Contents Chapter 20: Python in the Enterprise 427 Enterprise Applications 428 Document Management 428 The Evolution of Document Management Systems 429 What You Want in a Document Management System 430 People in Directories 431 Taking Action with Workflow 432 Auditing,Sarbanes-Oxley,and What You Need to Know 433 Auditing and Document Management 434 Working with Actual Enterprise Systems 435 Introducing the wftk Workflow Toolkit 435 Try It Out: Very Simple Record Retrieval 436 Try It Out: Very Simple Record Storage 438 Try It Out: Data Storage in MySQL 439 Try It Out: Storing and Retrieving Documents 441 Try It Out: A Document Retention Framework 446 The python-ldap Module 448 Try It Out: Using Basic OpenLDAP Tools 449 Try It Out: Simple LDAP Search 451 More LDAP 453 Back to the wftk 453 Try It Out: Simple Workflow Trigger 454 Try It Out: Action Queue Handler 456 Summary 458 Exercises 458 Chapter 21: Web Applications and Web Services 459 REST: The Architecture of the Web 460 Characteristics of REST 460 A Distributed Network of Interlinked Documents 461 A Client-Server Architecture 461 Servers Are Stateless 461 Resources 461 Representations 462 REST Operations 462 HTTP: Real-World REST 463 Try It Out: Python’s Three-Line Web Server 463 The Visible Web Server 464 Try It Out: Seeing an HTTP Request and Response 465 The HTTP Request 466 The HTTP Response 467 02_596543 ftoc.qxd 6/29/05 10:55 PM Page xxiv xxv Contents CGI: Turning Scripts into Web Applications 468 Try It Out: Running a CGI Script 469 The Web Server Makes a Deal with the CGI Script 470 CGI’s Special Environment Variables 471 Accepting User Input through HTML Forms 473 The cgi Module: Parsing HTML Forms 474 Try It Out: Printing Any HTML Form Submission 478 Building a Wiki 480 The BittyWiki Core Library 481 Back-end Storage 481 WikiWords 481 Writing the BittyWiki Core 481 Try It Out: Creating Wiki Pages from an Interactive Python Session 483 The BittyWiki Web Interface 484 Resources 484 Request Structure 484 But Wait—There’s More (Resources) 485 Wiki Markup 486 Web Services 493 How Web Services Work 494 REST Web Services 494 REST Quick Start: Finding Bargains on Amazon.com 495 Try It Out: Peeking at an Amazon Web Services Response 496 Introducing WishListBargainFinder 497 Giving BittyWiki a REST API 500 Wiki Search-and-Replace Using the REST Web Service 503 Try It Out: Wiki Searching and Replacing 507 XML-RPC 508 XML-RPC Quick Start: Get Tech News from Meerkat 509 The XML-RPC Request 511 Representation of Data in XML-RPC 512 The XML-RPC Response 513 If Something Goes Wrong 513 Exposing the BittyWiki API through XML-RPC 514 Try It Out: Manipulating BittyWiki through XML-RPC 517 Wiki Search-and-Replace Using the XML-RPC Web Service 518 SOAP 520 SOAP Quick Start: Surfing the Google API 520 The SOAP Request 522 The SOAP Response 524 If Something Goes Wrong 524 02_596543 ftoc.qxd 6/29/05 10:55 PM Page xxv xxvi Contents Exposing a SOAP Interface to BittyWiki 525 Try It Out: Manipulating BittyWiki through SOAP 526 Wiki Search-and-Replace Using the SOAP Web Service 527 Documenting Your Web Service API 529 Human-Readable API Documentation 529 The BittyWiki REST API Document 529 The BittyWiki XML-RPC API Document 529 The BittyWiki SOAP API Document 530 The XML-RPC Introspection API 530 Try It Out: Using the XML-RPC Introspection API 530 WSDL 531 Try It Out: Manipulating BittyWiki through a WSDL Proxy 533 Choosing a Web Service Standard 534 Web Service Etiquette 535 For Consumers of Web Services 535 For Producers of Web Services 535 Using Web Applications as Web Services 536 A Sampling of Publicly Available Web Services 536 Summary 538 Exercises 538 Chapter 22: Integrating Java with Python 539 Scripting within Java Applications 540 Comparing Python Implementations 541 Installing Jython 541 Running Jython 542 Running Jython Interactively 542 Try It Out: Running the Jython Interpreter 542 Running Jython Scripts 543 Try It Out Running a Python Script 543 Controlling the jython Script 544 Making Executable Commands 545 Try It Out: Making an Executable Script 546 Running Jython on Your Own 546 Packaging Jython-Based Applications 547 Integrating Java and Jython 547 Using Java Classes in Jython 548 Try It Out: Calling on Java Classes 548 Try It Out: Creating a User Interface from Jython 550 Accessing Databases from Jython 552 Working with the Python DB API 553 02_596543 ftoc.qxd 6/29/05 10:55 PM Page xxvi xxvii Contents Setting Up a Database 554 Try It Out: Create Tables 555 Writing J2EE Servlets in Jython 558 Setting Up an Application Server 559 Adding the PyServlet to an Application Server 560 Extending HttpServlet 561 Try It Out: Writing a Python Servlet 562 Choosing Tools for Jython 564 Testing from Jython 565 Try It Out: Exploring Your Environment with Jython 565 Embedding the Jython Interpreter 566 Calling Jython Scripts from Java 566 Try It Out: Embedding Jython 567 Compiling Python Code to Java 568 Handling Differences between C Python and Jython 569 Summary 570 Exercises 571 Appendix A: Answers to Exercises 573 Appendix B: Online Resources 605 Appendix C: What’s New in Python 2.4 609 Glossary 613 Index 623 Contents Acknowledgments xxix Introduction xxxi Chapter 1: Programming Basics and Strings 1 How Programming Is Different from Using a Computer 1 Programming Is Consistency 2 Programming Is Control 2 Programming Copes with Change 2 What All That Means Together 3 The First Steps 3 Starting codeEditor 3 Using codeEditor’s Python Shell 4 Try It Out: Starting the Python Shell 4 Beginning to Use Python—Strings 5 What Is a String? 5 Why the Quotes? 6 Try It Out: Entering Strings with Different Quotes 6 Understanding Different Quotes 6 Putting Two Strings Together 8 Try It Out: Using + to Combine Strings 8 Putting Strings Together in Different Ways 9 Try It Out: Using a Format Specifier to Populate a String 9 Try It Out: More String Formatting 9 Displaying Strings with Print 10 Try It Out: Printing Text with Print 10 Summary 10 Exercises 11 Chapter 2: Numbers and Operators 13 Different Kinds of Numbers 13 Numbers in Python 14 Try It Out: Using Type with Different Numbers 14 Try It Out: Creating an Imaginary Number 15 02_596543 ftoc.qxd 6/29/05 10:55 PM Page xi xii Contents Program Files 15 Try It Out: Using the Shell with the Editor 16 Using the Different Types 17 Try It Out Including Different Numbers in Strings 18 Try It Out: Escaping the % Sign in Strings 18 Basic Math 19 Try It Out Doing Basic Math 19 Try It Out: Using the Modulus Operation 20 Some Surprises 20 Try It Out: Printing the Results 21 Using Numbers 21 Order of Evaluation 21 Try It Out: Using Math Operations 21 Number Formats 22 Try It Out: Using Number Formats 22 Mistakes Will Happen 23 Try It Out: Making Mistakes 23 Some Unusual Cases 24 Try It Out: Formatting Numbers as Octal and Hexadecimal 24 Summary 24 Exercises 25 Chapter 3: Variables—Names for Values 27 Referring to Data – Using Names for Data 27 Try It Out: Assigning Values to Names 28 Changing Data Through Names 28 Try It Out: Altering Named Values 29 Copying Data 29 Names You Can’t Use and Some Rules 29 Using More Built-in Types 30 Tuples—Unchanging Sequences of Data 30 Try It Out: Creating and Using a Tuple 30 Try It Out: Accessing a Tuple Through Another Tuple 31 Lists—Changeable Sequences of Data 33 Try It Out Viewing the Elements of a List 33 Dictionaries—Groupings of Data Indexed by Name 34 Try It Out: Making a Dictionary 34 Try It Out: Getting the Keys from a Dictionary 35 Treating a String Like a List 36 Special Types 38 02_596543 ftoc.qxd 6/29/05 10:55 PM Page xii xiii Contents Other Common Sequence Properties 38 Referencing the Last Elements 38 Ranges of Sequences 39 Try It Out: Slicing Sequences 39 Growing Lists by Appending Sequences 40 Using Lists to Temporarily Store Data 40 Try It Out: Popping Elements from a List 40 Summary 41 Exercises 42 Chapter 4: Making Decisions 43 Comparing Values—Are They the Same? 43 Try It Out: Comparing Values for Sameness 43 Doing the Opposite—Not Equal 45 Try It Out: Comparing Values for Difference 45 Comparing Values—Which One Is More? 45 Try It Out: Comparing Greater Than and Less Than 45 More Than or Equal,Less Than or Equal 47 Reversing True and False 47 Try It Out: Reversing the Outcome of a Test 47 Looking for the Results of More Than One Comparison 48 How to Get Decisions Made 48 Try It Out: Placing Tests within Tests 49 Repetition 51 How to Do Something—Again and Again 51 Try It Out: Using a while Loop 51 Stopping the Repetition 52 Try It Out: Using else While Repeating 54 Try It Out: Using continue to Keep Repeating 54 Handling Errors 55 Trying Things Out 55 Try It Out: Creating an Exception with Its Explanation 56 Summary 57 Exercises 58 Chapter 5: Functions 59 Putting Your Program into Its Own File 59 Try It Out: Run a Program with Python -i 61 02_596543 ftoc.qxd 6/29/05 10:55 PM Page xiii xiv Contents Functions: Grouping Code under a Name 61 Try It Out: Defining a Function 61 Choosing a Name 62 Describing a Function in the Function 63 Try It Out: Displaying __doc__ 63 The Same Name in Two Different Places 64 Making Notes to Yourself 65 Try It Out: Experimenting with Comments 65 Asking a Function to Use a Value You Provide 66 Try It Out Invoking a Function with Parameters 67 Checking Your Parameters 68 Try It Out: Determining More Types with the type Function 69 Try It Out: Using Strings to Compare Types 69 Setting a Default Value for a Parameter—Just in Case 70 Try It Out: Setting a Default Parameter 70 Calling Functions from within Other Functions 71 Try It Out: Invoking the Completed Function 72 Functions Inside of Functions 72 Flagging an Error on Your Own Terms 73 Layers of Functions 74 How to Read Deeper Errors 74 Summary 75 Exercises 76 Chapter 6: Classes and Objects 79 Thinking About Programming 79 Objects You Already Know 79 Looking Ahead: How You Want to Use Objects 81 Defining a Class 81 How Code Can Be Made into an Object 81 Try It Out: Defining a Class 82 Try It Out: Creating an Object from Your Class 82 Try It Out: Writing an Internal Method 84 Try It Out: Writing Interface Methods 85 Try It Out: Using More Methods 87 Objects and Their Scope 89 Try It Out: Creating Another Class 89 Summary 92 Exercises 93 02_596543 ftoc.qxd 6/29/05 10:55 PM Page xiv xv Contents Chapter 7: Organizing Programs 95 Modules 96 Importing a Module So That You Can Use It 96 Making a Module from Pre-existing Code 97 Try It Out: Creating a Module 97 Try It Out: Exploring Your New Module 98 Using Modules—Starting With the Command Line 99 Try It Out: Printing sys.argv 100 Changing How Import Works—Bringing in More 101 Packages 101 Try It Out: Making the Files in the Kitchen Class 102 Modules and Packages 103 Bringing Everything into the Current Scope 103 Try It Out: Exporting Modules from a Package 104 Re-importing Modules and Packages 104 Try It Out: Examining sys.modules 105 Basics of Testing Your Modules and Packages 106 Summary 106 Exercises 107 Chapter 8: Files and Directories 109 File Objects 109 Writing Text Files 110 Reading Text Files 111 Try It Out: Printing the Lengths of Lines in the Sample File 112 File Exceptions 113 Paths and Directories 113 Paths 114 Directory Contents 116 Try It Out: Getting the Contents of a Directory 116 Try It Out: Listing the Contents of Your Desktop or Home Directory 118 Obtaining Information about Files 118 Recursive Directory Listings 118 Renaming,Moving,Copying,and Removing Files 119 Example: Rotating Files 120 Creating and Removing Directories 121 Globbing 122 02_596543 ftoc.qxd 6/29/05 10:55 PM Page xv xvi Contents Pickles 123 Try It Out: Creating a Pickle File 123 Pickling Tips 124 Efficient Pickling 125 Summary 125 Exercises 125 Chapter 9: Other Features of the Language 127 Lambda and Filter: Short Anonymous Functions 127 Reduce 128 Try It Out: Working with Reduce 128 Map: Short-Circuiting Loops 129 Try It Out: Use Map 129 Decisions within Lists—List Comprehension 130 Generating Lists for Loops 131 Try It Out: Examining an xrange Object 132 Special String Substitution Using Dictionaries 133 Try It Out: String Formatting with Dictionaries 133 Featured Modules 134 Getopt—Getting Options from the Command Line 134 Using More Than One Process 137 Threads—Doing Many Things in the Same Process 139 Storing Passwords 140 Summary 141 Exercises 142 Chapter 10: Building a Module 143 Exploring Modules 143 Importing Modules 145 Finding Modules 145 Digging through Modules 146 Creating Modules and Packages 150 Try It Out: Creating a Module with Functions 150 Working with Classes 151 Defining Object-Oriented Programming 151 Creating Classes 151 Try It Out: Creating a Meal Class 152 Extending Existing Classes 153 02_596543 ftoc.qxd 6/29/05 10:55 PM Page xvi
jwx是开源的java公众号开发MVC框架,基于spring配置文件和微信消息或事件注解,通过微信上下文处理一个或多个微信公众号服务请求。目的主要有两个,其一生封装微信请求xml消息为java实体对象,将返回对象转换为xml响应消息;其二是封装微信接口为java服务。微信公众号采用web服务作为消息与第三方平台发生交互,数据格式主要是xml和json,普通的web请求响应机制采用xml数据格式交互,微信接口服务采用json数据格式。jwx主要对这两个方面做了封装处理,另外借鉴springmvc的请求处理方式,以WeixinDispatcherServlet类作为消息分发控制器,通过消息组装工厂生成请求消息或事件实体,根据消息或事件类型,在消息策略处理工厂查找处理策略,获取相应的微信处理方法,Servlet获取到处理方法后,请求线程池获取线程调用微信方法,根据微信方法的返回值,生成请求的xml响应。本说明文档将分章节说明jwx框架的特征、快速入门、配置、扩展等各个方面。 一、特征 消息重排自动处理,提供消息重排缓存接口 明文/加密模式无感知切换 常用的微信接口服务封装 提供线程池执行微信方法,方法调用线程池大小可配置 长任务消息推送 通过微信上下文配置支持多个微信公众号处理 提供统一的异常处理机制 提供access_token自动更新机制 请求消息组装 灵活的响应消息类型 二、快速入门 本章教材提供一个最简单的例子,用户在微信公众号发一条foo的文本请求消息,公众号响应一条bar的文本响应消息。 1、maven配置文件 通过maven生成一个webapp项目,例如项目名为weixin,在maven配置文件pom.xml中添加jwx依赖,jwx的1.1.1jar包已经提交到maven中心仓库,通过中心仓库搜索jwx关键字可以获取jar包依赖配置。 com.github.jweixin jwx 1.1.1 2、web.xml文件配置 web.xml是web应用的配置文件,jwx从spring配置文件中获取配置信息,所以必须配置spring上下文环境;另外,需要配置微信消息处理分发Servlet(WeixinDispatcherServlet),用于处理微信送过来的请求消息或事件。jwx对springmvc没有依赖关系,web mvc框架可以根据实际需要配置。 org.springframework.web.context.ContextLoaderListener weixin com.github.jweixin.jwx.servlet.WeixinDispatcherServlet 1 weixin /wx/* load-on-startup表示Servlet在web应用启动阶段加载,数字代表了启动次序,如果项目使用了springmvc框架,可以调整该数字为2,放到springmvc框架后面启动加载,但实际上Servlet启动次序并没有太大的关系。 spring配置是jwx必须的,如果没有配置spring上下文,jwx在启动阶段会报错。 url-pattern模式匹配微信公众号平台服务器配置的URL配置,如果需要处理多个微信公众号,可以配置多个servlet-mapping或者使用路径通配符匹配多个url链接。 3、spring配置文件 spring配置文件applicationContext.xml里面需要配置WeixinConfigurer,这是jwx唯一必须配置项,如果没有配置,启动阶段会报错。 com.telecomjs.yc.controller component-scan配置了微信接口服务类,里面包含常用的微信公众号接口服务,例如菜单管理、消息服务、二维码服务、用户管理、微信网页授权、素材管理等服务内容,在web应用控制器类和微信控制器类里面可以通过@Autowired注解来注入服务。本配置并不是必须项。 WeixinConfigurer是唯一需要配置的部分,packages属性必须配置,里面是微信控制器包路径列表,WeixinDispatcherServlet启动阶段会扫描包路径及其下面的子包路径,如果类拥有@Weixin注解,则该类会被当作微信控制器类加载到微信上下文。 除了packages属性是必须配置的,其他配置都有缺省值,包括消息缓存、微信方法线程池的大小、微信方法调用超时阀值等,这部分内容放在配置部分说明了。 4、编写微信控制器类 当配置完上面的3个部分,所有的配置文件部分就结束了,是不是很简单呢。下面我们只需要写微信控制器类就能让我们的微信公众号活起来了。微信控制器类是用@Weixin注解的普通类,与sprngmvc里面的controller很类似,方法的执行也很类似。我们在com.telecomjs.yc.controller包下建一个java类WeixinController,如下: package com.telecomjs.yc.controller; import com.github.jweixin.jwx.context.Weixin; import com.github.jweixin.jwx.message.annotation.TextMsg; @Weixin(value="/wx/coreServlet", appID="xxx", appSecret="xxx", encodingAESKey="xxx", token="xxx") public class WeixinController { @TextMsg("foo") public String foo(){ return "bar"; } } @Weixin需要配置value值,这个实际就是微信服务器配置里面URL最后的部分,当然不包含域名和web应用的上下文,切记,不能包含web应用上下文,其他4个部分配置内容也是公众号配置内容,我们只需要登录到公众号看下填进去就行了。如果没有配置encodingAESKey,那么是不能处理加密消息的,如果有log4j的配置文件,启动阶段会给出告警信息的。 同一个公众号可以配置多个@Weixin注解控制器类,其中只需要一个有其他4项配置就可以了,如果多个控制器类配置了其他4个配置项,如果相对应的配置项值不相同,启动阶段会报错。 不同微信公众号是通过@Weixin的value值区分的,该值同时是微信上下文的查找关键字。 foo方法上面有@TextMsg注解,是定义的微信方法,在Servlet启动时通过包扫描加载到微信上下文对象中。jwx针对微信消息或事件类型设计了一组微信注解,基本涵盖了微信公众号定义的消息和事件类型。 @TextMsg是文本消息注解,代表请求类型的是文本消息,value值是发送的文本消息内容。处理文本适配模式,@TextMsg还支持正则表达式适配模式,这部分内容在使用参考部分说明。 本例中微信方法并没有设置参数,实际可以灵活设置参数,例如我们可以在方法中设置HttpServletRequest request,HttpServletResponse response,InMessage in, WeixinContext context等参数,这部分内容也放在使用参考部分说明。 本例中方法的返回类型是String,代表响应的消息内容是文本消息,jwx提供了丰富的返回值类型,这部分内容会在使用参考部分详细说明。 5、启动web应用 上面就是这个最简单例子的全部内容,让我们启动web应用,进入到我们的公众号,输入foo文本提交,看看返回的是不是bar这个内容了,如果是,恭喜你,你已经初步掌握了jwx的使用方法。下面更多的内容等着你呢! 三、配置说明 spring配置文件中唯一需要配置的bean是WeixinConfigurer类,是可选配置,但里面封装了微信接口服务类,建议一定要配置进spring配置文件中。 1、微信接口服务 微信接口服务类位于com.github.jweixin.jwx.weixin.service包中,在spring配置文件中通过扫描包载入服务,在web mvc框架和微信控制器类中都可以通过@Autowired注解注入,与其他spring普通的服务类主键使用方式一致,服务类每个方法都有accessToken参数,这个参数指的是微信access_token,在微信控制器类方法中,可以通过设置方法的WeixinContext context参数获取,在web mvc框架中,可以通过WeixinContextHelper类的静态方法getAccessToken(String url)获取。 CustomMsgService 客服消息服务 MassMsgService 群发消息服务 MaterialService 永久素材管理 MediaService 临时素材管理 MenuService 菜单服务 QrcodeService 二维码服务 TagService 标签服务 TemplateService 模板管理及消息发送 WebAuthService 微信网页授权服务 UserService 微信用户服务 SystemService 获取地址列表及长链接转短链接等其他类型服务 2、WeixinConfigurer配置 WeixinConfigurer是微信上下文全局配置类,里面包含了处理微信类扫描、微信消息重排处理、微信方法执行线程池大小、微信方法调用超时阀值等方面的配置,packages包扫描配置是唯一必须的配置部分,这个配置在快速入门部分已经描述,其他部分配置都有缺省配置,不是必须配置部分。 a、微信消息重排处理messageKeyCache配置 微信在处理消息推送时,如果没有获得响应,会隔5秒重试,最多重试3次。jwx在接到消息推送时,需要判断该消息是否已经接受过,如果接受过,则需要放弃处理。jwx设计了MessageKeyCache接口用于处理消息重排,里面需要实现唯一的方法public boolean hasMessageKey(String key);如果系统已经缓存了消息key值,返回true。jwx实现了一个默认的消息key值缓存ConcurrentHashMapMessageKeyCache。如果我们要设置缓存清理间隔,可以采用如下配置: <!-- 设置消息key缓存清理间隔,单位秒 --> com.telecomjs.yc.controller 另外我们可以实现自己的消息key缓存类,只需要实现MessageKeyCache接口就可以了,比如我们可以采用redis作为消息key值缓存数据库。 b、微信方法线程池大小threadPoolSize设置 微信方法是由Servlet在获取请求消息或事件的策略后取得,Servlet取得微信方法后,在线程池中获取线程执行微信方法。缺省线程池的大小是10个,如果微信公众并发比较频繁,我们可以调整线程池的大小,以提高处理效率。 如果我们调整线程池大小为100,可以采用如下配置: <!-- 设置消息key缓存清理间隔,单位秒 --> com.telecomjs.yc.controller <!-- 设置微信方法执行线程池大小 --> c、微信方法调用超时阀值weixinMethodTimeoutThreshold设置 微信推送消息或事件如果超过5秒,微信会中断连接,有时候微信方法的执行会超过5秒钟,针对这种情况,jwx采用微信方法调用超时阀值机制,如果微信方法调用线程不能在超时阀值内处理完毕,Servlet会先行返回http响应,后续Servlet会等待方法执行完毕,然后通过客服消息返回响应,对用户来说并没有感知。缺省的微信方法调用超时阀值是3000毫秒,该值可以通过配置调整,如下我们将超时阀值改成4秒: <!-- 设置消息key缓存清理间隔,单位秒 --> com.telecomjs.yc.controller <!-- 设置微信方法执行线程池大小 --> <!-- 设置微信方法调用超时阀值,单位毫秒 --> 四、使用参考 本部分会全面讲解jwx的概念及使用方法。 1、主要概念 微信上下文:微信上下文(WexinContext)是jwx最重要的部分,jwx可以同时处理多个微信公众号,每个公众号在jwx框架中对应一个微信上下文,微信上下文持有一个微信公众号所有的配置信息及处理策略。url是微信公众号配置的服务器地址的最后部分(不包括域名和web应用上下文),是识别微信公众号的唯一标识,透过url我们可以通过微信上下文帮助类(WeixinContextHelper)的静态方法获取到微信上下文及访问token,另外,在微信方法中我们也可以通过注入WeixinContext参数来获得微信上下文。微信上下文还包含了微信的access_token、appID、appSecret、encodingAESKey这些微信公众号的配置内容。微信上下文还保存微信方法与消息注解的策略对应关系,是微信消息能够得到处理的最重要的部分。微信上下文通过@Weixin注解来配置。 微信消息注解:jwx定义了14个消息或事件注解,涵盖了目前所有的微信消息和事件类型,这些注解定义在包com.github.jweixin.jwx.message.annotation中,微信注解代表了消息或事件类型,可以通过微信注解配置识别请求消息类型,获取相应的微信处理方法。 微信方法:被微信消息注解包围的方法,通过微信方法,我们可以处理微信公众号请求消息,返回公众号的响应消息。 2、@Weixin注解 @Weixin是用来配置微信上下文的,该注解使用在微信控制器类上。每个被@Weixin注解包围的类会在web应用启动时被扫描,配置项会加载到微信上下文中,@Weixin注解的参数说明: value:代表微信上下文关键字,不能为空,在微信公众号基本配置中,处于URL配置的最后部分。例如微信公众号的URL(服务器地址)配置是:http://nalan_weixin.tunnel.qydev.com/weixin/wx/coreServlet,其中http://nalan_weixin.tunnel.qydev.com是主机栏,/weixin是web应用的上下文栏,那么value值应该是/wx/coreServlet,一个公众号可以有多个类拥有@Weixin注解,如果多个注解的value相同,则会认为是同一个微信上下文,在jwx中,区分上下文的唯一标识就是@Weixin注解的value值配置。@Weixin注解还有其他4个配置项,都有缺省值,在一个微信控制器类中配置了其他4个值,那么相同value值得控制器类只需要配置value项就可以了,如果value配置项相同,而其他4个配置项的同项配置不同,jwx在初始启动扫描阶段会给出报错提示。 token:代表微信公众号基本配置中的Token(令牌)项的值。 encodingAESKey:代表微信公众号基本配置中的EncodingAESKey(消息加解密密钥),该项如果没有配置,那么jwx不能处理加密的请求消息,在jwx初始启动阶段会给出告警提示。如果我们配置了消息加解密方式为安全模式,没有配置encodingAESKey项,则运行阶段会报错。另外如果在加密请求消息到达时报如下错误:java.security.InvalidKeyException:illegal Key Size,则说明当前运行的JDK没有用JCE无限制权限策略文件替换相应的安全jar包,**解决方案:在官方网站下载JCE无限制权限策略文件(请到官网下载对应的版本, 例如JDK7的下载地址:http://www.oracle.com/technetwork/java/javase/downloads/jce-7-download-432124.html下载后解压,可以看到local_policy.jar和US_export_policy.jar以及readme.txt,如果安装了JRE,将两个jar文件放到%JRE_HOME%\lib\security目录下覆盖原来的文件;如果安装了JDK,将两个jar文件放到%JDK_HOME%\jre\lib\security目录下覆盖原来文件**。 appID:代表微信公众号基本配置中的AppID(应用ID)。 appSecret:代表微信公众号基本配置中的AppSecret(应用密钥)。 3、微信方法 在微信控制器类中,被微信消息(事件)注解包围的方法,被称为微信方法,微信方法是MVC框架里面的C部分,它控制着对请求消息的处理逻辑,并且返回响应消息。微信注解主要用于适配请求消息(事件)的类型及关键字内容,当适配成功后,由对应的微信方法执行处理逻辑,并且通过方法的返回值返回响应消息。微信方法的参数在请求消息到达时由Servlet注入,目前微信方法参数可以是HttpServletRequest request,HttpServletResponse response,InMessage in, WeixinContext context里面的任意次序和数量的组合,参数中InMessage可以是与注解对应的子类,对于@ExceptionHandler注解,可以添加Throwable及其子类作为方法参数,需要注意的是如果参数类型与实际消息类型或异常类型不能匹配,则该参数会被置为空。 4、微信注解 jwx定义了14个消息或事件注解,涵盖了目前所有的微信消息和事件类型,下面逐个讲解没有注解的使用。 @TextMsg @ClickEvent 5、响应类型

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值