最近、SpringMVCフレームワークに連絡し始めました。このフレームワークは、使用するのに非常に便利です。フレームワークが構築された後、コードはほぼ同じパターンです。もちろん、この時点に到達するには、SpringMVC関連の構成が完了し、構成が正しいことを確認する必要があります。
S-Flat RingMVCに関する私の最初のブログとして、このブログは主にSpringMVCを構成する方法について説明しており、通常はBeanエンティティに戻ることができます。ここのBeanエンティティは、通常、JSON文字列の形でフロントエンドに戻されます。
使用される開発ツールはEclipseであり、これも比較的人気のある開発ツールでもあります。それはすべての人が使用すると見なすことができますが、習熟度レベルは異なります!
特定の構成は次のとおりです。
web.xml:
<?xml version = "1.0" encoding = "utf-8"?> <web-app xmlns:xsi = "http://www.w3.org/2001/xmlschema-instance" xmlns = "http://xmlns.jcp.org/xml/ns/javaee xmlns:Context = "http://www.springframework.org/schema/context" xmlns:mvc = "http://www.springframework.org/schema/mvc" " xsi:schemalocation = "http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd http://www.springframework.org.org.org/schema http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/skema/mv.pring-4.pring id = "webapp_id" version = "3.1"> <context-param> <param-name> contextconfiglocation </param-name> <param-value> classpath:applicationContext.xml </param-value> </context-param> <リスナー> <リスナークラス> org.springframework.web.context.context.context.context.context.context.context <display-name> returnjsondemo </display-name> <welcome-file-list> <welcome-file> index.jsp </welcome-file> </welcome-file-list> <servlet-name> dispatcher </servlet-class> org.springframework.web.weblet.dispatreterervlete </servlete </servlete <param-name> contextconfiglocation </param-name> <param-value> classpath:dispatcher-servlet.xml </param-value> </init-param> </servlet> <servlet-name> dispatcher </servet-name> <url-pattern>/</url-perthn> </vervlet-mapping> </web-app>
Dispatcher-servlet.xml:
<?xml version = "1.0" encoding = "utf-8"?> <beans xmlns:xsi = "http://www.w3.org/2001/xmlschema-instance" xmlns = "http://www.springframework.org/schema/beans" xmlns:Context = "http://www.springframework.org/schema/context" xmlns:mvc = "http://www.springframework.org/schema/mvc" " xsi:schemalocation = "http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd http://www.springframework.org.org/scheand http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd "> <mvc:default-servletedler/> <context:com.zyq.springmvc.controller" expression = "org.springframework.stertype.service"/> </context:component-scan> <context:annotation-config/> <mvc:annotation-driven> <mvc:messagnverters> <bean> <プロパティ名= "supportedmediatypes"> <値> <値> charset = utf-8 </値> <value> text/html; charset = utf-8 </value> </list> </property> </bean> <bean> <property name = "supportedmediatypes"> <list> <balue> application/json; charset = utf-8 </value> <value> application/x-www-form-urlencoded; charset = utf-8 </value> </list> </property> </bean> </mvc:message-converters> </mvc:annotation-driven> </beans>
ApplicationContext.xmlもありますが、私はそれに何も書かないので、私はそれを与えません!
新しいインデックスを作成します。これは、各インターフェイスの返品値が正常かどうかをテストするためのメインインターフェイスとして使用されます。コードもここに記載されています:
<%@ page Language = "Java" contentType = "text/html; charset = iso-8859-1" pageencoding = "iso-8859-1" "http://www.w3.org/tr/html4/loose.dtd"> <html> <head> action = "/returnjsondemo/first"> <入力タイプ= "first"/> </form> <form action = "/returnjsondemo/second"> <入力タイプ= "" value = "second"/> </form> <form Action = "/returnjsondemo/surd"> <入力タイプ= "" 3番目のフォーム/フォーム> < <入力型= "submit" value = "fourth"/> </form> </body> </html>
この時点で、すべての構成が基本的に完了し、コントローラーが宣言されます。特定のコードは比較的単純で、基本的に固定形式です!
MainController.java
パッケージcom.zyq.springmvc.controller; import java.text.simpledateformat; import java.util.arraylist; Import java.util.hashmap; import java.util.list; import java.util.map; import org.springframework.steretype.controller; org.springframework.web.bind.annotation.requestmapping; Import org.springframework.web.bind.annotation.responsebody; Import com.zyq.springmvc.bean.commonbean; Import com.zyq.springmvc.bean.sonbunvean; @RequestMapping( "/first")@responsebody public commonbean getFirst(){commonbean bean = new commonbean(); bean.setResultCode( "success"); bean.settimestamp(new simpledateFormat( "yyyy/mm/dd hh:mm:ss")。format(system.currenttimemillis())); bean.setdata( "これは最初のメッセージです");豆を返します。 } @requestMapping( "/second")@responsebody public commonbean getSecond(){commonbean bean = new commonbean(); bean.setResultCode( "ss"); bean.settimestamp(new simpledateFormat( "yyyy/mm/dd hh:mm:ss")。format(system.currenttimemillis()));リスト<文字列> data = new ArrayList <>(); data.add( "Java"); data.add( "c"); data.add( "python"); data.add( "c ++"); bean.setdata(data);豆を返します。 } @RequestMapping( "/third")@responsebody public commonbean getthird(){commonbean bean = new commonbean(); bean.setResultCode( "success"); bean.settimestamp(new simpledateFormat( "yyyy/mm/dd hh:mm:ss")。format(system.currenttimemillis())); map <string、string> data = new Hashmap <>(); data.put( "first"、 "java"); data.put( "second"、 "python"); data.put( "third"、 "c ++"); data.put( "fourth"、 "c"); bean.setdata(data);豆を返します。 } @requestMapping( "/fourth")@responsebody public commonbean getFourth(){commonbean bean = new commonbean(); bean.setResultCode( "success"); bean.settimestamp(new simpledateFormat( "yyyy/mm/dd hh:mm:ss")。format(system.currenttimemillis())); Sonbean Sonbean = new Sonbean(); Sonbean.Setage(25); Sonbean.setname( "ハッカーの喜び"); Sonbean.setgender( "MALE"); bean.setdata(sonbean);豆を返します。 }}コードの実行効果は次のとおりです。
さまざまなブラウザには、インターフェイスのリクエスト操作が異なるようです。 Eclipse要求インターフェイスを使用する場合、JSONファイルがダウンロードされ、ファイルのコンテンツはJSON文字列です。
完全なプロジェクトを構成するときは、Springflamework JarパッケージとJacksonの関連JARパッケージを使用する必要があります。 Tomcat8.5私は実行するときにエラーをプロンプトし、コモンログJARパッケージを導入する必要があります。
JSON文字列を返すインターフェイスを宣言する場合は、@ResponseBodyアノテーションを使用してください。これにより、インターフェイスの返されたデータが応答中のボディ領域への戻りデータを書き込みます。
私がテストしていたとき、私は問題に遭遇しました。豆を返すとき、私はクラスパッケージのみを返すことができますが、クラスの継承またはインターフェイス継承は返品できません。例えば:
内部に子育てを含むParentBeanを返しても大丈夫です!
インターフェイスが定義されている場合、返された親クラスが実際に返されたサブクラスであり、この時点でエラーが報告され、サブクラスを親クラスに変換できない場合、文字列オブジェクトをオブジェクトオブジェクトに変換できないことを意味します。この点で、サブクラスの属性を見つけることができない親クラスに基づいている必要があります。したがって、フレームワーク内のインターフェイス内の豆を宣言することは許可されておらず、豆のサブクラスが返されます(これらの理由は単なる推測であり、特定の理由はフレームワークのコードの分析も必要です)!
わかりました、それはJSON文字列を返すことです!デモのソースコードを添付してください。必要なJARパッケージも内部にあります。必要に応じて自分でダウンロードできます。
ソースコードのダウンロード
springMVCを使用してJSON文字列を返す上記の例は、私があなたと共有するすべてのコンテンツです。参照を提供できることを願っています。wulin.comをもっとサポートできることを願っています。