Posts

Showing posts with the label spring-mvc

Spring MockMvc WebApplicationContext is null when running JUnit Tests with Maven

Spring MockMvc WebApplicationContext is null when running JUnit Tests with Maven I have a Spring mock-mvc JUnit test class that contains two tests. When I run the tests within Eclipse IDE both tests pass (I use Eclipse Maven plugin). When running the tests from the command line using mvn test one of the tests fails because the WebApplicationContext that is @Autowired is sometimes null. Here is my test class @WebAppConfiguration @ActiveProfiles({ "dev", "test" }) public class AddLinkEndpointMvcTest extends BaseMvc { @Autowired private WebApplicationContext wac; private MockMvc mockMvc; @Before public void before() { System.out.println("WAC = " + wac); mockMvc = MockMvcBuilders.webAppContextSetup(wac).build(); } @Test public void addLinkDoesNotSupportGet() throws Exception { mockMvc.perform(get("/myurl")).andExpect(status().is(HttpStatus.SC_METHOD_NOT_ALLOWED)); } @Test public v...

Spring boot 2.0 custom converter being ignored

Spring boot 2.0 custom converter being ignored I've either completely miss-understood converters, or all the examples/blogs/docs I've found are assuming I've done a step... My understanding is that when I post a String , the converter will pick it up and give the Controller a POJO instead. String Controller The problem I have is the converter is never being called. The payload coming into the controller is a standard JSON API string { "data":{ "attributes":{ "created-date":null, }, "type":"steaks" } } The converter: @Slf4j @Configuration public class SteakConverter implements Converter<String, Steak> { @Override public Steak convert(String value) { log.info("converter value {}", value); return new Steak.builder().build(); } } The controller: @Slf4j @CrossOrigin @RestController public class SteakController { @PostMapping("/steaks") ...

Spring MVC Bean Validation --Data Type Validation

Spring MVC Bean Validation --Data Type Validation public class User { @NotNull(message="Age is Required") @Min(value = 18, message = "Age must be greater than or equal to 18") @Max(value = 150, message = "Age must be less than or equal to 150") private Integer age; } If the User enters "String Value" in age, I want to validate them and should provide error message "Age is Invalid". @OnlyNumber(message = "Age is Invalid") //Like This Which Annotation helps in validating this kind of Type Validation, Similarly if the use enters different type format instead of Date. How do we handle them and provide custom error message. you can create your own @OnlyNumber – Pravin Mar 24 '15 at 15:25 @OnlyNumber 1 Answer 1 ...

XML file should be read and store in java object to use throughout application on spring mvc

XML file should be read and store in java object to use throughout application on spring mvc I have a xml file which is having all SQL queries. While Application Context gets loaded, from the servlet, I read the file and store it in HashMap which is static and will use that object throught the application. Now , I migrate to spring mvc . I included the sql xml in resourced folder . I need help to read and use that resource throught the application. Is there any way to handle this. Thanks. I don't know the structure of your file. But what I can suggest is to convert your xml file into an object UnMarshalling , then depending on your needs do whatever with that object. There are a lot of good tutorials out there. – Dfor Tye Jul 1 at 17:56 UnMarshalling Thank you. I can unmarshall the object by having that logic...