Hola como están? estoy armando un pequeño proyecto api rest ful y al usar una sola tabla sin relaciones funciona bien, ahora si le agrego mas tablas y relaciones me falla la obtencion del json. Consiguracion
@Configuration
@ComponentScan(basePackages = "ar.com.clothes")
public class AppConfig {
}
Hibernate
@Configuration
@EnableTransactionManagement
@ComponentScan({ "ar.com.clothes.configuration" })
@PropertySource(value = { "classpath:application.properties" })
public class HibernateConfiguration {
@Autowired
private Environment environment;
@Bean
public LocalSessionFactoryBean sessionFactory() {
LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean();
sessionFactory.setDataSource(dataSource());
sessionFactory.setPackagesToScan(new String[] { "ar.com.clothes.model" });
sessionFactory.setHibernateProperties(hibernateProperties());
return sessionFactory;
}
@Bean
public DataSource dataSource() {
DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setDriverClassName(environment.getRequiredProperty("jdbc.driverClassName"));
dataSource.setUrl(environment.getRequiredProperty("jdbc.url"));
dataSource.setUsername(environment.getRequiredProperty("jdbc.username"));
dataSource.setPassword(environment.getRequiredProperty("jdbc.password"));
return dataSource;
}
private Properties hibernateProperties() {
Properties properties = new Properties();
properties.put("hibernate.dialect", environment.getRequiredProperty("hibernate.dialect"));
properties.put("hibernate.show_sql", environment.getRequiredProperty("hibernate.show_sql"));
properties.put("hibernate.format_sql", environment.getRequiredProperty("hibernate.format_sql"));
return properties;
}
@Bean
@Autowired
public HibernateTransactionManager transactionManager(SessionFactory s) {
HibernateTransactionManager txManager = new HibernateTransactionManager();
txManager.setSessionFactory(s);
return txManager;
}
}
Mi pom
<properties>
<spring.version>4.2.1.RELEASE</spring.version>
<security.version>4.0.3.RELEASE</security.version>
<jdk.version>1.8</jdk.version>
<hibernate.version>4.3.5.Final</hibernate.version>
<org.aspectj-version>1.7.4</org.aspectj-version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- <dependency> -->
<!-- <groupId>com.fasterxml.jackson.core</groupId> -->
<!-- <artifactId>jackson-databind</artifactId> -->
<!-- <version>2.4.1</version> -->
<!-- </dependency> -->
<!-- Hibernate -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>${hibernate.version}</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>${hibernate.version}</version>
</dependency>
<!-- Apache Commons DBCP -->
<dependency>
<groupId>commons-dbcp</groupId>
<artifactId>commons-dbcp</artifactId>
<version>1.4</version>
</dependency>
<!-- Spring ORM -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- AspectJ -->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>${org.aspectj-version}</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.6</version>
</dependency>
<!-- https://mvnrepository.com/artifact/log4j/log4j -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
</dependencies>
<build>
<finalName>ClothesApi</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
</plugin>
</plugins>
</build>
Saque jackson por que aun no lo se usar. y aqui mi controller:
@RestController
public class UsuarioController {
@Autowired
UsuarioService usuarioService;
@RequestMapping(value = "/getAllUsuarios", method = RequestMethod.GET, headers = "Accept=application/json")
public List<Usuario> getAllUsuarios() {
List<Usuario> listUsuarios = usuarioService.findAllUsuarios();
return listUsuarios;
}
@RequestMapping(value = "/getUsuario/{id}", method = RequestMethod.GET, headers = "Accept=application/json")
public Usuario getUsuarioById(@PathVariable Integer id) {
return usuarioService.findById(id);
}
@RequestMapping(value = "/addUsuario", method = RequestMethod.POST, headers = "Accept=application/json")
public void addUsuario(@RequestBody Usuario usuario) {
usuarioService.saveUsuario(usuario);
}
@RequestMapping(value = "/updateUsuario", method = RequestMethod.PUT, headers = "Accept=application/json")
public void updateUsuario(@RequestBody Usuario usuario) {
usuarioService.updateUsuario(usuario);
}
@RequestMapping(value = "/deleteUsuario/{id}", method = RequestMethod.DELETE, headers = "Accept=application/json")
public void deleteUsuario(@PathVariable("id") int usuarioId) {
usuarioService.deleteUsuarioById(usuarioId);
}
// lukas
@RequestMapping(path = "/listaUsuarios", method = { RequestMethod.GET }, produces = MediaType.APPLICATION_JSON_VALUE)
public @ResponseBody List<Usuario> listUsuario() {
return this.usuarioService.findAllUsuarios();
}
}
al ejecutar obtengo esto en el log.
2017-03-03 14:39:03 [DEBUG] [org.hibernate.internal.util.EntityPrinter.toString(EntityPrinter.java:114)] Listing entities:
2017-03-03 14:39:03 [DEBUG] [org.hibernate.internal.util.EntityPrinter.toString(EntityPrinter.java:121)] ar.com.clothes.model.Empresa{encargos=<uninitialized>, domicilio=Necochea 465, idEmpresa=1, clienteses=<uninitialized>, telefono=4226288, usuarios=[ar.com.clothes.model.Usuario#1], [email protected], nombreEmpresa=Nicole Boutique}
2017-03-03 14:39:03 [DEBUG] [org.hibernate.internal.util.EntityPrinter.toString(EntityPrinter.java:121)] ar.com.clothes.model.Usuario{password=e10adc3949ba59abbe56e057f20f883e, nombreApellido=Juan Manuel Lopez, idUsuario=1, nombreUsuario=jlopez, empresa=ar.com.clothes.model.Empresa#1}
2017-03-03 14:39:03 [DEBUG] [org.hibernate.internal.util.EntityPrinter.toString(EntityPrinter.java:121)] ar.com.clothes.model.Empresa{encargos=<uninitialized>, domicilio=Necochea 345, idEmpresa=2, clienteses=<uninitialized>, telefono=4236241, usuarios=[ar.com.clothes.model.Usuario#2], [email protected], nombreEmpresa=Jeniffer Cabrini}
2017-03-03 14:39:03 [DEBUG] [org.hibernate.internal.util.EntityPrinter.toString(EntityPrinter.java:121)] ar.com.clothes.model.Usuario{password=e10adc3949ba59abbe56e057f20f883e, nombreApellido=Maria Sonia, idUsuario=2, nombreUsuario=msonia, empresa=ar.com.clothes.model.Empresa#2}
2017-03-03 14:39:03 [DEBUG] [org.hibernate.internal.SessionImpl.disconnect(SessionImpl.java:468)] Disconnecting session
2017-03-03 14:39:03 [DEBUG] [org.hibernate.engine.jdbc.internal.LogicalConnectionImpl.releaseConnection(LogicalConnectionImpl.java:246)] Releasing JDBC connection
2017-03-03 14:39:03 [DEBUG] [org.hibernate.engine.jdbc.internal.LogicalConnectionImpl.releaseConnection(LogicalConnectionImpl.java:264)] Released JDBC connection
2017-03-03 14:39:03 [DEBUG] [org.springframework.transaction.support.AbstractPlatformTransactionManager.processCommit(AbstractPlatformTransactionManager.java:759)] Initiating transaction commit
2017-03-03 14:39:03 [DEBUG] [org.springframework.orm.hibernate4.HibernateTransactionManager.doCommit(HibernateTransactionManager.java:580)] Committing Hibernate transaction on Session [SessionImpl(PersistenceContext[entityKeys=[],collectionKeys=[]];ActionQueue[insertions=org.hibernate.engine.spi.ExecutableList@7c4387d4 updates=org.hibernate.engine.spi.ExecutableList@67777290 deletions=org.hibernate.engine.spi.ExecutableList@43880303 orphanRemovals=org.hibernate.engine.spi.ExecutableList@3b0fe53a collectionCreations=org.hibernate.engine.spi.ExecutableList@5b12a8a8 collectionRemovals=org.hibernate.engine.spi.ExecutableList@72da2a8 collectionUpdates=org.hibernate.engine.spi.ExecutableList@43e2955 collectionQueuedOps=org.hibernate.engine.spi.ExecutableList@40dc2add unresolvedInsertDependencies=UnresolvedEntityInsertActions[]])]
2017-03-03 14:39:03 [DEBUG] [org.hibernate.engine.transaction.spi.AbstractTransactionImpl.commit(AbstractTransactionImpl.java:175)] committing
2017-03-03 14:39:03 [DEBUG] [org.hibernate.engine.transaction.internal.jdbc.JdbcTransaction.doCommit(JdbcTransaction.java:113)] committed JDBC Connection
2017-03-03 14:39:03 [DEBUG] [org.hibernate.engine.transaction.internal.jdbc.JdbcTransaction.releaseManagedConnection(JdbcTransaction.java:126)] re-enabling autocommit
2017-03-03 14:39:03 [DEBUG] [org.springframework.orm.hibernate4.HibernateTransactionManager.doCleanupAfterCompletion(HibernateTransactionManager.java:669)] Closing Hibernate Session [SessionImpl(PersistenceContext[entityKeys=[],collectionKeys=[]];ActionQueue[insertions=org.hibernate.engine.spi.ExecutableList@7c4387d4 updates=org.hibernate.engine.spi.ExecutableList@67777290 deletions=org.hibernate.engine.spi.ExecutableList@43880303 orphanRemovals=org.hibernate.engine.spi.ExecutableList@3b0fe53a collectionCreations=org.hibernate.engine.spi.ExecutableList@5b12a8a8 collectionRemovals=org.hibernate.engine.spi.ExecutableList@72da2a8 collectionUpdates=org.hibernate.engine.spi.ExecutableList@43e2955 collectionQueuedOps=org.hibernate.engine.spi.ExecutableList@40dc2add unresolvedInsertDependencies=UnresolvedEntityInsertActions[]])] after transaction
2017-03-03 14:39:03 [DEBUG] [org.hibernate.engine.jdbc.internal.LogicalConnectionImpl.releaseConnection(LogicalConnectionImpl.java:246)] Releasing JDBC connection
2017-03-03 14:39:03 [DEBUG] [org.hibernate.engine.jdbc.internal.LogicalConnectionImpl.releaseConnection(LogicalConnectionImpl.java:264)] Released JDBC connection
2017-03-03 14:39:03 [DEBUG] [org.springframework.web.servlet.handler.AbstractHandlerExceptionResolver.resolveException(AbstractHandlerExceptionResolver.java:131)] Resolving exception from handler [public java.util.List<ar.com.clothes.model.Usuario> ar.com.clothes.web.UsuarioController.listUsuario()]: org.springframework.web.HttpMediaTypeNotAcceptableException: Could not find acceptable representation
2017-03-03 14:39:03 [DEBUG] [org.springframework.web.servlet.handler.AbstractHandlerExceptionResolver.resolveException(AbstractHandlerExceptionResolver.java:131)] Resolving exception from handler [public java.util.List<ar.com.clothes.model.Usuario> ar.com.clothes.web.UsuarioController.listUsuario()]: org.springframework.web.HttpMediaTypeNotAcceptableException: Could not find acceptable representation
2017-03-03 14:39:03 [DEBUG] [org.springframework.web.servlet.handler.AbstractHandlerExceptionResolver.resolveException(AbstractHandlerExceptionResolver.java:131)] Resolving exception from handler [public java.util.List<ar.com.clothes.model.Usuario> ar.com.clothes.web.UsuarioController.listUsuario()]: org.springframework.web.HttpMediaTypeNotAcceptableException: Could not find acceptable representation
2017-03-03 14:39:03 [WARN ] [org.springframework.web.servlet.handler.AbstractHandlerExceptionResolver.logException(AbstractHandlerExceptionResolver.java:186)] Handler execution resulted in exception: Could not find acceptable representation
2017-03-03 14:39:03 [DEBUG] [org.springframework.web.servlet.DispatcherServlet.processDispatchResult(DispatcherServlet.java:1034)] Null ModelAndView returned to DispatcherServlet with name 'mvc-dispatcher': assuming HandlerAdapter completed request handling
2017-03-03 14:39:03 [DEBUG] [org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1000)] Successfully completed request
Espero me puedan dar una mano, muchas gracias.