I write request mapping at interface but it's not working.
Request / response body POJOs:
public class ReqAa {
private String firstValue;
private String secondValue;
public String getFirstValue() {
return firstValue;
}
public ReqAa setFirstValue(String firstValue) {
this.firstValue = firstValue;
return this;
}
public String getSecondValue() {
return secondValue;
}
public ReqAa setSecondValue(String secondValue) {
this.secondValue = secondValue;
return this;
}
}
public class RespAa{
private String status;
RespAa(String status){
this.status = status;
}
public String getStatus() {
return status;
}
public RespAa setStatus(String status) {
this.status = status;
return this;
}
}
Interfaces:
public interface A{
interface Aa{
#PostMapping("/do/something")
RespAa doSomething(#RequestBody ReqAa);
}
interface Ab{
#PostMapping("/do/another")
RespAb doAnother(#RequestBody ReqAb);
}
}
#PreAuthorize("hasAuthority('admin')")
#RequestMapping("/api/admin")
public interface IClient extends A.Aa{
}
Rest Controller:
#RestController
public class Client implements IClient{
#Override
public RespAa doSomething(ReqAa reqAa) {
return new RespAa("SUCCESS");
}
}
Spring boot #RequestBody could not mapping as Body. Its took it as parameter.
Example:
generated request: /api/admin/do/something?firstValue=fv&secondValue=sv
expected mapping: /api/admin/do/something
requestBody: { "firstValue":"fv","secondValue":"sv"}
its work when i used #RequestBody in implementaion method.
#RestController
public class Client implements IClient{
#Override
public RespAa doSomething(#RequestBody ReqAa reqAa) {
return new RespAa("SUCCESS");
}
}
I used spring boot version: 1.5.10.
I solved my problem using interface default method.
Working code here:
Interfaces:
public interface A{
interface Aa{
RespAa doSomething(ReqAa);
#PostMapping("/do/something")
default RespAa dDoSomething(#RequestBody ReqAa){
return doSomething(ReqAa);
}
}
interface Ab{
#PostMapping("/do/another")
RespAb doAnother(#RequestBody ReqAb);
}
}
#PreAuthorize("hasAuthority('admin')")
#RequestMapping("/api/admin")
public interface IClient extends A.Aa{
}
Rest Controller:
#RestController
public class Client implements IClient{
#Override
public RespAa doSomething(ReqAa reqAa) {
return new RespAa("SUCCESS");
}
}
Related
I have try to save data from database using Unit OF Work Pattern.But I got error.My code is:
Unit of Work code:
private PousadaManagementContext MinhaPousadaContext { get; }
public IPousadaRepository PousadaRepository { get; private set; }
public UnitOfWork
(
PousadaManagementContext minhaPousadaContext,
IPousadaRepository pousadaRepository
)
{
this.MinhaPousadaContext = minhaPousadaContext;
PousadaRepository = pousadaRepository;
}
public async Task<int> CompleteAsync()
{
return await MinhaPousadaContext.SaveChangesAsync();
}
public int Complete()
{
return MinhaPousadaContext.SaveChanges();
}
public void Dispose() => MinhaPousadaContext.Dispose();
public interface IUnitOfWork : IDisposable
{
int Complete();
Task<int> CompleteAsync ();
}
businessclass:
private UnitOfWork managementUoW { get; }
public PousadaBusiness(IUnitOfWork ManagementUoW)
{
ManagementUoW = managementUoW;
}
public async Task<Pousada> Save(Pousada p)
{
return await managementUoW.PousadaRepository.Save(p);
}
Here In Save method p all properties have value but managementUoW is null.
How to Solve this error.
In my spring mvc web-application i use a generic converter that converts String (id) to Company by fetch using (service and dao) components
first of all in my MVC-config i add the converter like follow :
#Override
public void addFormatters(FormatterRegistry registry) {
registry.addConverter(new GenericIdToCompanyConverter(new CompanyServiceImp()));
}
companyService
#Service
#Transactional
#Qualifier("companyService")
public class CompanyServiceImp implements ICompanyService {
#Resource
#Qualifier("companyDAO")
private ICompanyDao dao;
public void setDao(ICompanyDao dao) {
this.dao = dao;
}
#Override
public Company find(Long id) throws BusinessException {
Company current = dao.find(id);
if(current == null) {
throw new BusinessException("notFound");
}
return current;
}
....
}
Generic converter :
public class GenericIdToCompanyConverter implements GenericConverter {
private ICompanyService companyService;
public GenericIdToCompanyConverter(ICompanyService companyService) {
super();
this.companyService = companyService;
}
#Override
public Set<ConvertiblePair> getConvertibleTypes() {
ConvertiblePair[] pairs = new ConvertiblePair[] { new ConvertiblePair(Number.class, Company.class), new ConvertiblePair(String.class, Company.class) };
return ImmutableSet.copyOf(pairs);
}
#Override
public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor targetType) {
long id = 0;
if( sourceType.getType() == String.class) {
try {
id = Long.valueOf((String) source);
}catch(NumberFormatException e) {
return null;
}
}else if( sourceType.getType() == Number.class) {
id = (Long) source;
}else {
return null;
}
try {
return companyService.find(Long.valueOf(id));
} catch (BusinessException e) {
return null;
}
}
}
and here the controller that receives data form (via ajax request)
public #ResponseBody JsonResponseBean applay(#Valid VoucherForm form, BindingResult result, Locale locale) throws BusinessException {
....
}
where VoucherForm has these attributes
public class VoucherForm{
protected Long id;
protected Company company;
...
}
when i run the application and call controller method it returns type mismatch error for company attribute
and when i execute this on debug mode i see that it fails on serviceCompany - dao.find(id) statment where my dao is == null
Please help
finally i have to autowire the converter
Mvc-config
....
#Autowired
private GenericIdToCompanyConverter genericIdToCompanyConverter;
#Override
public void addFormatters(FormatterRegistry registry) {
registry.addConverter(genericIdToCompanyConverter);
}
and update the converter like follow :
public class GenericIdToCompanyConverter implements GenericConverter {
#Resource
#Qualifier("companyService")
private ICompanyService companyService;
#Override
public Set<ConvertiblePair> getConvertibleTypes() {
....
}
#Override
public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor targetType) {
....
}
}
How to fetch data from corda Custom tables?
my sample code is as follows :-
Api layer -- getIous() method
{
Field attributeValue=IOUSchemaV1.PersistentIOU.class.getDeclaredField("value");
CriteriaExpression currencyIndex = Builder.equal(attributeValue, "12");
QueryCriteria.VaultCustomQueryCriteria criteria = new
QueryCriteria.VaultCustomQueryCriteria(currencyIndex);
vaultStates = services.vaultQueryByCriteria(criteria,IOUState.class);
}
In ExamplePlugin I added below code for schema registration
public class ExamplePlugin extends CordaPluginRegistry implements
WebServerPluginRegistry
{
#NotNull
#Override
public Set<MappedSchema> getRequiredSchemas()
{
Set<MappedSchema> requiredSchemas = new HashSet<>();
requiredSchemas.add(new IOUSchemaV1());
return requiredSchemas;
}
}
My Schema classes are ---
public final class IOUSchema {
}
#CordaSerializable
public class IOUSchemaV1 extends MappedSchema
{
public IOUSchemaV1() {
super(IOUSchema.class, 1, ImmutableList.of(PersistentIOU.class));
}
#Entity
#Table(name = "iou_states")
public static class PersistentIOU extends PersistentState {
#Column(name = "sender_name") private final String senderName;
#Column(name = "recipient_name") private final String recipientName;
#Column(name = "value") private final int value;
public PersistentIOU(String senderName, String recipientName, int value) {
this.senderName = senderName;
this.recipientName = recipientName;
this.value = value;
}
public String getSenderName() {
return senderName;
}
public String getRecipientName() {
return recipientName;
}
public int getValue() {
return value;
}
}
}
my state has :-
public class IOUState implements LinearState, QueryableState
{
--- some code goes here and below methods as well.---
#Override
public PersistentState generateMappedObject(MappedSchema schema) {
if (schema instanceof IOUSchemaV1) {
return new IOUSchemaV1.PersistentIOU(
this.sender.getName().toString(),
this.recipient.getName().toString(),
this.iou.getValue());
} else {
throw new IllegalArgumentException("Unrecognised schema $schema");
}
}
#Override
public Iterable<MappedSchema> supportedSchemas() {
return ImmutableList.of(new IOUSchemaV1());
}
}
But all the time i am getting below exception.
Caused by: net.corda.core.node.services.VaultQueryException:
Please register the entity 'com.example.schema.IOUSchemaV1' class in your CorDapp's CordaPluginRegistry configuration (requiredSchemas attribute)
and ensure you have declared (in supportedSchemas()) and mapped (in generateMappedObject())
the schema in the associated contract state's QueryableState interface implementation.
Can anyone please help to resolve this.
Try deleting implements WebServerPluginRegistry from your plugin declaration.
In Spring, is there a way I can access the properties of the unitofwork across the DAO's?
Let's assume I have a service layer:
public Class A {
public doSave (){
impl.setUnitOfWork(details);
daoA.saveA(dto.getEntityADetails())
daoB.saveB(dto.getEntityBDetails());
}
}
public class Impl extends TopLinkDaoSupport {
public void setUnitOfWork (TxnDetailsDTO details) {
getSection().getTopLinkTemplate().execute (new UnitOfWorkCallback() {
protected object doInUnitOfWork (UnitofWork uow) {
uow.setProperty ('prop1', details);
return null;
}
});
}
}
public Class DAOA {
public saveA (DTO dto) {
getSection().getTopLinkTemplate().execute (new UnitOfWorkCallback() {
protected object doInUnitOfWork (UnitofWork uow) {
if (uow.getProperty('prop1'))
uow.registerObject(dto);
return null;
}
});
}
}
public Class DAOB {
public saveB (DTO dto) {
getSection().getTopLinkTemplate().execute (new UnitOfWorkCallback() {
protected object doInUnitOfWork (UnitofWork uow) {
if (uow.getProperty('prop1'))
uow.registerObject(dto);
return null;
}
});
}
}
Server is Tomcat.
I have a similar implementation done with WebSphere & that works, but with Tomcat I am not able to access it.
I have a simple resource like:
#Path("/")
public class RootResource {
#Context WebConfig wc;
#PostConstruct
public void init() {
assertNotNull(wc);
}
#GET
public void String method() {
return "Hello\n";
}
}
Which I am trying to use with JerseyTest (2.x, not 1.x) and the GrizzlyTestContainerFactory.
I can't work out what I need to do in terms of config to get the WebConfig object injected.
I solved this issue by creating a subclass of GrizzlyTestContainerFactory and explicitly loading the Jersey servlet. This triggers the injection of the WebConfig object. The code looks like this:
public class ExtendedGrizzlyTestContainerFactory implements TestContainerFactory {
private static class GrizzlyTestContainer implements TestContainer {
private final URI uri;
private final ApplicationHandler appHandler;
private HttpServer server;
private static final Logger LOGGER = Logger.getLogger(GrizzlyTestContainer.class.getName());
private GrizzlyTestContainer(URI uri, ApplicationHandler appHandler) {
this.appHandler = appHandler;
this.uri = uri;
}
#Override
public ClientConfig getClientConfig() {
return null;
}
#Override
public URI getBaseUri() {
return uri;
}
#Override
public void start() {
if (LOGGER.isLoggable(Level.INFO)) {
LOGGER.log(Level.INFO, "Starting GrizzlyTestContainer...");
}
try {
this.server = GrizzlyHttpServerFactory.createHttpServer(uri, appHandler);
// Initialize and register Jersey Servlet
WebappContext context = new WebappContext("WebappContext", "");
ServletRegistration registration = context.addServlet("ServletContainer", ServletContainer.class);
registration.setInitParameter("javax.ws.rs.Application",
appHandler.getConfiguration().getApplication().getClass().getName());
// Add an init parameter - this could be loaded from a parameter in the constructor
registration.setInitParameter("myparam", "myvalue");
registration.addMapping("/*");
context.deploy(server);
} catch (ProcessingException e) {
throw new TestContainerException(e);
}
}
#Override
public void stop() {
if (LOGGER.isLoggable(Level.INFO)) {
LOGGER.log(Level.INFO, "Stopping GrizzlyTestContainer...");
}
this.server.stop();
}
}
#Override
public TestContainer create(URI baseUri, ApplicationHandler application) throws IllegalArgumentException {
return new GrizzlyTestContainer(baseUri, application);
}
Notice that the Jersey Servlet configuration is being loaded from the ApplicationHandler that is passed in as a parameter using the inner Application object's class name (ResourceConfig is a subclass of Application). Therefore, you also need to create a subclass of ResourceConfig for this approach to work. The code for this is very simple:
package com.example;
import org.glassfish.jersey.server.ResourceConfig;
public class MyResourceConfig extends ResourceConfig {
public MyResourceConfig() {
super(MyResource.class);
}
}
This assumes the resource you are testing is MyResource. You also need to override a couple of methods in your test like this:
public class MyResourceTest extends JerseyTest {
public MyResourceTest() throws TestContainerException {
}
#Override
protected Application configure() {
return new MyResourceConfig();
}
#Override
protected TestContainerFactory getTestContainerFactory() throws TestContainerException {
return new ExtendedGrizzlyTestContainerFactory();
}
#Test
public void testCreateSimpleBean() {
final String beanList = target("test").request().get(String.class);
Assert.assertNotNull(beanList);
}
}
Finally, for completeness, here is the code for MyResource:
#Path("test")
public class MyResource {
#Context WebConfig wc;
#PostConstruct
public void init() {
System.out.println("WebConfig: " + wc);
String url = wc.getInitParameter("myparam");
System.out.println("myparam = "+url);
}
#GET
#Produces(MediaType.APPLICATION_JSON)
public Collection<TestBean> createSimpleBean() {
Collection<TestBean> res = new ArrayList<TestBean>();
res.add(new TestBean("a", 1, 1L));
res.add(new TestBean("b", 2, 2L));
return res;
}
#POST
#Produces(MediaType.APPLICATION_JSON)
#Consumes(MediaType.APPLICATION_JSON)
public TestBean roundTrip(TestBean s) {
return s;
}
}
The output of running the test shows that the WebConfig is loaded and the init param is now available:
WebConfig: org.glassfish.jersey.servlet.WebServletConfig#107d0f44
myparam = myvalue
The solution from #ametke worked well but wasn't picking up my ExceptionMapper classes. To solve this I simplified the start() method to:
#Override
public void start() {
try {
initParams.put("jersey.config.server.provider.packages", "my.resources;my.config");
this.server = GrizzlyWebContainerFactory.create(uri, initParams);
} catch (ProcessingException | IOException e) {
throw new TestContainerException(e);
}
}
This was based on Problems running JerseyTest when dealing with HttpServletResponse