Annotation RequestMapping() giving the error - java

I am starting with Spring Boot and trying to make a Rest service.
I am writing a controller where there are RequestMappings to 3 methods.
Two of them are working fine while the thirl annotation is giving this error while writing the code.
Multiple markers at this line
- Syntax error, insert "enum Identifier" to complete EnumHeader
- Syntax error, insert "EnumBody" to complete EnumDeclaration
I tried everything from other answers but cant seem to find out the issue. Here is my code for the Controller-
package io.springboot.topics;
import java.util.Arrays;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
#RestController
public class TopicsController {
#Autowired
private TopicSrvice topicService;
#RequestMapping("/topics")
public List<Topic> getAllTopics() {
return topicService.getAllTopics();
}
#RequestMapping("/topics/{id}")
public Topic getTopic(#PathVariable String id) {
return topicService.getTopic(id);
}
#RequestMapping(method=RequestMethod.POST,value="/topics")
}
The error is coming in the last line ie last Requestmapping().

A bit late but for those that are just finding this issue: You need to type in the actual method under the #RequestMapping. Eclipse gives an issue when you stop at this stage but once you write your method, you are good to go. At least that is what worked for me.
So:
#RequestMapping(method=RequestMethod.POST,value="/topics")
public ... {
//your method
}

You are writing /topics URL for two methods,one for GET and one for POST,Spring does not support this configuration,You can either change url for two different methods or you can write a single method having url /topics with array of HttpMethod like method = { RequestMethod.GET, RequestMethod.POST }

Related

Spring Boot Integration Test with awaitility and #SpyBean not working, because #SpyBean is always null

