spring-rest

Spring rest template readTimeOut

こ雲淡風輕ζ 提交于 2019-12-08 19:15:49
问题 I'm trying to understand the readTimeout available on restTemplate, what is it exactly ? Is it the the total amount of time the request can take before we get the timeout exception ? 回答1: You can define a read timeout on a RestTemplate as follows: HttpComponentsClientHttpRequestFactory clientRequestFactory = new HttpComponentsClientHttpRequestFactory(); // set the read timeot, this value is in miliseconds clientRequestFactory.setReadTimeout(500); RestTemplate restTemplate = new RestTemplate

SpingREST: Could not open JPA EntityManager for transaction; nested exception is org.hiberna

夙愿已清 提交于 2019-12-08 11:46:08
问题 When I start Postman to see my SpringREST service running I get following error: { "timestamp": 1506965117328, "status": 500, "error": "Internal Server Error", "exception": "org.springframework.transaction.CannotCreateTransactionException", "message": "Could not open JPA EntityManager for transaction; nested exception is org.hibernate.exception.GenericJDBCException: Unable to acquire JDBC Connection", "path": "/api/matchs" } I don't know where the error is, here are the classes of the project

Spring 4 - Custom SecurityExpression with Service

僤鯓⒐⒋嵵緔 提交于 2019-12-08 05:58:08
问题 In one of my controllers, I have the need to secure a method with more granular means than a role. I've got a MethodSecurityExpressionHandler created, but I can't figure out how to access one of my @Services in it. @Configuration @EnableGlobalMethodSecurity(prePostEnabled = true, securedEnabled = true, proxyTargetClass = false) public class CustomMethodSecurityConfiguration extends GlobalMethodSecurityConfiguration { @Autowired ApplicationContext applicationContext; @Override protected

How to deal with @OneToMany relation in REST

天大地大妈咪最大 提交于 2019-12-08 05:45:48
问题 I'm designing a small REST application that allows to perform some basic operations. So far so good, i have following @Entity called Client that needs to be persisted along side with @Entity called Loan : Client: @Entity @Table(name = "clients") public class Client{ @Id @GeneratedValue(strategy = GenerationType.AUTO) @Column(name = "CLIENT_ID") private Long id; private String name; private String surname; private String email; private String phone; @OneToMany(fetch = FetchType.LAZY, mappedBy

Sending Custom JSON from Java class in case of auth failure of REST API on spring boot

早过忘川 提交于 2019-12-08 03:30:52
问题 @Configuration @EnableWebSecurity @EnableGlobalMethodSecurity(prePostEnabled = true) public class SecurityConfig extends WebSecurityConfigurerAdapter{ @Autowired private RESTAuthenticationEntryPoint authenticationEntryPoint; @Autowired private RESTAuthenticationFailureHandler authenticationFailureHandler; @Autowired private RESTAuthenticationSuccessHandler authenticationSuccessHandler; @Autowired private UserDetailsService userDetailsService; @Override protected void configure

How to set the default media type for spring-data-rest?

纵饮孤独 提交于 2019-12-07 16:18:45
问题 From RepositoryRestConfiguration I can see that setting spring.data.rest.default-media-type=application/json could change the default media type served by @RepositoryRestResource . @SuppressWarnings("deprecation") public class RepositoryRestConfiguration { private MediaType defaultMediaType = MediaTypes.HAL_JSON; } Question: as this class is in deprecation , what is the correct way to set/override the default type? 回答1: You can do this via RepositoryRestConfiguration or simply with a property

Sending Custom JSON from Java class in case of auth failure of REST API on spring boot

不羁的心 提交于 2019-12-07 06:52:19
@Configuration @EnableWebSecurity @EnableGlobalMethodSecurity(prePostEnabled = true) public class SecurityConfig extends WebSecurityConfigurerAdapter{ @Autowired private RESTAuthenticationEntryPoint authenticationEntryPoint; @Autowired private RESTAuthenticationFailureHandler authenticationFailureHandler; @Autowired private RESTAuthenticationSuccessHandler authenticationSuccessHandler; @Autowired private UserDetailsService userDetailsService; @Override protected void configure(AuthenticationManagerBuilder auth) throws Exception { auth.userDetailsService(userDetailsService).passwordEncoder(new

Implement complex search feature using Spring BOOT REST and Spring Data JPA using Criteria API

心已入冬 提交于 2019-12-06 14:46:42
问题 I need to implement complex search feature using Spring Boot REST and Spring Data JPA using Criteria API. I need to provide RPIs like below /college?select=*&where=name:DemoCollege and location:LA and staff{firstName:foo, lastName:boo, workExp>10} and type in [1,2,3] Collage object has name , location , type fields and staff list. It has onetomany relationship with Staff so College can have one or many Staff members. based on the uri I need to build query using criteria api. I am finding it

How to use RestTemplate with multiple response types?

房东的猫 提交于 2019-12-06 03:56:00
问题 I'm using spring RestTemplate for communication with a xml webservice backend as follows: ResponseEntity<MainDTO> dto = restTemplate.postForObject(url, postData, MainDTO.class); Problem: the backend might either respond with MainDTO for normal data or with ErrorDTO in case of failures. But both with HTTP 200 . But I don't know which object will come back before! Anyways restTemplate requires me to pass the class type before. So, how could I parse the xml either to normal or the error bean?

Spring MVC ExceptionHandler for restful and normal

£可爱£侵袭症+ 提交于 2019-12-05 21:35:23
I want to handle exception for both normal and rest/ajax requests. Here is my code, @ControllerAdvice public class MyExceptionHandler { @ExceptionHandler(Exception.class) public ModelAndView handleCustomException(Exception ex) { ModelAndView model = new ModelAndView("error"); model.addObject("errMsg", ex.getMessage()); StringWriter sw = new StringWriter(); PrintWriter pw = new PrintWriter(sw); ex.printStackTrace(pw); sw.toString(); model.addObject("errTrace", sw); return model; } @ExceptionHandler(Exception.class) @ResponseBody public String handleAjaxException(Exception ex) { JSONObject model