spring-restcontroller

Optional @Pathvariable in REST controller spring 4

我怕爱的太早我们不能终老 提交于 2019-12-06 07:26:55
I'm writing a Rest Service (HTTP Get endpoint), where in the below uri does the following http://localhost:8080/customers/{customer_id} fetch the details for the customer_id passed in the uri if the customer_id is not passed ( http://localhost:8080/customers ), fetch all the customers details. Code: @RequestMapping(method = RequestMethod.GET, value = "customers/{customer_id}") public List<Customer> getCustomers( @PathVariable(name = "customer_id", required = false) final String customerId) { LOGGER.debug("customer_id {} received for getCustomers request", customerId); } However, with the above

unit test Spring MissingServletRequestParameterException JSON response

家住魔仙堡 提交于 2019-12-05 16:43:15
I have POST method in a Spring boot rest controller as follows @RequestMapping(value="/post/action/bookmark", method=RequestMethod.POST) public @ResponseBody Map<String, String> bookmarkPost( @RequestParam(value="actionType",required=true) String actionType, @RequestParam(value="postId",required=true) String postId, @CurrentUser User user) throws Exception{ return service.bookmarkPost(postId, actionType, user); } now if I test with missing parameter in Postman I get an 400 http response and a JSON body: { "timestamp": "2015-07-20", "status": 400, "error": "Bad Request", "exception": "org

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 =

How to return plain text in spring controller?

依然范特西╮ 提交于 2019-12-05 10:31:41
I want to return a simple plain text string as follows: @RestController @RequestMapping("/test") public class TestController { @ResponseStatus(HttpStatus.OK) @RequestMapping(value = "/my", method = RequestMethod.GET, produces="text/plain") public String test() { return "OK"; } Problem: I also have a global ContentNegotiation filter as follows: @Configuration public class ContentNegotiationAdapter extends WebMvcConfigurerAdapter { @Override public void configureContentNegotiation(ContentNegotiationConfigurer configurer) { configurer.favorPathExtension(false) .favorParameter(true)

In the context of SpringMVC, how to have web services that provide different JSON representation of a same class?

我与影子孤独终老i 提交于 2019-12-05 06:33:48
I have a data class, something like this: public class Person { private String name; private Long code; // corresponding getters and setters } I want to write two web services that provide two different JSON representation of Person. For example, one of them provide {"name":"foo"} but the other one {"name":"foo", "code":"123"} . As a more complicated scenario, suppose that Person has a reference property, for example address. Address has its own properties as well and I expect that both of my web services consider this property but each of which do this in their own manner. How should my

Difference between path and value in spring boot Requestmapping

匆匆过客 提交于 2019-12-04 22:44:34
What is the difference between below two and which on to Use when ? @GetMapping(path = "/usr/{userId}") public String findDBUserGetMapping(@PathVariable("userId") String userId) { return "Test User"; } @RequestMapping(value = "/usr/{userId}", method = RequestMethod.GET) public String findDBUserReqMapping(@PathVariable("userId") String userId) { return "Test User"; } As mentioned in the comments (and the documentation ), value is an alias to path . Spring often declares the value element as an alias to a commonly used element. In the case of @RequestMapping (and @GetMapping , ...) this is the

Restful-based video streaming

别等时光非礼了梦想. 提交于 2019-12-04 20:18:05
Using spring boot, I want to make RESTful-based video player. I have my .mp4 extension videos in my file browser. How can I serve these videos on the frontend side by creating a rest endpoint? I've tried this method. The video can be started or stopped. But it can not be done backwards or forwards. Can not get it to the desired minute and start. Spring Content supports video streaming out of the box. Using Spring Content for the file-system (FS) you would be able to create yourself a video store backed by the filesystem, put your video(s) in that store, and using the companion library Spring

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

nested exception is java.lang.IllegalArgumentException: Not a managed type: class

两盒软妹~` 提交于 2019-12-04 03:19:13
问题 I am trying to deploy a Spring-Boot application with Service accessing a JpaRepository which is connecting to PostgreSQL DB at runtime using JPA and Hibernate , refering to connection properties in src/main/resources/application.properties When I am deploying the built .WAR on Tomcat, the Application is failing to start with the error as given in below Error Log. Can somebody please help out what is the meaning of this error? Note I have noted the trouble points in MyServiceImpl and

Return HTTP 204 on null with spring @RestController

不想你离开。 提交于 2019-12-02 19:02:58
This returns 200 OK with Content-Length: 0 @RestController public class RepoController { @RequestMapping(value = "/document/{id}", method = RequestMethod.GET) public Object getDocument(@PathVariable long id) { return null; } } Simply put I'd like it to return 204 No Content on null. Is there a way to force spring-mvc/rest to return 204 on null not 200? I dont want to change every rest method to return ResponseEntity or something like that, only map null to 204 Karthik R Of course yes. Option 1 : @RestController public class RepoController { @RequestMapping(value = "/document/{id}", method =