Arquivo da tag: Spring

Spring Profiles

Spring Profiles provide a way to segregate parts of your application configuration and make it only available in certain environments. Any @Component or @Configuration can be marked with @Profile to limit when it is loaded:

@Configuration
@Profile("production")
public class ProductionConfiguration {

    // ...

}

In the normal Spring way, you can use a spring.profiles.active Environment property to specify which profiles are active. You can specify the property in any of the usual ways, for example you could include it in your application.properties:

spring.profiles.active=dev,hsqldb

or specify on the command line using the switch --spring.profiles.active=dev,hsqldb.

https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-profiles.html

Spring JDBC Create Temp Table

package spring.test;
 
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
 
import javax.sql.DataSource;
 
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.dao.DataAccessResourceFailureException;
import org.springframework.jdbc.core.BatchPreparedStatementSetter;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.datasource.SingleConnectionDataSource;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
 
@Component
public class DbTestDao {
 
  JdbcTemplate jdbcTemplate;
 
  @Autowired
  public void setDataSource(DataSource dataSource) {
    jdbcTemplate = new JdbcTemplate(dataSource);
  }
 
  @Transactional(readOnly = true)
  public List getCustomers() {
    return jdbcTemplate.query("select id, name, customer_since from customers", 
        new CustomerMapper());
  }
 
  // NOT Transactional - using Sybase temp tables
  public List getSomeCustomers(final List<Long> ids) {
    SingleConnectionDataSource scds = null;
    try {
      scds = new SingleConnectionDataSource(jdbcTemplate.getDataSource().getConnection(), true);
    } catch (SQLException e) {
      throw new DataAccessResourceFailureException("Unable to create single connnection ds", e);
    }
 
    List results;
    try {
      JdbcTemplate scdsJdbcTemplate = new JdbcTemplate(scds);
 
      scdsJdbcTemplate.execute("create table #my_ids (id int)");
 
      scdsJdbcTemplate.batchUpdate("insert into #my_ids(id) values(?)", 
          new BatchPreparedStatementSetter() {
 
            public int getBatchSize() {
              return ids.size();
            }
 
            public void setValues(PreparedStatement ps, int i)
                throws SQLException {
              ps.setLong(1, ids.get(i));
            }
          });
      results = scdsJdbcTemplate.query(
          "select id, name, customer_since from customers where id in (select id from #my_ids)", 
          new CustomerMapper()); 
      scdsJdbcTemplate.execute("drop table #my_ids");
    } 
    finally {
      scds.destroy();
    }
    return results;
  }
 
  public class CustomerMapper implements RowMapper {
    public Object mapRow(ResultSet rs, int row) throws SQLException {
      Customer c = new Customer();
      c.setId(rs.getLong(1));
      c.setName(rs.getString(2));
      c.setCustomerSince(rs.getDate(3));
      return c;
    } 
  } 
}

Referência

Spring REST – Download de .pdf

@RequestMapping(value="/getpdf", method=RequestMethod.POST)
public ResponseEntity<byte[]> getPDF(@RequestBody String json) {
    // convert JSON to Employee 
    Employee emp = convertSomehow(json);
 
    // generate the file
    PdfUtil.showHelp(emp);
 
    // retrieve contents of "C:/tmp/report.pdf" that were written in showHelp
    byte[] contents = (...);
 
    HttpHeaders headers = new HttpHeaders();
    headers.setContentType(MediaType.parseMediaType("application/pdf"));
    String filename = "output.pdf";
    headers.setContentDispositionFormData(filename, filename);
    headers.setCacheControl("must-revalidate, post-check=0, pre-check=0");
    ResponseEntity<byte[]> response = new ResponseEntity<byte[]>(contents, headers, HttpStatus.OK);
    return response;
}