spring-restcontroller

Spring MVC - @RequestParam causing MissingServletRequestParameterException with x-www-form-urlencoded

非 Y 不嫁゛ 提交于 2019-12-10 11:35:06
问题 The follwing Spring MVC code throws MissingServletRequestParameterException, @Controller @RequestMapping("/users") public class XXXXResource extends AbstractResource { ..... @RequestMapping(method = RequestMethod.PUT , produces = {"application/json", "application/xml"} , consumes = {"application/x-www-form-urlencoded"} ) public @ResponseBody Representation createXXXX(@NotNull @RequestParam("paramA") String paramA, @NotNull @RequestParam("paramB") String paramB, @NotNull @RequestParam("paramC"

Optional @Pathvariable in REST controller spring 4

匆匆过客 提交于 2019-12-10 10:44:18
问题 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) {

How to return plain text in spring controller?

扶醉桌前 提交于 2019-12-10 04:51: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

Infinite loop with spring-boot in a one to many relation

荒凉一梦 提交于 2019-12-09 06:24:34
问题 In a rest application, I use spring boot with jpa. I have a class Lodger who have @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "lodger") private List<Reference> referenceList; In my class Reference, i have @ManyToOne @JoinColumn(name = "lodgerId") private Lodger lodger; when i call this method @RequestMapping(value = "/lodgers/{lodgerId}", method = RequestMethod.GET) public Lodger getLogderById(@PathVariable("lodgerId") long lodgerId) { return lodgerService

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

Spring - RestController annotation does not catch request

北战南征 提交于 2019-12-08 01:36:34
问题 I don't understand why If I use RestController annotation to declare a class as service, like this: @RestController("/registration") public class RegistrationService { @RequestMapping(value="/", produces="application/json") public String initializeSession(Model model){ return "{\"success\":1}"; } } when I do a request like http://localhost:8080/SpringRest/registration/ I get an 404 status and in the console: No mapping found for HTTP request with URI [/SpringRest/registration/] in

Test spring rest controller secured with https

痞子三分冷 提交于 2019-12-07 16:43:05
问题 Can anybody provide me with a code sample to write integration test for a controller which is secured with HTTPS?? With HTTP I am able to write, but with HTTPS I am getting certification error. Controller @RestController @RequestMapping("/rest/otm/v1") public class LoginController { @Autowired UserAuthenticationService userAuthService; @ExceptionHandler({ AuthenticationFailedException.class}) @RequestMapping(value = "/login", method = RequestMethod.POST) @ResponseBody public void login

unit test Spring MissingServletRequestParameterException JSON response

会有一股神秘感。 提交于 2019-12-07 10:52:38
问题 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

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

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

我与影子孤独终老i 提交于 2019-12-07 02:10:29
问题 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