Spring Converter Getting started string into enumerations
Source: Internet
Author: User
Converter is a special feature introduced in Spring3, which is actually a converter that converts a type to another type. Especially in Web projects, you can check the input parameters of the interface, and then convert the type of the input parameters of the front end to the type that the backend can use. such as often used to do type conversion, string to empty, date format, and so on. Type conversions are performed before Spring3 using PropertyEditor, and the PropertyEditor Setastext () method enables a string to move to a specific type. But one of its biggest flaws is that it only supports strings to other types. The three types of transformation interfaces in Spring are: Converter interface: Simple to use, but not very flexible; Converterfactory interface: Use more complex, a little more flexible; Genericconverter interface: Most complex and flexible to use;
Because enumeration types are common in development, the front-end cannot pass directly to an enumeration type in Java. The blog will use converter to convert a string from the front end into an enumeration for ease of use.
First look at the converter interface Spring Converter interface is as follows:
Package org.springframework.core.convert.converter;
Public interface Converter<s, t> {T convert (S var1);}
That is, you can convert the S type to type T and support generics.
First, create a Simple object class person
One of the fields sex Genderenum uses enumerations. public class Person {
private String name; Private Genderenum type;
Public person () {}
Public person (genderenum type, String name) {this.type = type; THIS.name = name; }
Public Genderenum GetType () {return type; }
public void SetType (Genderenum type) {this.type = type; }
Public String GetName () {return name; }
public void SetName (String name) {this.name = name; }
@Override public String toString () {return "person{" + "type=" + Type + ", NA Me= ' "+ name + ' \ ' + '} '; } }
The enumerated classes that need to be transformed are two fields, male, female, respectively. The demand now is that the front end is passed in "male", "female", and the background can be converted to the Male,female in the enumeration. public enum Genderenum {male ("male"), female ("female");
Private final String value;
Genderenum (String v) {this.value = v; }
Public String toString () {return this.value; }
public static genderenum get (int v) {String str = string.valueof (v); return get (str); }
public static genderenum get (String str) {for (Genderenum e:values ()) {if (e.tostring (). Equals (str ) {return e; } return null; } }
Convert Conversion class
The converter interface is implemented in this class, and the Convert method is overridden. Final class Stringtogenderenumconverter implements Converter<string, genderenum> {
@Override public Genderenum Convert (string source) {String value = Source.trim (); if ("". Equals (value)) {return null; Return Genderenum.get (Integer.parseint (source)); } }
Spring-mvc.xml configuration in the spring configuration file, configure Conversionservice.
<mvc:annotation-driven conversion-service= "Conversionservice"/> <bean id= "ConversionService" Org.springframework.context.support.ConversionServiceFactoryBean "<property name=" converters "<set> <bean class= "Com.chenyufeng.springmvc.common.converter.StringToGenderEnumConverter"/> </set> ; </property> </bean>
Controller in the Controller interface, for enumerated fields, the front end can be passed directly into the string type "male", "female", and converter can be converted automatically. @Api (value = "converter", Description = "Auto Convert", produces = Mediatype.application_json_value) @Controller @RequestMapping ("/converter") public class Convertercontroller {
@Autowired private Converterservice Converterservice;
@RequestMapping (value = "/saveperson", method = Requestmethod.get) @ResponseBody The public String Saveperson ( @ApiParam (value = "type") @RequestParam genderenum type, @ApiParam (value = "ID") @RequestParam String ID) {
person who = new person (type, id); return Converterservice.saveperson (person); } }
The above cases are uploaded to: https://github.com/chenyufeng1991/StartSpringMVC_Modules.
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.