spring-rest

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

房东的猫 提交于 2019-12-05 19:16:49
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? You can do this via RepositoryRestConfiguration or simply with a property in your application.properties. See the documentation here . The RepositoryRestConfiguration class is NOT

REST POST works correctly with POSTMAN but exception when using Spring RestTemplate

人走茶凉 提交于 2019-12-05 19:13:08
问题 I am writing a Rest client to post JSON data using Spring RestTemplate. Using POSTMAN and following JSON data in body get the response correctly- { "InCode":"test", "Name":"This is test", "Email":"test@gmail.com", "Id":18, } However when trying to hit the REST API using Spring RestTemplate as follows ResponseEntity<String> response = restTemplate.exchange(baseUrl, HttpMethod.POST, getHeaders(), String.class); private HttpEntity<?> getHeaders() throws JSONException { JSONObject request = new

Get XML in plain text

两盒软妹~` 提交于 2019-12-05 11:53:29
I have this endpoint for Spring Rest API: @PostMapping(value = "/v1/", consumes = { MediaType.APPLICATION_XML_VALUE, MediaType.APPLICATION_JSON_VALUE }, produces = { MediaType.APPLICATION_XML_VALUE, MediaType.APPLICATION_JSON_VALUE }) public PaymentResponse handleMessage(@RequestBody PaymentTransaction transaction, HttpServletRequest request) throws Exception { // get here plain XML } XML model. @XmlRootElement(name = "payment_transaction") @XmlAccessorType(XmlAccessType.FIELD) public class PaymentTransaction { public enum Response { failed_response, successful_response } @XmlElement(name =

spring rest lazy loading with hibernate

我的未来我决定 提交于 2019-12-05 03:14:37
问题 I am trying to develop spring rest api with hibernate. after searching in google, I have not find solution to lazy loading. I have two entity like below: University.java @Entity() @Table(schema = "core", name = "university") public class University extends BaseEntity { private String uniName; private String uniTelephon; @LazyCollection(LazyCollectionOption.FALSE) @OneToMany(fetch = FetchType.LAZY, mappedBy = "university", cascade = CascadeType.ALL) @JsonManagedReference private List<Student>

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

放肆的年华 提交于 2019-12-04 19:16:13
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 very complex to implement the toPredicate() method of org.springframework.data.jpa.domain.Specification

Reading HTTP headers in a Spring REST controller

半腔热情 提交于 2019-12-04 15:08:03
问题 I am trying to read HTTP headers in Spring based REST API. I followed this. But I am getting this error: No message body reader has been found for class java.lang.String, ContentType: application/octet-stream I am new to Java and Spring so can't figure this out. This is how my call looks like: @WebService(serviceName = "common") @Consumes({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON }) @Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON }) public interface

How to use RestTemplate with multiple response types?

二次信任 提交于 2019-12-04 07:42:27
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? Sidenote: I don't have any control of the webservice backend. As you would figure, the problem is that

REST POST works correctly with POSTMAN but exception when using Spring RestTemplate

守給你的承諾、 提交于 2019-12-04 01:25:09
I am writing a Rest client to post JSON data using Spring RestTemplate. Using POSTMAN and following JSON data in body get the response correctly- { "InCode":"test", "Name":"This is test", "Email":"test@gmail.com", "Id":18, } However when trying to hit the REST API using Spring RestTemplate as follows ResponseEntity<String> response = restTemplate.exchange(baseUrl, HttpMethod.POST, getHeaders(), String.class); private HttpEntity<?> getHeaders() throws JSONException { JSONObject request = new JSONObject(); request.put("Email", "test@gmail.com"); request.put("Id", "18"); request.put("Name", "This

spring rest lazy loading with hibernate

南楼画角 提交于 2019-12-03 17:17:29
I am trying to develop spring rest api with hibernate. after searching in google, I have not find solution to lazy loading. I have two entity like below: University.java @Entity() @Table(schema = "core", name = "university") public class University extends BaseEntity { private String uniName; private String uniTelephon; @LazyCollection(LazyCollectionOption.FALSE) @OneToMany(fetch = FetchType.LAZY, mappedBy = "university", cascade = CascadeType.ALL) @JsonManagedReference private List<Student> students; //setter and getter } Student.java @Entity @Table(schema = "core",name = "student") public

Spring Data Rest Validation Confusion

不羁岁月 提交于 2019-12-03 06:08:17
问题 Looking for some help with Spring data rest validation regarding proper handling of validation errors: I'm so confused with the docs regarding spring-data-rest validation here: http://docs.spring.io/spring-data/rest/docs/current/reference/html/#validation I am trying to properly deal with validation for a POST call that tries to save a new Company entity I got this entity: @Entity public class Company implements Serializable { @Id @GeneratedValue private Long id; @NotNull private String name;