yanbasic 发布的文章

Spring MVC --BIG topic

Annotations:

@Controller

@RequestMapping

@ResponseBody

@RequestBody

@PathVaribale

@RestController

@EnableWebMvc

@ControllerAdvice

@ExceptionHandler

@InitBinder

@ModelAttribute


import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;


@Controller // 1
@RequestMapping("/anno") //2
public class JsonAnnoController {

    @RequestMapping(produces = "text/plain;charset=UTF-8")  // 3
    public @ResponseBody String index(HttpServletRequest request) { // 4
        return "url:" + request.getRequestURL() + " can access";
    }

    @RequestMapping(value = "/pathvar/{str}", produces = "text/plain;charset=UTF-8")// 5
    public @ResponseBody String demoPathVar(@PathVariable String str, //3
            HttpServletRequest request) {
        return "url:" + request.getRequestURL() + " can access,str: " + str;
    }

    @RequestMapping(value = "/requestParam", produces = "text/plain;charset=UTF-8") //6
    public @ResponseBody String passRequestParam(Long id,
            HttpServletRequest request) {
        
        return "url:" + request.getRequestURL() + " can access,id: " + id;

    }

    @RequestMapping(value = "/obj", produces = "application/json;charset=UTF-8")//7
    @ResponseBody //8
    public String passObj(DemoObj obj, HttpServletRequest request) {
        
         return "url:" + request.getRequestURL() 
                    + " can access, obj id: " + obj.getId()+" obj name:" + obj.getName();

    }

    @RequestMapping(value = { "/name1", "/name2" }, produces = "text/plain;charset=UTF-8")//9
    public @ResponseBody String remove(HttpServletRequest request) {
        
        return "url:" + request.getRequestURL() + " can access";
    }

}
<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.wedding0326</groupId>
    <artifactId>demo_springmvc</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>war</packaging>

    <properties>
        <!-- Generic properties -->
        <java.version>1.7</java.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <!-- Web -->
        <jsp.version>2.2</jsp.version>
        <jstl.version>1.2</jstl.version>
        <servlet.version>3.1.0</servlet.version>
        <!-- Spring -->
        <spring-framework.version>4.1.5.RELEASE</spring-framework.version>
        <!-- Logging -->
        <logback.version>1.0.13</logback.version>
        <slf4j.version>1.7.5</slf4j.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>javax</groupId>
            <artifactId>javaee-web-api</artifactId>
            <version>7.0</version>
            <scope>provided</scope>
        </dependency>

        <!-- Spring MVC -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>${spring-framework.version}</version>
        </dependency>

        <!-- web deps -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
            <version>${jstl.version}</version>
        </dependency>
        
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>${servlet.version}</version>
            <scope>provided</scope>
        </dependency>
        
        <dependency>
            <groupId>javax.servlet.jsp</groupId>
            <artifactId>jsp-api</artifactId>
            <version>${jsp.version}</version>
            <scope>provided</scope>
        </dependency>

        <!-- Spring and Transactions -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-tx</artifactId>
            <version>${spring-framework.version}</version>
        </dependency>

        <!-- SLF4J LogBack as log -->
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>${slf4j.version}</version>
        </dependency>
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.16</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>jcl-over-slf4j</artifactId>
            <version>${slf4j.version}</version>
        </dependency>
        <dependency>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-classic</artifactId>
            <version>${logback.version}</version>
        </dependency>
        <dependency>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-core</artifactId>
            <version>${logback.version}</version>
        </dependency>
        <dependency>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-access</artifactId>
            <version>${logback.version}</version>
        </dependency>

        <!--json xml -->
        <dependency>
            <groupId>com.fasterxml.jackson.dataformat</groupId>
            <artifactId>jackson-dataformat-xml</artifactId>
            <version>2.5.3</version>
        </dependency>

        <!-- file upload -->
        <dependency>
            <groupId>commons-fileupload</groupId>
            <artifactId>commons-fileupload</artifactId>
            <version>1.3.1</version>
        </dependency>
        <!-- IO -->
        <dependency>
            <groupId>commons-io</groupId>
            <artifactId>commons-io</artifactId>
            <version>2.3</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>${spring-framework.version}</version>
            <scope>test</scope>
        </dependency>
        
    
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.11</version>
            <scope>test</scope>
        </dependency>

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.3.2</version>
                <configuration>
                    <source>${java.version}</source>
                    <target>${java.version}</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <version>2.3</version>
                <configuration>
                    <failOnMissingWebXml>false</failOnMissingWebXml>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>
  • Server Send Event:

Spring EL-Spring

import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
import org.springframework.core.env.Environment;
import org.springframework.core.io.Resource;

