Some small questions about the use of Fastjson for Jsoup crawl data after spring consolidates velocity

Source: Internet
Author: User

1. Import Jar Package

<project xmlns= "http://maven.apache.org/POM/4.0.0" xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance" xsi: schemalocation= "http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" > < Modelversion>4.0.0</modelversion> <groupId>com.zdf.jsoup</groupId> <artifactId>  Zdfjsoup</artifactid> <version>0.0.1-SNAPSHOT</version> <packaging>war</packaging> <dependencies> <dependency> <groupId>org.jsoup</groupId> <artifactid>jsoup</ artifactid> <version>1.11.2</version> </dependency> <dependency> <groupId>  Com.alibaba</groupid> <artifactId>fastjson</artifactId> <version>1.2.45</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId> spring-core</artifactid> <version>4.3.8.RELEASE</version> </dependency> <dependenCy> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>4.3.8.RELEASE</version> </dependency> <dependency> <groupId> Org.springframework</groupid> <artifactId>spring-webmvc</artifactId> <version>4.3.8.  release</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context-support</artifactId> <version>4.3.8.RELEASE</version> </ dependency> <dependency> <groupId>org.apache.velocity</groupId> <artifactid>velocity </artifactId> <version>1.6.3</version> </dependency> <dependency> <groupId> Org.apache.velocity</groupid> <artifactId>velocity-tools</artifactId> <version>2.0</ version> </dependency> <dependency> <groupId>log4j</groupId> &LT;ARTIFACtid>log4j</artifactid> <version>1.2.11</version> </dependency> </dependencies> </project>

  

2. Code writing

Package Com.zdf.jsoup;import Java.io.ioexception;import Java.util.arraylist;import java.util.list;import Org.jsoup.connection;import Org.jsoup.jsoup;import Org.jsoup.nodes.document;import Org.jsoup.nodes.Element;import Org.jsoup.select.elements;import Org.springframework.stereotype.controller;import Org.springframework.web.bind.annotation.requestmapping;import Org.springframework.web.bind.annotation.responsebody;import Com.alibaba.fastjson.json;import Com.zdf.jsoup.domain.Ticket; @Controllerpublic class Jsouptest {@RequestMapping ("/jsouptest") public @ResponseBody List<ticket> jsouptest () {String URL = "Http://dynamic.12306.cn/otn/board/boardMore"; Connection connect = jsoup.connect (URL), try {list<ticket> arr = new arraylist<ticket> ();D ocument Document = C Onnect.get (); Element table = document.getElementById ("Table_board_more"); Elements tr = Table.getelementsbytag ("tr"), for (element Element:tr) {element td = Element.child (0), if (Td.val (). Equals (" Shanghai ")) {System.out.printlN ("Departure:" +td.val () + "terminus:" +element.child (1). Val ()); Ticket Ticket = new Ticket () ticket.setstartplace ("Shanghai"); Ticket.setendplace (Element.child (1). Val ()); Arr.add (Ticket) ;}} return arr;} catch (IOException e) {//TODO auto-generated catch Blocke.printstacktrace ();} return null;} Jump page @requestmapping ("/") public String Tovm () {return "Velocity/ticket";}}

Mistakes made:

1, although import Fastjson.jar but did not configure the conversion in Web. XML, so report no converter found for return value of Type:class java.util.ArrayList

Solution:

<!--processor adapter--><bean class= " Org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter "><property name=" Messageconverters "><list><ref bean=" Mappingfastjsonhttpmessageconverter "/></list></ property></bean><!--View parser--><!--Velocity parser, replacing the original JSP--<bean id= " Mappingfastjsonhttpmessageconverter "          class=" Com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter ">        <property name=" Supportedmediatypes ">            <list>                <value>application/json;charset=UTF-8</value>            </list>        </property>    </bean>

2, just start using JSP to represent the page is the source code, because it is configured by the velocity of the view resolver, so this error will occur

Workaround: Use the VM to import the file

Two ways: Use # include ("ticket.jsp") is the current position to insert the entire ticket.jsp page, adjust as needed.

Some small questions about the use of Fastjson for Jsoup crawl data after spring consolidates velocity

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.