Example code of spring message converter
//domain
package com.crazy.goods.tools;/** * 0755-351512 * @author Administrator * */public class Phone {private String qno;private String number;public String getQno() {return qno; }public void setQno(String qno) {this.qno = qno; }public String getNumber() {return number; }public void setNumber(String number) {this.number = number; } }
//Message Converter To implement an abstract class AbstractHttpMessageConverter
package com.crazy.goods.tools;import java.io.BufferedReader;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;import org.springframework.http.HttpInputMessage;import org.springframework.http.HttpOutputMessage;import org.springframework.http.converter.AbstractHttpMessageConverter;import org.springframework.http.converter.HttpMessageNotReadableException;import org.springframework.http.converter.HttpMessageNotWritableException;public class MyMessageConvertor extends AbstractHttpMessageConverter<Phone> {/** * 将请求头数据转换成Phone */ @Overrideprotected Phone readInternal(Class<? extends Phone> arg0, HttpInputMessage msg) throws IOException, HttpMessageNotReadableException {//参数必须使用post提交必须在body中InputStream is=msg.getBody(); BufferedReader br=new BufferedReader(new InputStreamReader(is)); String param=br.readLine(); String phone=param.split("=")[1]; Phone phoneObj=new Phone(); phoneObj.setQno(phone.split("-")[0]); phoneObj.setNumber(phone.split("-")[1]);return phoneObj; }/** * 当前的转换器支持转换的类 */@Overrideprotected boolean supports(Class<?> arg0) {if(arg0==Phone.class){return true; }return false; }/** * 用于将返回的对象转换成字符串显示在网页 */@Overrideprotected void writeInternal(Phone phone, HttpOutputMessage arg1)throws IOException, HttpMessageNotWritableException { String p=phone.getQno()+"-"+phone.getNumber(); arg1.getBody().write(p.getBytes("UTF-8")); } }
//springmvc.xml To configure bean: message converter, only the post submission method will be intercepted by the converter
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"xmlns:tx="http://www.springframework.org/schema/tx"xmlns:aop="http://www.springframework.org/schema/aop"xmlns:mvc="http://www.springframework.org/schema/mvc"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/context http://www.springframework.org/schema/tx http://www.springframework.org/schema/aop http://www.springframework.org/schema/mvc "> <!--springmvc只能扫描控制层 --> <context:component-scan base-package="com.crazy.goods"> <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Service"/> <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Repository"/> </context:component-scan> <!--消息转换器 必须使用post提交 --> <mvc:annotation-driven> <mvc:message-converters> <bean class="com.crazy.goods.tools.MyMessageConvertor"> <property name="supportedMediaTypes"> <list> <value>text/html;charset=UTF-8</value> <value>application/x-www-form-urlencoded</value> </list> </property> </bean> </mvc:message-converters> </mvc:annotation-driven> </beans>
servlet test
<br>
package com.crazy.goods.servlet;
<br>
import java.io.IOException;
<br>
import javax.servlet.ServletException;<br>import javax.servlet.annotation.WebServlet;<br>import javax.servlet.http. HttpServlet;<br>import javax.servlet.http.HttpServletRequest;<br>import javax.servlet.http.HttpServletResponse;
<br>
import org.springframework.stereotype.Controller;<br>import org.springframework. web.bind.annotation.PathVariable;<br>import org.springframework.web.bind.annotation.RequestBody;<br>import org.springframework.web.bind.annotation.RequestMapping;<br>import org.springframework.web. bind.annotation.RequestMethod;<br>import org.springframework.web.bind.annotation.ResponseBody;
<br>
import com.crazy.goods.tools.Phone;
<br>
/** <br> * @author Administrator<br> * Creation time: July 1, 2017 3:11:27 pm<br>*/<br>@Controller<br>public class ReservePageServelt {
<br>
// /**<br>// * forward: forward <br>// * redirect: redirect <br>// * @param req<br>// * @param resp<br>// * @return<br>// * @throws ServletException<br>// * @throws IOException<br>//*/<br>// @RequestMapping(value="/add",method ={RequestMethod.GET})<br>// public String doGet(HttpServletRequest req, HttpServletResponse resp/*,@PathVariable("testid") String testid*/) throws ServletException, IOException {<br>// req.getRequestDispatcher( "/reversegood.jsp").forward(req, resp);<br>// return "/reversegood.jsp";<br>// resp.getWriter().print(testid);<br>// } <br><br><br> //Message converter idea, <br><br> //The principle is to convert the request body or request header data into the parameters of the action method, and at the same time convert the content of the method's return value For the response header <br> //When the url path is accessed, the @RequestBody annotation is used. This annotation indicates that this class is to be processed by the message converter. The message converter will be read in the springmvcxml file, and then the supports method will be entered. <br> //Determine whether this class is supported by the specified converter. If it is supported, call the readInternal method, cut it, and then pass the value to the object. After processing is completed into the object, writeInternal will be called to convert into a response header<br> @RequestMapping(value="/add")<br> @ResponseBody<br> public Phone messageConvertor( @RequestBody Phone phone,HttpServletResponse response) {<br> System.out.println(phone.getQno()+phone. getNumber());<br> return phone;<br><br> }<br><br>}
<br>
Summary: The principle of the message converter is, Customize to convert the request body data into formal parameters (objects), and then convert the method's return value content into response headers
Steps:
When the url path is accessed, you will see the use The @RequestBody annotation indicates that this class is to be processed by the message converter. The message converter will be read in the springmvcxml file, and then the supports method <br> will be entered to determine whether this class is supported by the specified converter. If so, Call the readInternal method, cut, and then pass the value to the object.
After processing is completed into an object, writeInternal will be called to convert into a response header
The above is the detailed content of Example code of spring message converter. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



The message has been sent but rejected by the other party. This means that the sent information has been successfully sent from the device, but for some reason, the other party did not receive the message. More specifically, this is usually because the other party has set certain permissions or taken certain actions, which prevents your information from being received normally.

How to Use Swipe to Reply in iMessages on iPhone Note: The Swipe to Reply feature only works with iMessage conversations in iOS 17, not regular SMS conversations in the Messages app. Open the Messages app on your iPhone. Then, head to the iMessage conversation and simply swipe right on the iMessage you want to reply to. Once this is done, the selected iMessage will be in focus while all other messages will be blurred in the background. You'll see a text box for typing a reply and a "+" icon for accessing iMessage apps like Check-ins, Places, Stickers, Photos, and more. Just enter your message,

In iOS17, Apple has added several new features to its Messages app to make communicating with other Apple users more creative and fun. One of the features is the ability to use emojis as stickers. Stickers have been around in the Messages app for years, but so far, they haven't changed much. This is because in iOS17, Apple treats all standard emojis as stickers, allowing them to be used in the same way as actual stickers. This essentially means you're no longer limited to inserting them into conversations. Now you can also drag them anywhere on the message bubble. You can even stack them on top of each other to create little emoji scenes. The following steps show you how it works in iOS17

1. Being added to the blacklist: The message has been sent but rejected by the other party. Generally, you have been blacklisted. At this time, you will not be able to send messages to the other party, and the other party will not be able to receive your messages. 2. Network problems: If the recipient's network condition is poor or there is a network failure, the message may not be successfully received. At this point, you can try to wait for the network to return to normal before sending the message again. 3. The other party has set up Do Not Disturb: If the recipient has set up Do Not Disturb in WeChat, the sender’s messages will not be reminded or displayed within a certain period of time.

The native Messages app on iPhone lets you easily edit sent texts. This way, you can correct your mistakes, punctuation, and even autocorrect wrong phrases/words that may have been applied to your text. In this article, we will learn how to edit messages on iPhone. How to Edit Messages on iPhone Required: iPhone running iOS16 or later. You can only edit iMessage text on the Messages app, and then only within 15 minutes of sending the original text. Non-iMessage text is not supported, so they cannot be retrieved or edited. Launch the Messages app on your iPhone. In Messages, select the conversation from which you want to edit the message

Xiaomi 14Pro is a flagship model with excellent performance and configuration. It has achieved high sales since its official release. Many small functions of Xiaomi 14Pro will be ignored by everyone. For example, it can be set to light up the screen for messages. Although the function is small, , but it is very practical. Everyone will encounter various problems when using the mobile phone. So how to set up the Xiaomi 14Pro to light up the screen for messages? How to set up Xiaomi Mi 14 Pro to light up the screen for messages? Step 1: Open your phone’s Settings app. Step 2: Swipe down until you find the "Lock screen and password" option and click to enter. Step 3: In the "Lock screen & passcode" menu, find and click the "Turn on screen for notifications" option. Step 4: On the "Turn on screen when receiving notifications" page, turn on the switch to enable

Today, Transformers are key modules in most advanced natural language processing (NLP) and computer vision (CV) architectures. However, the field of tabular data is still dominated by gradient boosted decision tree (GBDT) algorithms. So, there were attempts to bridge this gap. Among them, the first converter-based tabular data modeling paper is the paper "TabTransformer: Tabular Data Modeling Using Context Embedding" published by Huang et al. in 2020. This article aims to provide a basic presentation of the content of the paper, while also delving into the implementation details of the TabTransformer model and showing you how to specifically use Ta for our own data.

Vivox100s release date confirmed! The latest news has been previewed. Recently, there has been a craze about Vivox100s in the technology world. This highly anticipated product has finally confirmed its release date, making many consumers and technology enthusiasts excited. It is reported that Vivox100s will be officially released at the end of this month. What surprises will it bring? What are the highlights of the much-anticipated new product? Let us uncover this technological mystery together. Vivox100s, as the latest masterpiece of the Vivox series, has attracted much attention since its exposure.
