0

I hope someone can help me with my problem. I write at the moment a Spring MVC Server and a client. Unfortunatly my Rest Controller only returns a 404. I am using a Tomcat 8. What I know is that the Spring Application is deployed correctly because I can visit my index.jsp page. The context, under which it is deployed is /backend. So my base url is localhost:8080/backend. Now I have a restcontroller whitch looks like this.

@Controller
@RequestMapping("/api/developer")
public class DeveloperController {

@Autowired
private DeveloperOutboundDipatcher service;

@RequestMapping(method = RequestMethod.GET, value = "/getAll")
public List<DeveloperDTO> findAll() {
    return service.findAllDeveloper();
}

@RequestMapping(method = RequestMethod.GET, value = "/get/{name}")
public DeveloperDTO findByName(@PathVariable String name) {
    return service.findOneByName(name);
}

@RequestMapping(method = RequestMethod.DELETE, value = "/delete/{name}")
public void deleteDeveloper(@PathVariable String name) {
    service.deleteDeveloper(name);
}

@RequestMapping(method = RequestMethod.POST, value = "/save/")
public void saveDeveloper(@RequestBody DeveloperDTO developer) {
    service.save(developer);
}

@RequestMapping(method = RequestMethod.PUT, value = "/edit/")
public void editDeveloper(@RequestBody DeveloperDTO developer) {
    service.edit(developer);
}
}

my config class looks like this:

package gamescreation;
@Configuration
@ComponentScan(basePackages= "gamescreation")
@EnableJpaRepositories("gamescreation")
@EnableTransactionManagement
@EnableWebMvc
public class MvcConfiguration extends WebMvcConfigurerAdapter{
private final String PROPERTY_DRIVER = "driver";
private final String PROPERTY_URL = "url";
private final String PROPERTY_USERNAME = "user";
private final String PROPERTY_PASSWORD = "password";
private final String PROPERTY_SHOW_SQL = "hibernate.show_sql";
private final String PROPERTY_DIALECT = "hibernate.dialect";

@Autowired
Environment environment;

@Bean
public ViewResolver getViewResolver(){
    InternalResourceViewResolver resolver = new InternalResourceViewResolver();
    resolver.setPrefix("/WEB-INF/views/");
    resolver.setSuffix(".jsp");
    return resolver;
}

@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
    registry.addResourceHandler("/resources/**").addResourceLocations("/resources/");
}

@Bean
public ModelMapper modelMapper() {
    return new ModelMapper();
}



@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
    LocalContainerEntityManagerFactoryBean em
            = new LocalContainerEntityManagerFactoryBean();
    em.setDataSource(dataSource());
    em.setPackagesToScan("gamescreation.entity" );

    JpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
    em.setJpaVendorAdapter(vendorAdapter);
    em.setJpaProperties(hibernateProperties());

    return em;
}

@Bean
public DataSource dataSource() {
    BasicDataSource dataSource = new BasicDataSource ();
    dataSource.setDriverClassName("org.postgresql.Driver");
    return dataSource;
}

@Bean
public PlatformTransactionManager transactionManager(
        EntityManagerFactory emf){
    JpaTransactionManager transactionManager = new JpaTransactionManager();
    transactionManager.setEntityManagerFactory(emf);

    return transactionManager;
}


Properties hibernateProperties() {
    return new Properties() {
        {
            setProperty("hibernate.hbm2ddl.auto",
                   "false");
            setProperty("hibernate.dialect", "org.hibernate.dialect.PostgreSQL82Dialect");
            setProperty("hibernate.globally_quoted_identifiers",
                    "true");
        }
    };
}

Properties additionalProperties() {
    Properties properties = new Properties();
    properties.setProperty("hibernate.hbm2ddl.auto", "create-drop");
    properties.setProperty(
            "hibernate.dialect", "org.hibernate.dialect.MySQL5Dialect");

    return properties;
}
@Bean
public SpringLiquibase liquibase() {
    SpringLiquibase liquibase = new SpringLiquibase();
    liquibase.setChangeLog("classpath:/db-changelog-master.xml");
    liquibase.setDataSource(dataSource());
    return liquibase;
}





@Bean
public PersistenceExceptionTranslationPostProcessor exceptionTranslation() {
    return new PersistenceExceptionTranslationPostProcessor();
}
}

I hope someone can tell me what is missing.

My client class looks like this:

@Service
public class DeveloperService {

private static final String DEVELOPER_BASEURL = "http://localhost:8080/backend/api/developer";

public List<DeveloperDTO> getDeveloperList() {
    RestTemplate restTemplate = new RestTemplate();
    DeveloperList response = restTemplate.getForObject(DEVELOPER_BASEURL + "/getAll", DeveloperList.class);
    return response.getDevelopers();
}

public DeveloperDTO getSelectedDeveloper(String name) {
    String url = DEVELOPER_BASEURL + "/get/{name}";
    UriComponentsBuilder builder = UriComponentsBuilder.fromUriString(url).queryParam("name", name);
    RestTemplate restTemplate = new RestTemplate();
    DeveloperDTO response = restTemplate.getForObject(builder.toUriString(), DeveloperDTO.class);
    return response;
}

public void delete(String name) {
    String url = DEVELOPER_BASEURL + "/delete/{name}";
    UriComponentsBuilder builder = UriComponentsBuilder.fromUriString(url).queryParam("name", name);
    RestTemplate restTemplate = new RestTemplate();
    restTemplate.delete(builder.toUriString());
}

public DeveloperDTO save(DeveloperDTO dto) {
    String url = DEVELOPER_BASEURL + "/save";

    HttpEntity<DeveloperDTO> request = new HttpEntity<>(dto);
    RestTemplate restTemplate = new RestTemplate();
    DeveloperDTO response = restTemplate.postForObject(url, request, DeveloperDTO.class);
    return response;

}
}

Thank you in advance for your help. If you need any other information from my project, feel free to ask.

6
  • for all paths you get 404 ?? Commented Oct 4, 2018 at 17:25
  • yes i get for every path 404 I tried it with soap ui Commented Oct 4, 2018 at 17:28
  • from your configuration class i can see that you are using a resolver for jsp files but i guess you want to create a rest api so remove this cinfiguration class MvcConfiguration and move the other configuration annotations to somewhere alse Commented Oct 4, 2018 at 17:35
  • Please replace @ Controller with @ RestController, your application is trying to figure out the view with the name of the response Commented Oct 4, 2018 at 17:42
  • @RestController hasnt work Commented Oct 4, 2018 at 18:00

1 Answer 1

1

@Controller alone will not work please use @Responsebody to get the response in REST or use the @RestController.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.