I want to test my scheduled task, so I followed this tutorial
#SpringJUnitConfig(SchedulerConfig.class)
public class MailJobFinderTaskIT {
#SpyBean
private MailJobFinderTask mailJobFinderTask;
#Test
public void whenWaitThreeSecond_ThenTaskCalledThreeTimes(){
await()
.atMost(Duration.ofSeconds(3))
.untilAsserted(() -> verify(mailJobFinderTask, atLeast(3)).findEmailJobs());
}
}
But actually it does not work, because the the following error
org.mockito.exceptions.misusing.NullInsteadOfMockException:
Argument passed to verify() should be a mock but is null!
Examples of correct verifications:
verify(mock).someMethod();
verify(mock, times(10)).someMethod();
verify(mock, atLeastOnce()).someMethod();
not: verify(mock.someMethod());
Also, if you use #Mock annotation don't miss initMocks()
here is the signature of my Task-Class
#Component
public class MailJobFinderTask extends SuppressedLogPoller {
....
}
#Scheduled(fixedRate = 1000)
public void findEmailJobs() {
.
.
}
I tried already to change the Annotation to #SpringBootTest and also tried to used #MockBean instead of #SpyBean but without any success. Actually I do not understand why my bean mailJobFinderTask is not created
Your code seems to work fine on my system, but I had to do a few changes.
I would recommend these as starting points to your troubleshooting. Also, you haven't mentioned if you can successfully replicate the linked tutorial, which also runs fine on my system. If you haven't, I would recommend trying the tutorial first, confirming it works, and then adding your own code.
If the below does not help you, then it could be that the issue is not in the part of the code you posted. In that case, sharing the full code would help us better troubleshoot the problem. But for now, maybe pay particular attention to the import statements and the main changes listed below.
Changes from your code
I changed the Duration.ofSeconds(3) method to Duration.FIVE_SECONDS. The reason for this is that the former is from java.time package, while await() expects a Duration class from the awaitility package. I just used FIVE_SECONDS for compatibility purposes.
I had to remove the "extends SuppressedLogPoller" part, as I did not have access to this class. I would also recommend removing it and adding this back again to see if it is causing the issue.
Test class code
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.mock.mockito.SpyBean;
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
import org.awaitility.Duration;
import static org.awaitility.Awaitility.await;
import static org.mockito.Mockito.atLeast;
import static org.mockito.Mockito.verify;
#SpringJUnitConfig(ScheduledConfig.class)
public class MailJobFinderTaskIT {
#SpyBean
private MailJobFinderTask mailJobFinderTask;
#Test
public void whenWaitThreeSecond_ThenTaskCalledThreeTimes(){
await()
.atMost(Duration.FIVE_SECONDS)
.untilAsserted(() -> verify(mailJobFinderTask, atLeast(7)).findEmailJobs());
}
}
Domain class
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
#Component
public class MailJobFinderTask {
#Scheduled(fixedRate = 1200)
public void findEmailJobs() {
}
}
As mentioned in the error message:
Examples of correct verifications:
verify(mock).someMethod();
verify(mock, times(10)).someMethod();
verify(mock, atLeastOnce()).someMethod();
not: verify(mock.someMethod());
You should update the following line:
.untilAsserted(() -> verify(mailJobFinderTask, atLeast(3)).findEmailJobs());
as shown below:
.untilAsserted(() -> verify(mailJobFinderTask, atLeast(3)).findEmailJobs();

A null pointer exception of connecting hybris database using flexiblesearch service because of getJaloResult() error

A null pointer exception occurred when trying to connect to hybris database using hybris flexible search service seemingly due to getJaloResult() method.
I need to retrieve certain data from hybris commerce database. I tried to use hybris flexible service to do that by using defaultFlexibleSearchService.search() method, but I got a null pointer exception. It seems that the exception occurred when search() method tries to call getJaloResult() method. I have no clue about the solution - thanks for any hints.
My class definition code is here
package de.hybris.platform.integrationservices.audit;
import java.util.stream.Stream;
import com.sun.tools.javac.util.List;
import de.hybris.platform.audit.TypeAuditReportConfig;
import de.hybris.platform.audit.view.AuditViewService;
import de.hybris.platform.audit.view.impl.ReportView;
import de.hybris.platform.servicelayer.search.FlexibleSearchQuery;
import de.hybris.platform.servicelayer.search.FlexibleSearchService;
import de.hybris.platform.servicelayer.search.RelationQuery;
import de.hybris.platform.servicelayer.search.SearchResult;
import de.hybris.platform.servicelayer.search.impl.DefaultFlexibleSearchService;
import de.hybris.platform.integrationservices.model.IntegrationObjectModel;
public class IntegrationObjectAudit implements AuditViewService
{
private DefaultFlexibleSearchService defaultFlexibleSearchService;
public IntegrationObjectAudit() {
defaultFlexibleSearchService = new DefaultFlexibleSearchService();
}
public List<IntegrationObjectModel> searchModel(){
String query = "SELECT {PK} FROM {IntegrationObject}";
FlexibleSearchQuery flexibleSearchQuery = new FlexibleSearchQuery(query);
flexibleSearchQuery.setCount(1);
de.hybris.platform.servicelayer.search.SearchResult<IntegrationObjectModel> resListIntegrationModel = this.defaultFlexibleSearchService.search(query);
List<IntegrationObjectModel> resList = (List<IntegrationObjectModel>) resListIntegrationModel.getResult();
return resList;
}
}
Didnt notice it was posted days back. Hope you already resolved it. Still adding my answer as it may be helpful for others.
What I see you are not injecting flexibleSearchService bean in IntegrationObjectAudit. And as flexibleSearchService bean is not injected, its causing NullPointerException when you call any function on it.
You can fix it as following (spring bean injection concept)
Either you should create a setter function and inject it by spring xml
Or use #Resource or #Autowire annotation for same
public class IntegrationObjectAudit implements AuditViewService
{
#Resource
private DefaultFlexibleSearchService flexibleSearchService;
.....
Hope it helps. Let me know please.

Jax-ws endpoint not asking to create wrapper classes

I'm following this simple tutorial: https://www.mkyong.com/webservices/jax-ws/jax-ws-hello-world-example-document-style/?fbclid=IwAR0vxhYrj9MKy1Q28h6luFVJoSxDP4KWBOLEu_v_Ss4uQztmB-9JuYsS4RI and at step 3 it mentions that I should receive the error:
Wrapper class com.mkyong.ws.jaxws.GetHelloWorldAsString is not found.
Have you run APT to generate them?
However, I do not get such error(no error at all) and I'm worried that it is not working as expected.
My classes:
Interface:
package com.soap3sk.ws;
import javax.jws.WebMethod;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
import javax.jws.soap.SOAPBinding.Style;
import javax.jws.soap.SOAPBinding.Use;
// Service Endpoint Interface
#WebService
#SOAPBinding(style= Style.DOCUMENT, use= Use.LITERAL) // optional
public interface NoteEndpoint {
//#WebMethod ArrayList<ToDoNote> getNotes();
#WebMethod String getHelloWorldAsString(String name);
}
Implementation:
package com.soap3sk.ws;
import javax.jws.WebService;
#WebService(endpointInterface = "com.soap3sk.ws.NoteEndpoint")
public class NoteEndpointImpl implements NoteEndpoint {
#Override
public String getHelloWorldAsString(String name) {
return "Hello World JAX-WS " + name;
}
}
Publisher:
package com.soap3sk.endpoint;
import javax.xml.ws.Endpoint;
import com.soap3sk.ws.NoteEndpointImpl;
public class NoteEndpointPublisher {
public static void main (String[] args) {
Endpoint.publish("http://localhost:5000/ws/hello", new NoteEndpointImpl());
}
}
Project structure: https://imagizer.imageshack.com/img924/3514/BAuOcl.png
What I also noticed that those 2 .class files(asString and Response that are mentioned in the guide) are not generated anywhere as well. I'm using Eclipse and created a maven project with the quickstart archetype. Runnning it as a standard java application.
I can access the wsdl file going here: http://localhost:5000/ws/hello?wsdl and the I can see getHelloWorldAsString and getHelloWorldAsStringResponse there, but they are nowhere to be seen in my project and no error is thrown that they could not be found as mentioned in the guide that it should.
I also tried downloading the sample project and deleting the .java files that should be required, but it is stil the same(no error, not asking to create those classes).
I would be very grateful if someone could help. Thank you.
EDIT
I found a similiar question here: Java web service not giving error message Could someone explain his answer? Is the creation of those two classes not necessary?
you're trying to replicate a situation reported almost 10 years ago. Don't you want to try a newer tutorial like the following:
https://www.baeldung.com/jax-ws
https://spring.io/guides/gs/producing-web-service/

Cannot resolve method 'List.of(java.lang.String, java.lang.String)'

I've been trying to create a simple hello world application with Java and SpringBoot in IntelliJ IDEA, but I get this error and I don't know how to solve it.
I get the error at the return. Java doesn't seem to know how to resolve the of method that's in the public List<String> getHelloWorld method.
package com.myname.SpringApp;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
#RestController
public class HelloWorldController {
#RequestMapping("api/hello-world")
#GetMapping
public List<String> getHelloWorld(){
return List.of("Hello", "World");
}
}
The overloaded List.of methods were introduced in Java 9.
Since:
9
You must be compiling with an older version. Update to Java 9 or later. Here's how.
You can use Arrays.asList.
List.of will support java 9 and above

Get service in Rest Controller is not getting called

package test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
#RestController
public class CommodityControllerTest {
#GetMapping("/api/test")
public void test() {
System.out.println("in controler");
}
}
When I call Get service from POSTMAN, it's not getting called as there is nothing coming in the console.
What I am doing wrong here?
Please let me know if additional information is required.
Try moving the CommodityControllerTest class to the com.miapp.MIApp.controller package.
Your CommodityControllerTest class probably isn't getting picked up during the component scan.
From your screen shot it looks like the class is not in a package. If you just have an #SpringBootApplication annotation on you main class you will need to put your class in a package below where your main class is so that it can find it.

Categories