/**
 * Created by yyi on 8/8/2016.
 */
@Configuration
@ComponentScan("ch2.el")
@PropertySource("classpath:ch2/el/test.properties")
public class ElConfig {
    @Value("I Love You!") //1
    private String normal;

    @Value("#{systemProperties['os.name']}") //2
    private String osName;

    @Value("#{ T(java.lang.Math).random() * 100.0 }") //3
    private double randomNumber;

    @Value("#{demoService.another}") //4
    private Resource fromAnother;

    @Value("classpath:/ch2/el/test.txt") //5
    private Resource testFile;

    @Value("http://www.baidu.com") //6
    private Resource testUrl;

    @Value("${company.team}") //7
    private String bookName;

    @Autowired
    private Environment environment; //7

    @Bean //7
    public static PropertySourcesPlaceholderConfigurer propertyConfigure() {
        return new PropertySourcesPlaceholderConfigurer();
    }
    public void outputResource() {
        try {
            System.out.println(normal);
            System.out.println(osName);
            System.out.println(randomNumber);
            System.out.println(fromAnother);
            System.out.println(IOUtils.toString(fromAnother.getInputStream()));

            System.out.println(IOUtils.toString(testFile.getInputStream()));
            System.out.println(IOUtils.toString(testUrl.getInputStream()));
            System.out.println(bookName);
            System.out.println(environment.getProperty("book.author"));
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

}

Upgrade pax-url-aether version to 2.4.7 for karaf 4.0.3

Upgrade pax-url-aether version to 2.4.7 for karaf 4.0.3

talend-esb/pom.xml

  • add version variable
    <pax-url-aether.version>2.4.7</pax-url-aether.version>
  • add dependency
<!-- workaround for pax-url-aether bundle (TESB-17522) -->
<dependency>
    <groupId>org.ops4j.pax.url</groupId>
    <artifactId>pax-url-aether</artifactId>
    <version>${pax-url-aether.version}</version>
    <type>jar</type>
</dependency>
  • add copy jar in generate-resources phase
<artifactItem>
    <groupId>org.ops4j.pax.url</groupId>
    <artifactId>pax-url-aether</artifactId>
    <version>${pax-url-aether.version}</version>
    <type>jar</type>
    <outputDirectory>target/dependencies</outputDirectory>
    <destFileName>pax-url-aether-${pax-url-aether.version}.jar</destFileName>
</artifactItem>
  • add replace version in generate-resources phase for framework-${karaf.version}-features.xml file
<!-- patch for Karaf org.apache.karaf.features.core bundle -->
<replace file="target/dependencies/framework-${karaf.version}-features.xml">
    <replacefilter
        token="org.apache.karaf.features/org.apache.karaf.features.core/${karaf.version}&lt;"
        value="org.apache.karaf.features/org.apache.karaf.features.core/${karaf.version}/jar/TESB&lt;"/>
    <!-- patch for upgrading pax-url-aether to 2.4.7 (TESB-17522) -->
    <replacefilter
        token="mvn:org.ops4j.pax.url/pax-url-aether/2.4.3"
        value="mvn:org.ops4j.pax.url/pax-url-aether/${pax-url-aether.version}"/>
</replace>
  • add repo dependencies in pom
<repository>
    <id>ops4j.sonatype.releases</id>
    <name>OPS4J releases repository</name>
    <url>https://oss.sonatype.org/content/repositories/ops4j-releases/</url>
    <releases>
        <enabled>true</enabled>
    </releases>
    <snapshots>
        <enabled>false</enabled>
    </snapshots>
</repository>

talend-esb/src/main/descriptors/unix-bin.xml and talend-esb/src/main/descriptors/win-bin.xml

  • exclude startup.properties file <exclude>etc/startup.properties</exclude>
  • copy jar into local repo
<!-- pax-url-aether library patch (TESB-17522) -->
<file>
    <source>${basedir}/target/dependencies/pax-url-aether-${pax-url-aether.version}.jar</source>
    <outputDirectory>/container/system/org/ops4j/pax/url/pax-url-aether/${pax-url-aether.version}/</outputDirectory>
    <fileMode>0644</fileMode>
</file>    

talend-esb/src/main/filtered-resources/etc/org.ops4j.pax.url.mvn.cfg

  • add always param in the cfg file for maven
    org.ops4j.pax.url.mvn.globalUpdatePolicy=always

talend-esb/src/main/filtered-resources/etc/startup.properties

  • add mvn\:org.ops4j.pax.url/pax-url-aether/2.4.7 = 5 in this file

https://github.com/Talend/tesb-rt-se/commit/a72e35789b1d3a33d8b97b641d25f4df8b0ab8ea